Skill: Robust Surgical Editing
This skill provides a three-step protocol to ensure that the replace tool succeeds on the first attempt by eliminating guesswork regarding whitespace and literal content.
The Protocol
Step 1: Locating the Target (Grep)
Use grep_search with a specific pattern to identify the target file and the approximate line numbers. Do not proceed until you have a confirmed file path and a line number range.
Step 2: Capturing Literal Content (Read Exact)
Once the region is identified, use read_file with the start_line and end_line parameters.
- Goal: Capture the EXACT literal string of the code you intend to replace.
- Critical: This includes all leading/trailing whitespace, tabs, and newline characters.
Step 3: Executing the Replacement (Replace)
Using the literal string captured in Step 2 as your old_string, perform the replacement.
- Instruction: In the
replacetool call, provide the literal text exactly as it appeared in theread_fileoutput. - Validation: If the tool returns a "replacement failed" error, DO NOT RETRY with a guessed string. Repeat Step 2 to re-capture the literal content and identify any invisible characters or unexpected formatting.
Best Practices
- Minimize Scope: Keep the
old_stringas small as possible while remaining unique. - Avoid Wildcards: The
replacetool expects a literal match. Do not use regex or placeholders inold_string. - Batching: You can read multiple regions in parallel before executing a batch of replacements.