2.5 Select and apply built-in tools (Read, Write, Edit, Bash, Grep, Glob) effectively
Pick the right built-in tool for the job (Grep for contents, Glob for paths, Read/Write for whole files, Edit for unique-anchor changes, Bash for commands) and explore codebases incrementally.
Key points
- 1
Grep searches file contents for a pattern: callers of a function (
calculateTax\(), error messages, import statements. It is built on ripgrep, so use ripgrep regex syntax and escape metacharacters (interface\{\}). - 2
Glob finds files by path pattern:
**/*.test.tsx,src/**/*.ts,*.{json,yaml}. Results are sorted by modification time. Use it for "which files match this naming convention", never for "which files contain this text". - 3
Read loads full file contents (with line numbers; also images, PDFs and notebooks). Write creates or overwrites a whole file with exactly the content given, so writing only the changed lines destroys the rest of the file.
- 4
Edit makes targeted modifications by exact string replacement and requires
old_stringto appear exactly once. If it appears more than once, either widen the anchor with surrounding context to make it unique or setreplace_all: truewhen every occurrence should change. - 5
When Edit fails because the match is not unique and no practical unique anchor exists (identical generated blocks), the reliable fallback is Read the whole file then Write the full modified content. Retrying the same Edit is deterministic and will fail again;
replace_allchanges occurrences you did not intend. - 6
Edit is the default for small changes because it costs only the changed lines; Read plus full Write for every one-line change bloats context and risks overwriting concurrent modifications (formatter hooks, other tools). Bash
sed -iby line number is fragile after edits and bypasses the Edit/Write safeguards and hook matchers. - 7
Build codebase understanding incrementally: Grep for entry points (route registrations,
main, server bootstrap), then Read those files and follow imports to trace the flow. Do not Read every file upfront and do not infer flow from file names alone. - 8
Trace usage across wrapper or barrel modules by first identifying all exported names (Read the index file), then Grep for each name across the codebase. Searching for the module path only shows which files import the barrel, not which functions they use.
- 9
Read, Grep and Glob run without permission prompts; Write, Edit and Bash require permission. Use Bash for running commands (tests, builds, git), not as a substitute for the file tools.
- 10
MCP tools with rich descriptions can complement built-ins (semantic cross-repo search alongside Grep); the built-in is still the right choice for exact-string searches in the current checkout.
Read the source
Test yourself on 2.5 Select and apply built-in tools (Read, Write, Edit, Bash, Grep, Glob) effectively
Ten questions, with the answer and explanation after each one.