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
{
"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
{
"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
{
"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
{
"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
{
"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
{
"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
{
"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
{
"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.