1Edit files using find-and-replace. Must read file first with `view`.
2
3<when_to_use>
4Use Edit when:
5- Making targeted changes to existing code
6- Changing specific functions, lines, or blocks
7- Single file, 1-3 changes
8
9Do NOT use Edit when:
10- Creating new files β use `write`
11- Complete file rewrite β use `write`
12- Multiple changes to same file β use `multiedit`
13- Moving/renaming files β use `bash` with `mv`
14</when_to_use>
15
16<critical_rule>
17**ALWAYS `view` the file first.** The old_string must match EXACTLYβevery space, tab, newline, and blank line.
18</critical_rule>
19
20<parameters>
21- file_path: Absolute path (required)
22- old_string: Exact text to find (required for edits, empty for new file)
23- new_string: Replacement text (required)
24- replace_all: Replace all occurrences (default: false)
25</parameters>
26
27<special_cases>
28- Create file: provide file_path + new_string, leave old_string empty
29- Delete content: provide file_path + old_string, leave new_string empty
30</special_cases>
31
32<matching_rules>
33Include 3-5 lines of context to ensure unique match:
34
35```
36Good: Match entire function signature + first lines
37old_string: "func ProcessUser(id string) error {\n if id == \"\" {\n return errors.New(\"empty\")\n }"
38
39Bad: Match just one line that appears many times
40old_string: "return nil"
41```
42
43**Tip:** In large files, include the function or class signature as context to disambiguate similar code blocks.
44</matching_rules>
45
46<common_failures>
47```
48Expected: " func foo() {" (4 spaces)
49Provided: " func foo() {" (2 spaces) β
50
51Expected: "}\n\nfunc bar()" (blank line between)
52Provided: "}\nfunc bar()" (no blank line) β
53
54Expected: "// comment" (space after //)
55Provided: "//comment" β
56```
57</common_failures>
58
59<recovery>
60If "old_string not found":
611. `view` the file at target location
622. Copy exact text character-by-character
633. Include more surrounding context
644. Check tabs vs spaces, blank lines
65</recovery>