# Claude Code Hooks — Custom Automation Recipes > Collection of ready-to-use Claude Code hook recipes for automating code formatting, testing, notifications, and security checks. Copy-paste into settings.json. Community-maintained. ## Install Save the content below to `.claude/skills/` or append to your `CLAUDE.md`: ## Quick Use Open Claude Code settings: `claude config` or edit `~/.claude/settings.json` Add hooks to the `hooks` section: ```json { "hooks": { "PostToolUse": [ { "matcher": "Write|Edit", "command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || true" } ] } } ``` This auto-formats every file Claude Code writes or edits. --- ## Intro Claude Code Hooks are shell commands that run automatically before or after specific tool uses — like git hooks but for your AI agent. This collection provides 20+ ready-to-use hook recipes for auto-formatting, test running, notifications, security scanning, and workflow automation. Copy any recipe into your `settings.json` and Claude Code gains new automated behaviors. Best for developers who want to enforce code quality and automate repetitive tasks around AI-generated code. Works with: Claude Code. Setup time: under 1 minute per hook. --- ## Hook Types | Event | When It Fires | |-------|--------------| | `PreToolUse` | Before a tool runs (can block) | | `PostToolUse` | After a tool completes | | `Notification` | When Claude sends a notification | | `Stop` | When Claude finishes a response | ## Popular Recipes ### Auto-Format on Write ```json { "hooks": { "PostToolUse": [ { "matcher": "Write|Edit", "command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null || true" } ] } } ``` ### Run Tests After Code Changes ```json { "hooks": { "PostToolUse": [ { "matcher": "Write|Edit", "command": "if echo $CLAUDE_FILE_PATH | grep -q '\\.test\\.'; then npx jest $CLAUDE_FILE_PATH --no-coverage 2>&1 | tail -5; fi" } ] } } ``` ### Lint Python Files ```json { "hooks": { "PostToolUse": [ { "matcher": "Write|Edit", "command": "if echo $CLAUDE_FILE_PATH | grep -q '\\.py$'; then ruff check --fix $CLAUDE_FILE_PATH 2>/dev/null; fi" } ] } } ``` ### Block Sensitive File Edits ```json { "hooks": { "PreToolUse": [ { "matcher": "Write|Edit", "command": "if echo $CLAUDE_FILE_PATH | grep -qE '\\.(env|pem|key)$'; then echo 'BLOCKED: Cannot edit sensitive files' >&2; exit 1; fi" } ] } } ``` ### Slack Notification on Task Complete ```json { "hooks": { "Stop": [ { "command": "curl -s -X POST $SLACK_WEBHOOK -d '{\"text\":\"Claude Code finished a task in '$(basename $(pwd))'\"}' 2>/dev/null || true" } ] } } ``` ### Auto-Commit After Changes ```json { "hooks": { "Stop": [ { "command": "if [ -n \"$(git diff --name-only)\" ]; then git add -A && git commit -m 'wip: Claude Code auto-save' --no-verify 2>/dev/null; fi" } ] } } ``` ### Type Check TypeScript ```json { "hooks": { "PostToolUse": [ { "matcher": "Write|Edit", "command": "if echo $CLAUDE_FILE_PATH | grep -q '\\.tsx\\?$'; then npx tsc --noEmit 2>&1 | head -20; fi" } ] } } ``` ### Security Scan ```json { "hooks": { "PreToolUse": [ { "matcher": "Bash", "command": "if echo $CLAUDE_TOOL_INPUT | grep -qiE 'rm -rf|curl.*\\|.*sh|wget.*\\|.*bash'; then echo 'BLOCKED: Potentially dangerous command' >&2; exit 1; fi" } ] } } ``` ### Key Stats - 20+ ready-to-use recipes - 4 hook event types - Pattern matching with regex - Environment variables available - Community-maintained ### FAQ **Q: What are Claude Code Hooks?** A: Shell commands that run automatically before or after Claude Code tool uses — like auto-formatting after every file write, or blocking edits to sensitive files. **Q: Can hooks block Claude Code actions?** A: Yes, PreToolUse hooks can return exit code 1 to block the action. **Q: Do hooks slow down Claude Code?** A: Hooks run synchronously, so keep them fast (<1 second). Use background processes for slow operations. --- ## Source & Thanks > Community-maintained collection based on [Claude Code documentation](https://docs.anthropic.com/en/docs/claude-code). > > Contributions welcome — share your hook recipes! --- ## Quick Use Edit `~/.claude/settings.json` and add hooks: ```json { "hooks": { "PostToolUse": [{ "matcher": "Write|Edit", "command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || true" }] } } ``` --- ## Intro Claude Code Hooks are shell commands that run automatically before or after tool use. This collection offers 20+ ready-to-use hook recipes: auto-formatting, running tests, security scanning, Slack notifications, and more. Copy them into settings.json and go. Ideal for developers who want to enforce code quality and automate repetitive tasks. --- ## Source & Thanks > Community-maintained collection based on the official Claude Code docs. Contributions welcome — share your hook recipes! --- Source: https://tokrepo.com/en/workflows/claude-code-hooks-custom-automation-recipes-36711c75 Author: Skill Factory