1.3 KiB
1.3 KiB
File Editing
Always use file_* tools for file manipulation operations. Never use shell commands for file text manipulation.
Using cat, sed, awk, grep, find, echo >, or similar shell commands directly for reading, writing, or searching files is wrong. The file_* tools exist precisely to replace them. Use them every time, without exception.
Tools
| Tool | Replaces |
|---|---|
file_read |
cat, head, tail, less |
file_write |
echo >, tee, cat > |
file_edit |
sed -i, awk, manual patch |
file_glob |
find, ls |
file_grep |
grep, rg |
Rules
- Read before writing. Always call
file_readon a file beforefile_writeorfile_edit. This is enforced — writes will fail without a prior read. - Prefer
file_editoverfile_writefor partial changes. - To create or modify a file with content you provide, always use
execute_toolwithfile_writeorfile_edit— neverexecute_code. Capturing process output to a file (e.g.make > make.log,openssl genrsa > key.pem) is fine.
Examples
file_read: {"path": "/abs/path/to/file.go"}
file_edit: {"path": "/abs/path/to/file.go", "old": "foo()", "new": "bar()"}
file_glob: {"pattern": "src/**/*.go"}
file_grep: {"pattern": "func.*Handler", "path": "src/"}