Ravindra BagaleCourses & study guides Track your progress

Guides · Generative AI

Claude Code Hooks Examples: Auto-Format, Block Files and Get Notified

Hooks are shell commands that Claude Code runs automatically at set moments, such as before a tool runs (PreToolUse), after it runs (PostToolUse), when Claude finishes (Stop) or needs you (Notification). As of 25 October 2026, the docs say you configure them in .claude/settings.json and can view them with /hooks. A PreToolUse hook that exits with code 2 blocks the action and sends its stderr message back to Claude. Hooks run with your permissions, so read every hook before adding it.

Suppose Rahul in Chennai keeps forgetting to run Prettier, and Chloe in Paris worries the agent might edit her .env file. Rahul adds a PostToolUse hook that formats every edited file; Chloe adds a PreToolUse hook that blocks edits to .env.

Related pages

See also Claude Code Skills Examples, and AGENTS.md vs CLAUDE.md vs Cursor Rules.

What you will learn in this guide

  • What hooks are
  • Main hook events
  • Exit code 2 blocks
  • Three copy-paste hooks
  • Safety rules

1. Why: rules that always run

Instructions in CLAUDE.md are advice the model may forget. Hooks are code that always runs, so they are good for formatting, blocking and alerts.

2. What: main events

Event When it runs
PreToolUse Before a tool runs; can block
PostToolUse After a tool succeeds
Notification When Claude needs your input
Stop When Claude finishes a reply
SessionStart When a session starts or resumes
Your first hook in five steps Steps from top to bottom: 1 Pick an event, Pre or PostToolUse; 2 Pick a matcher, Edit or Write; 3 Write the command, Reads JSON on stdin; 4 Add to settings.json, .claude folder; 5 Check with /hooks, Then test. 1Pick an eventPre or PostToolUse 2Pick a matcherEdit or Write 3Write the commandReads JSON on stdin 4Add to settings.json.claude folder 5Check with /hooksThen test

Figure 1. Your first Claude Code hook in five steps: pick an event, a matcher, write the command, add to settings.json, check with /hooks.

3. How: add a hook

  1. Install jq (hooks receive JSON on stdin).
  2. Open or create .claude/settings.json in your project.
  3. Paste one hook from the examples below.
  4. Start claude and type /hooks to confirm it loaded.
  5. Do a small edit and watch the hook run.

4. Source

  • Claude Code docs, hooks guide and reference (checked 25 Oct 2026).
  • Exit code 2 in PreToolUse blocks the tool call; stderr is shown to Claude.
  • Stop hooks should check stop_hook_active to avoid endless loops.

5. Worked example 1: auto-format after edits (start here)

In .claude/settings.json:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command",
            "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write" }
        ]
      }
    ]
  }
}

Expected output (yours will be similar, not the same):

After Claude edits app.js you see Prettier output like: app.js 45ms

Needs Node.js and Prettier available in the project.

6. Worked example 2: block edits to .env

Save as .claude/hooks/protect-files.sh and run chmod +x on it:

#!/bin/bash
FILE=$(jq -r '.tool_input.file_path // empty')
case "$FILE" in
  *.env|*.env.*|*/secrets/*)
    echo "Blocked: $FILE is protected" >&2
    exit 2 ;;
esac
exit 0

Expected output (yours will be similar, not the same):

When Claude tries to edit .env: Blocked: /project/.env is protected

Then register it in settings.json under PreToolUse with matcher "Edit|Write" and command "$CLAUDE_PROJECT_DIR"/.claude/hooks/protect-files.sh.

7. Worked example 3: desktop alert when Claude needs you (Linux)

Add under "hooks":

"Notification": [
  {
    "hooks": [
      { "type": "command",
        "command": "notify-send 'Claude Code' 'Claude needs your input'" }
    ]
  }
]

Expected output (yours will be similar, not the same):

A desktop popup: Claude Code — Claude needs your input

On macOS use osascript; on Windows use a PowerShell toast command.

Names, amounts and companies in the examples are made up for practice.

8. Common mistakes

Mistake Risk Fix
Copying hooks you do not understand Runs any command Read every line
Stop hook without stop_hook_active check Endless loop Check the flag
Wrong exit code Block not working Use exit 2
Missing jq Hook errors Install jq
Script not executable Permission denied chmod +x

9. Try at home

Try at home

  1. Add the Prettier hook.
  2. Check /hooks.
  3. Add the .env blocker and test it.

Recap

In short

  1. Hooks are commands run on events.
  2. Configure in .claude/settings.json; view with /hooks.
  3. PreToolUse exit 2 blocks.
  4. Great for format, block, notify.
  5. Read every hook; they run with your permissions.

Advice can be forgotten; hooks always run.

Frequently asked questions

Hooks vs skills?

Hooks run automatically on events. Skills run when you or Claude call them.

Can hooks see what Claude is doing?

Yes, they receive JSON on stdin, such as tool_name and tool_input.

Do hooks work on Windows?

Yes, but commands must suit your shell; WSL makes bash examples easier.

Where else can hooks live?

In ~/.claude/settings.json for all projects, or .claude/settings.local.json for personal, uncommitted ones.

Related: Claude Code Skills Examples: Create Your First SKILL.md