# Claude Code hooks examples

> A Claude Code hook is a shell command that runs at a fixed point, such as before a tool call or after a file edit, so a rule holds every time instead of when Claude remembers it. Copy the settings file and the three scripts below into .claude, make the scripts executable, and restart Claude Code.

Updated November 30, 2026. Canonical: https://sniptide.com/templates/claude-code-hooks
Open it in the Sniptide editor: https://app.sniptide.com/new?template=claude-code-hooks
Files as JSON: https://sniptide.com/templates/claude-code-hooks.json

## Files

### .claude/settings.json

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/format.sh"
          }
        ]
      }
    ],
    "Notification": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/notify.sh"
          }
        ]
      }
    ]
  }
}
```

### .claude/hooks/protect-files.sh

```bash
#!/usr/bin/env bash
# Blocks edits to secrets, lockfiles and merged migrations before Claude makes them.
# Exit code 2 cancels the tool call and sends stderr back to Claude as the reason.
set -euo pipefail

file=$(jq -r '.tool_input.file_path // empty')
if [ -z "$file" ]; then exit 0; fi

case "$file" in
  *.env.example) ;;
  *.env | *.env.* | */.git/*)
    echo "Blocked: $file holds secrets or git internals. Ask the user to change it." >&2
    exit 2
    ;;
  *package-lock.json | *pnpm-lock.yaml | *yarn.lock | *bun.lock)
    echo "Blocked: $file is generated. Change package.json and run the install command." >&2
    exit 2
    ;;
  */migrations/*)
    if git -C "$CLAUDE_PROJECT_DIR" ls-files --error-unmatch "$file" >/dev/null 2>&1; then
      echo "Blocked: $file is a committed migration. Add a new migration instead." >&2
      exit 2
    fi
    ;;
esac

exit 0
```

### .claude/hooks/format.sh

```bash
#!/usr/bin/env bash
# Formats each file Claude writes, so diffs stay clean without asking Claude to run Prettier.
set -euo pipefail

file=$(jq -r '.tool_input.file_path // empty')
if [ -z "$file" ] || [ ! -f "$file" ]; then exit 0; fi

case "$file" in
  *.ts | *.tsx | *.js | *.jsx | *.json | *.css | *.md | *.mdx | *.yml | *.yaml)
    cd "$CLAUDE_PROJECT_DIR"
    npx --no-install prettier --write --log-level warn "$file" || true
    ;;
esac

exit 0
```

### .claude/hooks/notify.sh

```bash
#!/usr/bin/env bash
# Shows a desktop notification when Claude is waiting for you.
set -euo pipefail

message=$(jq -r '.message // "Claude Code needs your attention"')

if command -v osascript >/dev/null 2>&1; then
  osascript -e "display notification \"${message//\"/\\\"}\" with title \"Claude Code\""
elif command -v notify-send >/dev/null 2>&1; then
  notify-send "Claude Code" "$message"
fi

exit 0
```

## Install them [#install-them]

The scripts need `bash` and [jq](https://jqlang.org). The formatter also expects Prettier in the project's dependencies.

```bash title="Terminal"
chmod +x .claude/hooks/*.sh
claude
```

Run `/hooks` in Claude Code to see the registered hooks. To test the guard, ask Claude to add a line to `.env`. The edit is refused and Claude replies with the reason from the script. Commit `.claude/settings.json` and `.claude/hooks/` so your team gets the same hooks. Hooks run with your user's permissions, so read a hook script before you add it. [Claude Code hooks reference](https://code.claude.com/docs/en/hooks)

## How a hook runs [#how-a-hook-runs]

Each entry under an event has a `matcher` and the commands to run. The matcher is a regular expression on the tool name, so `Edit|Write` matches both file tools. Claude Code passes the event to the command as JSON on stdin. For file tools, `tool_input.file_path` holds the path, which is why every script starts with `jq`.

| Event | When it runs | Used here for |
| --- | --- | --- |
| `PreToolUse` | Before a tool call. Exit code 2 cancels the call and sends stderr to Claude. | `protect-files.sh` refuses edits to `.env` files, lockfiles and committed migrations. |
| `PostToolUse` | After a tool call succeeds. | `format.sh` runs Prettier on the file Claude wrote. |
| `Notification` | When Claude needs your permission or has waited for input. | `notify.sh` shows a desktop notification on macOS or Linux. |

`$CLAUDE_PROJECT_DIR` is the project root, so the commands work from any working directory. Keep the quotes around it for paths with spaces.

Other events include `UserPromptSubmit`, `Stop`, `SessionStart` and `SubagentStop`. Exit code 0 lets the action continue, and any other code except 2 shows an error to you without blocking.

## Variant: run tests before Claude stops [#variant-run-tests-before-claude-stops]

A `Stop` hook runs when Claude finishes a response. Exit code 2 sends Claude back to work with the message on stderr. Check `stop_hook_active` so a failing suite can't loop forever:

```bash title=".claude/hooks/require-tests.sh"
#!/usr/bin/env bash
set -euo pipefail

if [ "$(jq -r '.stop_hook_active')" = "true" ]; then exit 0; fi

cd "$CLAUDE_PROJECT_DIR"
if ! npm test --silent >/tmp/claude-tests.log 2>&1; then
  echo "Tests fail. Read /tmp/claude-tests.log and fix them before you finish." >&2
  exit 2
fi
```

Register it under `"Stop"` in `.claude/settings.json`, with no matcher, the same way as the notification hook.

## Variant: personal hooks [#variant-personal-hooks]

Put hooks that only you want, such as the notification, in `~/.claude/settings.json`. They run in every project. Project hooks and personal hooks both run when they match the same event.
