Overview
The Edit tool makes surgical changes to files by finding and replacing exact text. It’s designed for targeted modifications and requires exact matching of whitespace, indentation, and formatting. For creating new files or complete rewrites, use the Write tool instead.Parameters
The absolute path to the file to modify. Relative paths are resolved from the working directory.
The exact text to find and replace. Must match character-for-character including all whitespace, tabs, and newlines. Leave empty to create a new file.
The replacement text. Leave empty to delete the old_string content.
If
true, replaces all occurrences of old_string. If false (default), old_string must appear exactly once.Special Operations
Create New File
Leaveold_string empty and provide new_string:
Delete Content
Provideold_string and leave new_string empty:
Replace Content
Provide bothold_string and new_string:
Critical Requirements
Exact Matching
The Edit tool is extremely literal. Every character must match exactly:- Spaces vs Tabs -
(4 spaces) ≠\t(1 tab) - Indentation -
if(2 spaces) ≠if(4 spaces) - Blank Lines -
}\n\nfunc(2 newlines) ≠}\nfunc(1 newline) - Comment Spacing -
// comment≠//comment - Brace Positioning -
func() {≠func(){ - Trailing Spaces -
return nil≠return nil
Common Failures
Uniqueness Requirement
Whenreplace_all: false (default), the old_string must appear exactly once in the file. If it appears multiple times, the tool returns an error.
Solution 1: Add More Context
Instead of:
Prerequisites
Before using the Edit tool:- Use the View tool to read the file and understand its contents
- Note the exact formatting including indentation, blank lines, and spacing
- Verify parent directory exists when creating new files (use LS tool)
- Count the indentation spaces or tabs at each level
Best Practices
Include Context
Provide 3-5 lines of context before and after the change point: ✅ Good - Includes Context:Verify Before Submitting
Checklist before each edit:- Read the file with View tool first
- Counted indentation spaces/tabs
- Included blank lines if they exist
- Matched brace/bracket positioning
- Included 3-5 lines of surrounding context
- Verified text appears exactly once (or using replace_all)
- Copied text character-for-character, not approximated
Handle Whitespace Carefully
When copying from View tool output, remember:-
Line numbers are prefixed - The View tool shows:
The actual file content is:
\tfunc example() {(note the tab) - Copy from after the line number prefix - Don’t include the spaces and numbers
- Count indentation from the actual file content, not the display
Recovery Steps
If you get “old_string not found in file”:- View the file again to see current state
- Copy more context - include entire function if needed
- Check whitespace character-by-character:
- Count indentation spaces/tabs
- Look for blank lines
- Check for trailing spaces
- Verify character-by-character that your old_string matches
- Never guess - always View the file to get exact text
- Add more context to make it unique
- Use replace_all: true if you want to change all occurrences
- Consider separate edits with unique context for each instance
Examples
Simple Replacement
Adding Code with Context
Replacing All Occurrences
Deleting Code
Multiple Edits
For multiple edits to the same file, you have two options:Option 1: Multiple Edit Calls (in one message)
Option 2: Use MultiEdit Tool
For multiple edits to the same file, consider using the MultiEdit tool instead:Permissions
The Edit tool requires permission for all write operations. You can pre-approve edits for specific paths:LSP Integration
After editing a file, Crush automatically:- Notifies LSP servers about the file change
- Waits for diagnostics (errors, warnings)
- Includes diagnostics in the tool response
Platform Notes
Line Endings
- Unix/Linux/macOS: LF (
\n) - Windows: CRLF (
\r\n)
- Normalizes to LF internally for matching
- Preserves the original line ending style when writing
\n in your old_string and new_string regardless of platform.
Path Separators
Use forward slashes/ for file paths on all platforms:
- ✅
C:/Users/name/file.txt - ❌
C:\\Users\\name\\file.txt
When to Use Edit vs Write
Use Edit When:
- Making targeted changes to existing files
- Replacing specific functions or sections
- Modifying configuration values
- Adding/removing specific code blocks
- You need precise control over what changes
Use Write When:
- Creating new files from scratch
- Completely rewriting a file
- The file content is mostly changing
- You want to replace the entire file
- The file is small and simple to regenerate