prefer_write_tool
Block bash file creation, soft-remind for file reading
Problem
AI models often use bash commands for file operations:
File Creation
File Reading
File creation via bash bypasses tool UI/diffs/approval flow and has escaping issues. File reading via bash works but misses line numbers, formatting, and control that read_file provides.
Solution
This plugin uses two strategies:
File Creation
Hard block — raises ValueError, agent must use Write or Edit tool
File Reading
Soft reminder — command runs, system reminder appended suggesting read_file
Detected Patterns
File Creation (hard blocked)
cat <<EOF > file.py - heredoc redirectionecho "..." > file.py - output redirectionprintf "..." > file.py - printf redirectioncmd > ./file - output redirection to pathcmd >> ./file - append redirection to pathtee file.py - tee commandFile Reading (soft reminder)
cat file.txt - standalone cat (not piped)head file.txt - read first linestail file.log - read last linesless file.txt - page through filemore file.txt - page through fileNote: cat file | grep pattern (piped cat) is not detected — piping is legitimate bash usage.
What Happens
For file creation (blocked)
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 (soft reminder)
[actual command output here] <system-reminder> You used bash to read a file. Consider using 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
File creation is stopped before execution. File reading runs normally with a gentle nudge toward read_file.
How It Works
The plugin exports two event handlers:
block_bash_file_creation (before_each_tool) — detects file creation patterns and raises ValueError. For file reading, sets a session flag.
remind_read_file (after_each_tool) — if the flag is set, appends a system reminder to the tool result message.
Combining with tool_approval
You can use both plugins together:
Order Matters
prefer_write_tool should come first to block file creation before tool_approval prompts for approval.
