Demo, all content is generated
Question

PostToolUse hook in settings.json never runs

Solved · 191 views · asked by arash · edited

I want prettier to run after every edit Claude makes. I put this in .claude/settings.json:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "edit",
        "hooks": [
          { "type": "command", "command": "npx prettier --write ." }
        ]
      }
    ]
  }
}

Nothing happens. No error either. Files are still formatted however Claude likes.

What I’ve tried

Checked the json is valid, restarted the terminal, also tried putting it in ~/.claude/settings.json.

Comment
Does /hooks inside Claude Code list it at all? chidi_eze · edited

2 answers

Marked as helpful by the asker
abby_ops · edited

Two things:

  1. The matcher is case sensitive and matches tool names. The tools are Edit, Write, MultiEdit, not edit. Use "matcher": "Edit|MultiEdit|Write".
  2. Formatting . on every edit is slow on a real project. The hook gets JSON on stdin with the file path, so format only that file:
{
  "type": "command",
  "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}

Run /hooks inside Claude Code to see what it actually loaded. Hooks are read when the session starts, so after editing the file start a new session (or review them in /hooks).

Comment
It was the capital E. Feel a bit dumb. Per-file version works, thanks. arash · edited
Everybody does the lowercase one once. abby_ops · edited
If you don't have jq: brew install jq. Took me a minute to realize why mine failed silently. mo_saleh · edited
chidi_eze · edited

One safety note since you're getting into hooks: they run as shell commands with your user's permissions, every time. Don't copy hook configs from random repos without reading them, and keep them in the project settings where they're visible in git.

Comment