Chapter 4 · File Operations and Code Editing
Chapter goals: Master Claude Code's file read/write toolchain, understand the diff preview confirmation flow, and learn to efficiently search the codebase.
4.1 File Tool Overview
Claude Code has the following built-in file operation tools:
| Tool | Function | Requires Confirmation |
|---|---|---|
| Read | Read file contents | ❌ |
| Write | Create new files or overwrite | ✅ |
| Edit | Precise text replacement in existing files | ✅ |
| Grep | Regex search file contents (similar to ripgrep) | ❌ |
| Glob | Match file paths by pattern | ❌ |
4.2 Read: Reading Files
When you ask "what does this function do," Claude Code automatically calls Read to fetch file contents:
> What does the parse_config function in src/utils/parser.py do?It will:
- Read
src/utils/parser.pywith Read - Analyze the function logic
- Return an explanation
You can also specify a line range:
> Read lines 50-100 of app/models/user.py4.3 Write and Edit: Creating and Modifying
Creating a New File
> Create a date_utils.py under src/utils/ with functions to format dates and parse date stringsClaude Code will use Write to create the file and display a diff preview in the terminal for your confirmation:
+ src/utils/date_utils.py (new file)
+
+ from datetime import datetime, timezone
+
+
+ def format_iso(dt: datetime) -> str:
+ """Format datetime to ISO 8601 string in UTC."""
+ ...Press y to confirm, n to reject, e to edit before saving.
Modifying an Existing File
> Change ValueError to a custom ConfigParseError in parser.pyClaude Code uses the Edit tool for precise text replacement, also showing a diff.
4.4 Grep and Glob: Searching
> Find all places in the project using the deprecated requests.getInternally executes equivalent to grep -rn "requests.get" --include="*.py".
> Which Python files have "test" in their filename?Uses Glob to match **/test*.py.
4.5 Multi-File Atomic Operations
Claude Code can coordinate modifications across multiple files in a single task:
> Replace all print() calls in the project with logger.info()It will:
- Use Grep to find all
print(occurrences - Use Edit to replace one by one in each file
- Show the diff for each file
- Ask for your confirmation per file
Chapter Summary
- Five file tools: Read / Write / Edit / Grep / Glob;
- Write operations require your confirmation via diff before taking effect;
- Multi-file modifications are auto-coordinated with per-file confirmation;
- Grep/Glob are free searches that don't consume additional permissions.
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. Which tool in Claude Code is used for precise text replacement in existing files?
2. What is Claude Code's default behavior when writing or modifying files?
3. To search all Python files containing TODO comments in a project, what should you use?
4. For a cross-file task like "change all print to logging.info in the project," how does Claude Code handle it?
🛠️ Hands-on Practice
- Have Claude Code create a new module and write tests for it.
- Have it replace an old API call in your project with the new version.
- Use the search feature to find all hardcoded URLs in your project.
After completing, proceed to Chapter 5.