Hook

Auto-format on edit

A PostToolUse hook that runs your formatter (Prettier/Black/gofmt) on every file Claude edits — so you never review unformatted diffs.

formattingprettierposttooluse
Install to
settings.json → hooks.PostToolUse
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "f=$(jq -r '.tool_input.file_path // empty'); case \"$f\" in *.js|*.ts|*.jsx|*.tsx|*.json|*.css|*.md) npx prettier --write \"$f\" 2>/dev/null ;; *.py) black -q \"$f\" 2>/dev/null ;; *.go) gofmt -w \"$f\" 2>/dev/null ;; esac"
          }
        ]
      }
    ]
  }
}

How to install

  1. Open your settings file: ~/.claude/settings.json (user), .claude/settings.json (project, shared), or .claude/settings.local.json (project, private).
  2. Merge the hooks block into it. If a hooks key already exists, add this event to it rather than replacing the whole object.
  3. Hooks live-reload — no restart needed in most cases.
  4. Verify: run /debug to confirm the hook is registered, then trigger the event (e.g. edit a file) and watch it fire.
  5. This hook relies on jq reading the event JSON from stdin — make sure jq is installed.

← More hooks