Skip to content
Sniptide

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

Files

.claude/settings.jsonRaw
{
  "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.shRaw
#!/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.shRaw
#!/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.shRaw
#!/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

Save with your agent

Run this command to add Sniptide to Claude Code in the current project. Open /mcp in Claude Code and sign in to Sniptide. Once the server shows as connected, use the prompt below to save a private copy in your library.

Terminal
claude mcp add --scope local --transport http sniptide https://app.sniptide.com/api/mcp
Prompt
Read https://sniptide.com/templates/claude-code-hooks.json and use create_snippet to save its files as a private Sniptide snippet titled "Claude Code hooks examples". Keep the file names and contents from the JSON.

Install them

The scripts need bash and jq. The formatter also expects Prettier in the project's dependencies.

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

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

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:

.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

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.

All templates
  • Agent filesCLAUDE.md template

    A CLAUDE.md template for Claude Code with commands, layout, code style and rules, plus monorepo and AGENTS.md variants you can copy.

  • Agent filesClaude Code skill template

    A Claude Code skill template with a SKILL.md example and a supporting file, plus the frontmatter fields, scopes and two variants explained.

  • Agent filesClaude Code subagent template

    Two Claude Code subagent examples, a code reviewer and a test runner, with the frontmatter fields, tool limits, model choice and scopes explained.

Pick where your library lives.

Use sniptide.com

Save your first pattern in the app, then connect Claude Code, Cursor or Codex with one sign-in in the browser.

Run it on your own server

One image and a database give you the app, the API and the MCP endpoint. Sniptide is open source under AGPL-3.0.

$docker compose up -d