prefer_write_tool
Block bash file operations, remind agent to use proper tools instead
Problem
AI models often use bash commands for file operations:
File Creation
code
File Reading
code
Why This is an Anti-Pattern
- •Bypasses proper tool UI/diffs/approval flow
- •Escaping issues with special characters
- •Harder to review and track changes
- •No line numbers or formatting for reading
Solution
This plugin detects bash file operations before execution and rejects them with a system reminder telling the agent to use the proper tools (read_file, write, edit).
main.py
Detected Patterns
File Creation (blocked)
•
cat <<EOF > file.py - heredoc redirection•
echo "..." > file.py - output redirection•
printf "..." > file.py - printf redirection•
cmd > file - any output redirection•
cmd >> file - append redirection•
tee file.py - tee commandFile Reading (blocked)
•
cat file.txt - read file contents•
head file.txt - read first lines•
tail file.log - read last lines•
less file.txt - page through file•
more file.txt - page through fileWhat Happens
When detected, the tool is rejected and the agent receives a system reminder:
For file creation
Bash file creation blocked. <system-reminder> You tried to create a file using bash. This is blocked. Use the Write tool instead: Write(file_path="/path/to/file.py", content="...") For editing existing files: Edit(file_path="/path/to/file.py", old_string="...", new_string="...") </system-reminder>
For file reading
Bash file reading blocked. <system-reminder> You tried to read a file using bash. This is blocked. Use the read_file tool instead: read_file(file_path="/path/to/file.txt") Why: read_file provides line numbers, proper formatting, and better control. </system-reminder>
Result
The agent will then use the proper tools (read_file, write, edit).
Combining with tool_approval
You can use both plugins together:
main.py
⚠️ Order Matters
prefer_write_tool should come first to block file creation before tool_approval prompts for approval.
