OmniscioGuidesA format-on-save hook
A Claude Code hook that formats every file after an edit
Claude Code edits a file with a tool and the file changes at once. There is no editor save to hang a formatter on, so the equivalent of format on save is a hook that runs the moment the edit tool finishes.
Checked against Anthropic’s Claude Code hooks documentation on November 25, 2026. By the Omniscio team. Updated .
The hook, complete
Add this to .claude/settings.json at the root of the project. If the file already has a "hooks" object, add the PostToolUse array inside it beside the events already there; only add the "hooks" object itself when the file has none.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\""
}
]
}
]
}
}It needs jq for the JSON and Prettier in the project, which is one command: npm install --save-dev prettier. Without a local Prettier, npx tries to fetch it, and a hook is not a place where a download prompt can be answered. Install jq with brew install jq on macOS, sudo apt-get install jq on Debian and Ubuntu, or winget install jqlang.jq on Windows.
On Windows, a command hook runs in Git Bash. Without Git Bash it cannot start, and the error names that. If you would rather stay in PowerShell, set the handler’s "shell": "powershell" and write the command in PowerShell, where jq is a separate install and npx resolves differently.
A version pinned to the project
Calling the local binary skips the dependency on npx and its download:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path') && \"$CLAUDE_PROJECT_DIR\"/node_modules/.bin/prettier --write --ignore-unknown \"$f\"",
"timeout": 60
}
]
}
]
}
}On Windows that .bin entry is a .cmd shim rather than an executable, so it needs the shell form shown here rather than an args list, because Anthropic documents that a .cmd or .bat shim cannot be spawned in exec form.
Save the file and start a new session before you expect the hook to run; if /hooks does not list it, the JSON is invalid, or the session started before the file existed.
The problem it solves
Left alone, a session drifts. One edit uses single quotes, the next uses double, indentation changes halfway through a file, and a long line stays long because nothing in the loop cares. You can ask Claude to format the file and it often will, but that is a request the model may not act on, and it costs a turn every time.
A hook is the other kind of control: your command, at a fixed moment, whatever the model decides. The moment after an edit is the one that matters here, and Anthropic’s documentation has PostToolUse for exactly it.
What each line does
| Part | Job |
|---|---|
"hooks" | The one object every hook lives in. Each event name is a key inside it. |
"PostToolUse" | The event: it runs immediately after a tool call completes successfully. |
"matcher": "Edit|Write" | Filters by tool name. A value made only of letters, digits, _, -, spaces, commas and | is read as an exact string or a list of exact strings, so this matches the Edit and Write tools and nothing else. |
"type": "command" | Runs a shell command and hands it the event’s JSON on stdin. |
jq -r '.tool_input.file_path' | Pulls the absolute path of the edited file out of that JSON. For the file tools Anthropic gives the path always as absolute. |
"$f" | Quoted, so a path containing a space stays one argument. Anthropic’s security guidance asks for exactly this. |
--write | Prettier’s flag for formatting a file in place. |
--ignore-unknown | Skips files Prettier cannot parse, such as a .py, a .rb or a lockfile, instead of failing the hook on them. |
timeout | Optional, in seconds. A command hook defaults to ten minutes; a timed-out hook is cancelled and its output discarded. |
Success is quiet on purpose. A PostToolUse hook that exits 0 prints nothing to the conversation, and Claude never sees its stderr, so the file is the only evidence. That is the first thing to understand about debugging it.
Why PostToolUse and not PreToolUse
PreToolUse runs after Claude has built the tool’s parameters and before the tool call is processed. Formatting there is a race: the edit has not been written yet, so the formatter sees the old file, or the edit overwrites what it did.
PostToolUse runs after the tool completed, so the new content is on disk. The trade-off is stated plainly in the documentation: a PostToolUse hook cannot undo anything, because the tool has already run. For a formatter that is fine, because formatting the file is the whole intent. For anything that must prevent a change, the event is PreToolUse, which the blocking recipe covers.
Making it fire only on the files you want
The matcher is about the tool, not the file. Edit|Write already keeps the hook off Bash, Read and every other tool, but it does not care whether the file is a .ts or a .png.
Filter with if, using permission-rule syntax. It sits inside the handler object, beside type and command, and this runs the hook only for TypeScript edits:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"if": "Edit(*.ts)",
"command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write \"$f\""
}
]
}
]
}
}Anthropic evaluates if only on tool events, and a hook that carries if on any other event never runs at all. The other two ways to narrow it are a check in the script, for example case "$f" in *.py) ruff format "$f" ;;, or letting --ignore-unknown and a .prettierignore decide, which is the least code and the least surprise.
One scoping fact matters for a monorepo: handlers run in the current directory with Claude Code’s environment, so a relative formatter path resolves against wherever the session is. That is why the pinned version above uses $CLAUDE_PROJECT_DIR.
How to test that it fires
- Check the two dependencies first.
jq --versionmust answer, andnpx prettier --versionin the project root must answer without downloading anything new. A hook that cannot find either fails before it formats anything. - Check the hook is registered. Type
/hooksin a session: it is a read-only browser that lists each hook under its event, and selecting one shows the settings file it came from. - Run the block’s own command by hand, with sample JSON. This is the test that separates a broken command from a hook that never ran, and it uses the exact command from the settings file rather than a rewrite of it:
mkdir -p /tmp/hooktest && printf "const a = 'x'\nconsole.log(a)\n" > /tmp/hooktest/app.js echo '{"tool_name":"Edit","tool_input":{"file_path":"/tmp/hooktest/app.js"}}' \ | sh -c 'f=$(jq -r ".tool_input.file_path") && npx prettier --write --ignore-unknown "$f"' cat /tmp/hooktest/app.jsWith Prettier’s defaults the single quotes become double quotes, which is the same check Anthropic’s quickstart describes: ask Claude to add a line with single-quoted strings, then open the file. - Ask Claude for a real edit and open the file. Nothing appears in the conversation when the hook works, so confirm the change on disk.
- Read the debug log when it does not. Start with
claude --debug-file /tmp/claude.logand follow the file, or run/debugmid-session to turn logging on and see where it went. In the transcript view (Ctrl+O) a blocking error shows its message and a successful hook shows nothing.
The command in step three is Anthropic’s, and we did not run a Claude Code session with it wired in, so what this page describes is the documented behaviour of that block rather than a live run.
What makes this hook do nothing
1. jq is not installed
The hook exits non-zero before Prettier is reached. You see a PostToolUse hook error notice carrying something like jq: command not found. Claude does not see it, because a missing jq produces an ordinary non-zero code rather than the blocking one — on PostToolUse, exit 2 is what puts a hook’s stderr in front of Claude.
2. The formatter writes nothing
A .prettierignore entry makes Prettier skip the file silently and exit 0, which looks exactly like success from every angle. Run the command by hand on that one file and read what Prettier prints. A file Prettier cannot parse is a different failure: without --ignore-unknown it exits with an error, which the block above surfaces to Claude.
3. The edit happened, the hook did not
A PostToolUse hook matching Edit|Write does not fire when a Bash command rewrites the same file, because no edit tool ran. Anthropic documents FileChanged for that: it fires when a watched file changes on disk whatever wrote it, but it has no decision control, so it can format and cannot block. A hook from a settings file also runs inside subagents, so an edit made by a subagent is covered.
4. The wrong event
A PreToolUse hook on Edit|Write looks right and is not: it runs before the write, so the formatting is overwritten by the edit that follows. If formatting appears to work only sometimes, check which event the block is under.
5. Windows without Git Bash
A shell-form hook runs in Git Bash on Windows. Without Git for Windows it cannot start at all, and the notice says so rather than blaming your formatter. Install Git for Windows, or set "shell": "powershell" and translate the command. A related trap inside a script: the file_path arrives with backslashes on Windows, so a forward-slash pattern never matches and the script silently does nothing.
6. A hook that cannot start at all
A mistyped settings file, a script path that does not exist or a file without the execute bit leave the hook never running, and on most events that is a non-blocking error, so the edit simply proceeds. If the hook must block when it cannot start, that is what onFailure: "block" does, and it requires Claude Code v2.1.295 or later.
One warning belongs here too. Command hooks run with your full user permissions, so a formatter you would not run by hand in a repository you do not trust is not a formatter you should wire into a hook. Anthropic’s guidance is to review and test every hook command before adding it, and to keep sensitive files such as .env and .git/ out of what a hook touches.
Where Omniscio fits
This hook belongs in the project, and a project file is the right place for it: commit .claude/settings.json and every teammate gets the same formatting rule. Omniscio takes the other angle. Its Hooks panel composes the hooks it ships, and any you define, into every session it spawns, keeping them in its own database rather than writing into your global Claude configuration, with a global switch and per-project overrides. The Claude Code hooks guide covers that panel, and the hooks builder assembles a block like this one from the event, matcher and handler you pick.
Omniscio runs on Windows, macOS and Linux, and the pricing page covers the plans.
How this page was written
Everything about events, matchers, the if field, input fields and timeouts was read from Anthropic’s Claude Code hooks reference on November 25, 2026, quoted from the page as it reads today. The example block and its test come from Anthropic’s hooks quickstart, which is the source named below.
The block is Anthropic’s example, reproduced as documented. No Claude Code session was configured to run it for this page, so the page describes the documented behaviour of the block and does not claim to have watched it fire.
Sources
- Anthropic’s Hooks reference, for the events, matcher rules, handler fields,
tool_input, exit codes, timeouts,FileChangedand the security guidance, read November 25, 2026. - Anthropic’s Automate actions with hooks, for the auto-format example and its test, read November 25, 2026.
- Prettier’s CLI documentation, for
--writeand--ignore-unknown, read November 25, 2026.
Common questions
Does Claude Code have a format-on-save setting?
No. There is no save step to hang it on: Claude edits a file with a tool and the file changes immediately. The equivalent is a PostToolUse hook with an Edit|Write matcher, which runs your formatter after the edit tool finishes.
Why does my hook run but nothing changes?
The usual causes are a missing jq, which fails the hook before Prettier is called, a file listed in .prettierignore, or Prettier not installed in the project so npx cannot resolve it inside a hook. Check the file, then read the debug log.
Can I format only TypeScript files?
Yes. The matcher filters on tool name, not on path, so keep Edit|Write and add an if field such as Edit(*.ts) to the handler, or have the script check the extension itself. The if field is evaluated only on tool events.
Does the hook catch files changed by a Bash command?
No. A PostToolUse hook matching Edit|Write does not fire when a Bash command rewrites the same file. Anthropic documents FileChanged for that case: it fires after a watched file changes on disk, whatever wrote it, but it has no decision control and cannot block.
Does this work with ESLint, Ruff or gofmt?
Yes. The handler runs whatever command you give it, so the shape is the same and only the formatter changes: ruff format "$f", gofmt -w "$f", or a npm run lint -- --fix "$f" of your own. Keep --ignore-unknown only if the tool has an equivalent; otherwise let the script skip extensions it does not handle.
Every session, the same rules
Omniscio runs your Claude Code sessions side by side and composes the hooks it ships, and any you define, into every one it spawns, so a formatting rule does not depend on which folder you opened.
The features page covers what else it does, pricing covers the plans, and you can download it for Windows, macOS or Linux.