internal/agent/tools/edit.md
Edit a file by exact find-and-replace; can also create or delete content. For renames/moves use bash. For large edits use write.
<prerequisites> 1. Use View tool to understand file contents and context 2. For new files: Use LS tool to verify parent directory exists 3. **CRITICAL**: Note exact whitespace, indentation, and formatting from View output </prerequisites> <parameters> 1. file_path: Absolute path to file (required) 2. old_string: Text to replace (must match exactly including whitespace/indentation) 3. new_string: Replacement text 4. replace_all: Replace all occurrences (default false) </parameters><special_cases>
<critical_requirements> EXACT MATCHING: The tool is extremely literal. Text must match EXACTLY
// comment vs //comment)func() { vs func(){)Common failures:
Expected: " func foo() {" (4 spaces)
Provided: " func foo() {" (2 spaces) ❌ FAILS
Expected: "}\n\nfunc bar() {" (2 newlines)
Provided: "}\nfunc bar() {" (1 newline) ❌ FAILS
Expected: "// Comment" (space after //)
Provided: "//Comment" (no space) ❌ FAILS
UNIQUENESS (when replace_all=false): old_string MUST uniquely identify target instance
SINGLE INSTANCE: Tool changes ONE instance when replace_all=false
VERIFICATION BEFORE USING: Before every edit
<recovery_steps> If you get "old_string not found in file":
<best_practices>
<whitespace_checklist> Before submitting an edit, verify:
old_string: "func ProcessData(input string) error {\n if input == \"\" {\n return errors.New(\"empty input\")\n }\n return nil\n}"
new_string: "func ProcessData(input string) error {\n if input == \"\" {\n return errors.New(\"empty input\")\n }\n // New validation\n if len(input) > 1000 {\n return errors.New(\"input too long\")\n }\n return nil\n}"
❌ Incorrect: Not enough context
old_string: "return nil" // Appears many times!
❌ Incorrect: Wrong indentation
old_string: " if input == \"\" {" // 2 spaces
// But file actually has: " if input == \"\" {" // 4 spaces
✅ Correct: Including context to make unique
old_string: "func ProcessData(input string) error {\n if input == \"\" {\n return errors.New(\"empty input\")\n }\n return nil"
<windows_notes>