How to Auto-Format Files Edited by Claude Code
You will configure Claude Code to format files with Prettier immediately after every modification. Prompting an agent in CLAUDE.md to format code relies on probabilistic instructions that fail under high context load. Setting up a PostToolUse hook in .claude/settings.json provides deterministic formatting by executing Prettier at the system level whenever Claude Code runs the Edit or Write tools, completely outside the model's reasoning loop.
Before you start
- Claude Code installed on your development machine.
jqinstalled and accessible in your systemPATH.- Prettier installed in your project (
npm install --save-dev prettier).
How the hook runs
flowchart LR
A[Claude Code calls Edit or Write] --> B[Tool updates target file]
B --> C[PostToolUse hook fires]
C --> D[Pipes JSON payload to stdin]
D --> E[jq parses tool_input.file_path]
E --> F[Prettier formats the edited file]
Step 1: Choose the configuration scope
Claude Code reads settings from three locations: ~/.claude/settings.json for global machine rules, .claude/settings.local.json for gitignored local overrides, and .claude/settings.json for repository-wide configuration. Use the project-level file so your entire team shares the same formatting hook.
mkdir -p .claude
Step 2: Add the PostToolUse hook configuration
Open .claude/settings.json and declare a PostToolUse entry with a matcher for file edits. When triggered, Claude Code pipes an event payload to stdin. Extract .tool_input.file_path using jq and pass it directly to Prettier.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "FILE=$(jq -r '.tool_input.file_path // empty') && [ -n \"$FILE\" ] && npx prettier --write --ignore-unknown \"$FILE\" 2>/dev/null || true"
}
]
}
]
}
}
Step 3 (optional): Delegate execution to a bash script
Inline shell scripts become fragile as logic grows. Move the formatting instructions into a dedicated script to guard against environments without Prettier and prevent failed hook executions on non-code assets.
Create .claude/hooks/prettier-formatter.sh:
#!/bin/bash
set -e
input=$(cat)
file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
if [ -z "$file_path" ] || [ "$file_path" = "null" ]; then
exit 0
fi
if [ -f "package.json" ] && grep -q '"prettier"' package.json; then
npx prettier --write --ignore-unknown "$file_path" 2>/dev/null || true
fi
Make the script executable:
chmod +x .claude/hooks/prettier-formatter.sh
Point your .claude/settings.json hook command to the script:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "bash .claude/hooks/prettier-formatter.sh"
}
]
}
]
}
}
Step 4: Verify hook discovery
Start a new Claude Code session in your terminal. Run the /hooks command to confirm that the project configuration registered correctly.
/hooks
Confirm that the output lists your PostToolUse hook sourced from .claude/settings.json.
Step 5: Test the formatting pipeline
Ask Claude Code to generate a file with inconsistent indentation or formatting to verify that the hook executes on disk.
Write a file named test.js with poorly formatted JavaScript code.
Inspect test.js after the operation finishes. Claude Code executes the Write tool, triggers the hook on the emitted path, and leaves the file formatted according to your repository rules.
Common mistakes
- Running full repository checks.
PostToolUsehooks run synchronously and block the agent turn until the command exits. Never run whole-project formatters or full test suites in a tool hook. Target only the extracted file path. - Incorrect tool matcher casing. Claude Code tool names are case-sensitive. Using lowercase
"edit|write"fails to match any tool execution. Use"Edit|Write"or"Edit|MultiEdit|Write"to catch batched edits. - Silent failures on missing binaries. If
jqis not found in your environment, shell hooks abort without surfacing visible errors in the prompt. Test your hook manually by piping a mock payload:echo '{"tool_input": {"file_path": "test.js"}}' | bash .claude/hooks/prettier-formatter.sh.
Related
- Blocking AI Mistakes: Proofs vs Planning
- Why AI Agents Prefer Grep Over LSPs
- Hidden Costs of AI Dev Tools in 2026
- Consult the official Claude Code Hooks documentation for schema details and lifecycle hooks.