Claude Code writes decent code, but it doesn't always match your formatter. You can keep asking it to "run prettier", or you can make formatting automatic with a PostToolUse hook.
What a PostToolUse hook does
Hooks are shell commands Claude Code runs at fixed points. A PostToolUse hook runs right after a tool call finishes. Point it at the edit tools and your formatter runs every time a file changes.
Add this to .claude/settings.json in your project:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs -r npx prettier --write --ignore-unknown"
}
]
}
]
}
}
The hook gets the tool call as JSON on stdin. jq pulls out the edited file's path, and Prettier formats just that file. --ignore-unknown keeps it quiet on file types Prettier doesn't handle.
Other languages
Swap the formatter for your stack:
- Python:
xargs -r ruff format - Go:
xargs -r gofmt -w - Rust:
xargs -r rustfmt
Tips
- Keep the command fast. It runs after every edit, so format one file, not the whole repo.
- Commit
.claude/settings.jsonso the whole team gets the same behavior. - Run
/hooksin Claude Code to check the hook is loaded.
Shared settings, a risky-git PreToolUse hook, a short CLAUDE.md and a read-only reviewer subagent all ship ready to copy in Vildanden Agent Config. The free sample is enough to get started today.
Top comments (0)