commands

How to Configure Hooks in Claude Code (Run X on Every Y)

3 min · updated June 10, 2026

Hooks run a shell command automatically on Claude Code events — for example, format code after every edit, or block edits to protected files. They’re defined in your settings.

1. Where hooks live

In .claude/settings.json (project) or ~/.claude/settings.json (global).

2. Example: format after every edit

Run Prettier whenever Claude edits or writes a file:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "npx prettier --write \"$CLAUDE_FILE_PATHS\"" }
        ]
      }
    ]
  }
}

3. Common events

4. Apply it

Restart Claude Code so it picks up the new settings. Test by making an edit and watching the hook fire.


Tip: start with one harmless hook (formatting) before adding blocking PreToolUse hooks — a bad matcher can get in your way.

← All guides