edit_file
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.
Parameters
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.
What It Does
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 is it used?
- When making a targeted change to a specific, uniquely identifiable location in a file
- When updating a specific string literal or configuration value at a known location
- When fixing a specific instance of a typo or outdated terminology
- When replacing a uniquely-identified occurrence of a deprecated API or import path
- When creating a new file or appending content to an existing file (
old_string="") - When you need to ensure exact match replacement without fuzzy logic
Key Features
- 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
Limitations
- 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
How It Works
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.
Relation to Other Tools
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.