| description | Replace a uniquely-identified occurrence of text in files using the edit_file search-and-replace tool in Roo Code. | ||||||
|---|---|---|---|---|---|---|---|
| keywords |
|
The edit_file tool performs targeted search-and-replace operations on files. By default it replaces exactly one uniquely-identified occurrence and errors if multiple matches are found. It also supports a special file-creation mode when old_string is empty.
The tool accepts these parameters:
file_path(required): The path of the file to modify relative to the current working directory.old_string(required): The exact text to search for and replace. Pass an empty string ("") to create a new file or append to an existing file.new_string(required): The replacement text.expected_replacements(optional): Expected number of replacements (defaults to 1). The operation fails if the actual count doesn't match. Use this only when intentionally replacing more than one occurrence.
This tool searches for an exact string in a file and replaces exactly one occurrence with new text. The search string must uniquely identify the target location. If multiple matches are found, the tool returns an error unless expected_replacements is explicitly set to match. When old_string is empty, the tool creates a new file or appends new_string to an existing file.
- When renaming variables, functions, or identifiers throughout a file
- When updating repeated string literals or configuration values
- When fixing consistent typos or outdated terminology
- When replacing all instances of a deprecated API or import path
- When you need to ensure exact match replacement without fuzzy logic
- Replaces exactly one uniquely-identified occurrence by default
- Errors if multiple matches are found (unless
expected_replacementsis explicitly set) old_string=""mode: creates a new file or appends content to an existing file- Exact string matching (no regex or fuzzy matching)
- Optional
expected_replacementsfor intentional multi-occurrence replacements - Shows preview of changes before applying
- Fails safely if actual replacement count doesn't match
expected_replacements - Preserves file formatting and structure
- Requires exact string matches (case-sensitive, whitespace-sensitive)
- Errors if the search string matches more than one location (unless
expected_replacementsis set) - Cannot use regular expressions or patterns
- Not suitable for context-dependent replacements
- Less precise than
apply_difffor complex edits
When the edit_file tool is invoked, it follows this process:
- Parameter Validation: Validates required
file_path,old_string, andnew_stringparameters. - File Creation Mode: If
old_stringis empty (""), creates the file withnew_stringas content (or appends if the file already exists), then stops. - File Loading: Reads the target file content.
- Uniqueness Check: Counts occurrences of
old_string. If the count doesn't matchexpected_replacements(default: 1), returns an error. - Replacement: Replaces the matched occurrence(s) with
new_string. - User Review: Shows a preview of changes for user approval.
- Application: Applies changes to the file if approved.
- Feedback: Reports the number of replacements made.
edit_file: Replaces exactly one uniquely-identified occurrence by default; supportsold_string=""file creation (this tool)edit: Replaces first occurrence only (unlessreplace_all: true)search_replace: Also replaces exactly one uniquely-identified occurrenceapply_diff: Use for precise, context-aware edits with fuzzy matching
These are different implementations of search-and-replace with varying capabilities.