{"name":"claude-code-hooks","title":"Claude Code hooks examples","description":"Claude Code hooks examples in a settings.json that block edits to secrets and lockfiles, format every file Claude writes and notify you when it waits.","category":"agents","updated":"2026-11-30","url":"https://sniptide.com/templates/claude-code-hooks","files":[{"name":".claude/settings.json","language":"json","content":"{\n  \"hooks\": {\n    \"PreToolUse\": [\n      {\n        \"matcher\": \"Edit|Write\",\n        \"hooks\": [\n          {\n            \"type\": \"command\",\n            \"command\": \"\\\"$CLAUDE_PROJECT_DIR\\\"/.claude/hooks/protect-files.sh\"\n          }\n        ]\n      }\n    ],\n    \"PostToolUse\": [\n      {\n        \"matcher\": \"Edit|Write\",\n        \"hooks\": [\n          {\n            \"type\": \"command\",\n            \"command\": \"\\\"$CLAUDE_PROJECT_DIR\\\"/.claude/hooks/format.sh\"\n          }\n        ]\n      }\n    ],\n    \"Notification\": [\n      {\n        \"hooks\": [\n          {\n            \"type\": \"command\",\n            \"command\": \"\\\"$CLAUDE_PROJECT_DIR\\\"/.claude/hooks/notify.sh\"\n          }\n        ]\n      }\n    ]\n  }\n}\n"},{"name":".claude/hooks/protect-files.sh","language":"bash","content":"#!/usr/bin/env bash\n# Blocks edits to secrets, lockfiles and merged migrations before Claude makes them.\n# Exit code 2 cancels the tool call and sends stderr back to Claude as the reason.\nset -euo pipefail\n\nfile=$(jq -r '.tool_input.file_path // empty')\nif [ -z \"$file\" ]; then exit 0; fi\n\ncase \"$file\" in\n  *.env.example) ;;\n  *.env | *.env.* | */.git/*)\n    echo \"Blocked: $file holds secrets or git internals. Ask the user to change it.\" >&2\n    exit 2\n    ;;\n  *package-lock.json | *pnpm-lock.yaml | *yarn.lock | *bun.lock)\n    echo \"Blocked: $file is generated. Change package.json and run the install command.\" >&2\n    exit 2\n    ;;\n  */migrations/*)\n    if git -C \"$CLAUDE_PROJECT_DIR\" ls-files --error-unmatch \"$file\" >/dev/null 2>&1; then\n      echo \"Blocked: $file is a committed migration. Add a new migration instead.\" >&2\n      exit 2\n    fi\n    ;;\nesac\n\nexit 0\n"},{"name":".claude/hooks/format.sh","language":"bash","content":"#!/usr/bin/env bash\n# Formats each file Claude writes, so diffs stay clean without asking Claude to run Prettier.\nset -euo pipefail\n\nfile=$(jq -r '.tool_input.file_path // empty')\nif [ -z \"$file\" ] || [ ! -f \"$file\" ]; then exit 0; fi\n\ncase \"$file\" in\n  *.ts | *.tsx | *.js | *.jsx | *.json | *.css | *.md | *.mdx | *.yml | *.yaml)\n    cd \"$CLAUDE_PROJECT_DIR\"\n    npx --no-install prettier --write --log-level warn \"$file\" || true\n    ;;\nesac\n\nexit 0\n"},{"name":".claude/hooks/notify.sh","language":"bash","content":"#!/usr/bin/env bash\n# Shows a desktop notification when Claude is waiting for you.\nset -euo pipefail\n\nmessage=$(jq -r '.message // \"Claude Code needs your attention\"')\n\nif command -v osascript >/dev/null 2>&1; then\n  osascript -e \"display notification \\\"${message//\\\"/\\\\\\\"}\\\" with title \\\"Claude Code\\\"\"\nelif command -v notify-send >/dev/null 2>&1; then\n  notify-send \"Claude Code\" \"$message\"\nfi\n\nexit 0\n"}]}