openhands.runtime.plugins.agent_skills.file_ops.file_ops
file_ops.py
This module provides various file manipulation skills for the OpenHands agent.
Functions:
- open_file(path: str, line_number: int | None = 1, context_lines: int = 100): Opens a file and optionally moves to a specific line.
- goto_line(line_number: int): Moves the window to show the specified line number.
- scroll_down(): Moves the window down by the number of lines specified in WINDOW.
- scroll_up(): Moves the window up by the number of lines specified in WINDOW.
- create_file(filename: str): Creates and opens a new file with the given name.
- search_dir(search_term: str, dir_path: str = './'): Searches for a term in all files in the specified directory.
- search_file(search_term: str, file_path: str | None = None): Searches for a term in the specified file or the currently open file.
- find_file(file_name: str, dir_path: str = './'): Finds all files with the given name in the specified directory.
- edit_file_by_replace(file_name: str, to_replace: str, new_content: str): Replaces specific content in a file with new content.
- insert_content_at_line(file_name: str, line_number: int, content: str): Inserts given content at the specified line number in a file.
- append_file(file_name: str, content: str): Appends the given content to the end of the specified file.
open_file
def open_file(path: str,
line_number: int | None = 1,
context_lines: int | None = WINDOW) -> None
Opens the file at the given path in the editor. IF the file is to be edited, first use scroll_down
repeatedly to read the full file!
If line_number is provided, the window will be moved to include that line.
It only shows the first 100 lines by default! context_lines
is the max number of lines to be displayed, up to 100. Use scroll_up
and scroll_down
to view more content up or down.
Arguments:
path
- str: The path to the file to open, preferred absolute path.line_number
- int | None = 1: The line number to move to. Defaults to 1.context_lines
- int | None = 100: Only shows this number of lines in the context window (usually from line 1), with line_number as the center (if possible). Defaults to 100.
goto_line
def goto_line(line_number: int) -> None
Moves the window to show the specified line number.
Arguments:
line_number
- int: The line number to move to.
scroll_down
def scroll_down() -> None
Moves the window down by 100 lines.
Arguments:
None
scroll_up
def scroll_up() -> None
Moves the window up by 100 lines.
Arguments:
None
create_file
def create_file(filename: str) -> None
Creates and opens a new file with the given name.
Arguments:
filename
- str: The name of the file to create.
edit_file_by_replace
def edit_file_by_replace(file_name: str, to_replace: str,
new_content: str) -> None
Edit an existing file. This will search for non-empty to_replace
in the given file and replace it with non-empty new_content
.
to_replace
and new_content
must be different! Split large edits into multiple smaller edits if necessary!
Use append_file
method for writing after create_file
!
Every to_replace must EXACTLY MATCH the existing source code, character for character, including all comments, docstrings, etc.
Include enough lines to make code in to_replace
unique. to_replace
should NOT be empty.
For example, given a file "/workspace/example.txt" with the following content:
EDITING: If you want to replace the second occurrence of "line 2", you can make to_replace
unique:
edit_file_by_replace( '/workspace/example.txt', to_replace='line 2 line 3', new_content='new line line 3', )
This will replace only the second "line 2" with "new line". The first "line 2" will remain unchanged.
The resulting file will be:
REMOVAL: If you want to remove "line 2" and "line 3", you can set new_content
to an empty string:
edit_file_by_replace( '/workspace/example.txt', to_replace='line 2 line 3', new_content='', )
new_content
0
new_content
1
Arguments:
new_content
2 - str: The name of the file to edit.to_replace
- str: The content to search for and replace.new_content
- str: The new content to replace the old content with.
insert_content_at_line
def insert_content_at_line(file_name: str, line_number: int,
content: str) -> None
Insert content at the given line number in a file. This will NOT modify the content of the lines before OR after the given line number.
For example, if the file has the following content:
and you call insert_content_at_line('file.txt', 2, 'new line')
, the file will be updated to:
line 1
line 2
line 3
line 1
new line
line 2
line 3
Arguments:
file_name
- str: The name of the file to edit.line_number
- int: The line number (starting from 1) to insert the content after.content
- str: The content to insert.
append_file
def append_file(file_name: str, content: str) -> None
Append content to the given file.
It appends text content
to the end of the specified file, ideal after a create_file
!
Arguments:
file_name
- str: The name of the file to edit.content
- str: The content to insert.
search_dir
def search_dir(search_term: str, dir_path: str = './') -> None
Searches for search_term in all files in dir. If dir is not provided, searches in the current directory.
Arguments:
search_term
- str: The term to search for.dir_path
- str: The path to the directory to search.
search_file
def search_file(search_term: str, file_path: str | None = None) -> None
Searches for search_term in file. If file is not provided, searches in the current open file.
Arguments:
search_term
- str: The term to search for.file_path
- str | None: The path to the file to search.
find_file
def find_file(file_name: str, dir_path: str = './') -> None
Finds all files with the given name in the specified directory.
Arguments:
file_name
- str: The name of the file to find.dir_path
- str: The path to the directory to search.