Hooks Recipes
Hooks is a feature that automatically executes shell commands on specific Claude Code events (before/after tool execution, notifications, etc.). Configured via the
hooksfield in.claude/settings.json.
Code Quality
Auto Formatting
Automatically run Prettier after file edits — using PostToolUse(Edit) event
SafetyBlock Dangerous Commands
Block destructive commands like rm -rf, DROP TABLE, force-push via PreToolUse
ProductivityCompletion Notification
Desktop notification on task completion — check results after working on something else
Code QualityAuto Test
Automatically run tests when test files change — using PostToolUse(Write)
ProductivityWork Log
Automatically record file modification history to a log file — useful for audit trails and recovery
Basic Hooks Configuration Structure
json
// .claude/settings.json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "prettier --write $CLAUDE_FILE_PATH" }
]
}
]
}
}Event types: PreToolUse, PostToolUse, Notification, Stop
| Field | Value |
|---|---|
| Source URL | https://docs.anthropic.com/en/docs/claude-code/hooks |
| License | Anthropic Official Documentation (reference only) |
| Translation Date | 2026-04-12 |
| Author | Claude-Code-Study Project |
Hooks automatically execute shell commands, so misconfiguration can have destructive consequences. Always test in a safe environment first.