# 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! --- ## 快速使用 编辑 `~/.claude/settings.json`,添加 hooks: ```json { "hooks": { "PostToolUse": [{ "matcher": "Write|Edit", "command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || true" }] } } ``` --- ## 简介 Claude Code Hooks 是在工具使用前后自动运行的 Shell 命令。本合集提供 20+ 即用 Hook 配方:自动格式化、运行测试、安全扫描、Slack 通知等。复制到 settings.json 即可使用。适合想要强制代码质量和自动化重复任务的开发者。 --- ## 来源与感谢 > 基于 Claude Code 官方文档的社区维护合集。欢迎贡献你的 Hook 配方! --- Source: https://tokrepo.com/en/workflows/36711c75-fc63-49ad-9013-4a7cf5fae8a1 Author: Skill Factory