Claude Code hooks can run a formatter automatically after successful file edits. Configure a PostToolUse hook that matches Edit or Write, extracts the changed file’s path from the hook input, and passes that path to the formatter.
What Claude Code Hooks Do After a File Edit
Claude Code hooks run commands at selected lifecycle events. A hook configuration combines an event name, an optional matcher, and a command. For post-edit formatting, the documented workflow uses PostToolUse because this event occurs after a tool completes successfully.
The matcher Edit|Write limits the hook to Claude Code operations that edit an existing file or write a file. When one of those operations succeeds, the hook receives structured JSON input. That input includes fields such as hook_event_name, tool_name, and tool_input; tool_input contains the arguments supplied to the triggering tool, including the relevant file path.
This makes the hook deterministic: instead of relying on an instruction that Claude should remember to format each change, the configured command runs whenever the matching event occurs.

How to Configure a PostToolUse Formatting Hook
Add a PostToolUse entry to the appropriate Claude Code settings file. The provider-documented Prettier configuration uses this structure:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
]
}
}
The PostToolUse event determines when the hook is considered. The Edit|Write matcher determines which successful tool operations trigger it. The command then reads the hook’s JSON input from standard input. jq -r '.tool_input.file_path' selects the edited path, and xargs passes that value to npx prettier --write.
For a team-wide configuration, the hook can be placed in .claude/settings.json and checked into the repository. If the command invokes a script stored in the project, official Academy guidance recommends using CLAUDE_PROJECT_DIR when referring to that script. Before sharing the configuration, review the formatter command and confirm that repository-level automation is the intended scope.
This is a documented configuration pattern, not a claim that the command was independently executed or tested for this article.

Automated Hooks vs. Manual Formatting
An automated hook is preferable when the same formatter should run reliably after every matching Claude Code edit. It removes the repeated step of entering a formatting command and avoids depending on a prompt or project instruction being followed each time. A repository-level configuration can also give contributors the same hook behavior.
Manual formatting remains a reasonable editorial choice when formatting is occasional or when a person should review each situation before running the command. It requires more deliberate action but preserves per-run control. This frequency-and-control distinction is practical guidance; the documented product distinction is that a matching hook executes deterministically at its configured event.
Consider the scope as well as convenience. A personal workflow may call for an individual settings choice, while a team that wants consistent behavior can share .claude/settings.json. If every successful Edit or Write operation should lead directly to formatting, automation fits the requirement. If formatting should happen only after selective review, run the formatter manually.
Conclusion
Claude Code formatting hooks are best suited to repeatable rules that should run after every matching successful edit; manual commands offer more control for occasional or review-sensitive work. Official guidance identifies Prettier, Black, and gofmt as formatter examples for PostToolUse automation.
Choose the appropriate personal or repository scope, review the command, and then add the documented PostToolUse structure with the Edit|Write matcher to the corresponding Claude Code settings file.
Disclosures and limitations
- This article was created with AI assistance and is based on the cited Claude Code documentation. The described workflow is not presented as an independent hands-on test.
- Tool mentions are informational and do not constitute a product ranking or purchasing recommendation.
Sources
- Curated research source from code.claude.com — code.claude.com
- HowTo-ClaudeCodeWeb/kb-article-062-configure-hooks-auto-format-code-after-edits.md at main · geeohme/HowTo-ClaudeCodeWeb — GitHub
- 5 Claude Code Hooks That Automate My Entire Workflow — DEV Community
- Claude Code Hooks: Auto-Format, Auto-Lint, Auto-Test Every Change – Wouter Gerrits — Wouter Gerrits — AI Infrastructure for Sales & Marketing Teams
- Automate actions with hooks – Claude Code Docs — Claude Code Docs
- Claude Code power user customization: How to configure hooks — Claude
- Hooks · Claude Code 101 · Claude Academy — Claude Academy
- GitHub – ryanlewis/claude-format-hook: Auto-format code when Claude Code edits files. Supports JavaScript/TypeScript, Python, Go, Kotlin, and Markdown with popular formatters like Biome, Ruff, and Prettier. — GitHub
- Claude Code hooks: run scripts automatically before and after every AI edit — DEV Community
Improve this guide
Report a problem or suggest the next question
Submissions enter a private editorial queue. They are reviewed and deduplicated before any public update or new page is created.
