What Are Hooks?
Hooks are JSON configuration files (.json) stored in .claude/hooks/ that define:
- Trigger events - When the hook should run (before/after tool use)
- Matchers - Which tools trigger the hook (Edit, Bash, Write, etc.)
- Actions - What to execute (commands, scripts, notifications)
- Conditions - Optional filters for selective triggering
Hooks run automatically in the background. Claude Code executes them without requiring user interaction.
Hook Types
Claude Code supports two main hook types:- PreToolUse
- PostToolUse
PreToolUse hooks run BEFORE Claude executes a tool.Use cases:
- Validate operations before execution
- Create backups before edits
- Check permissions before dangerous operations
- Block operations that violate policies
Hook Structure
Hooks are configured in settings files (.claude/settings.json or .claude/settings.local.json):
Hook Fields
Tool Matchers
Hooks can match specific Claude Code tools:Use specific matchers to avoid unnecessary hook execution. Matching
Edit won’t trigger on Bash commands.Hook Categories
- Git Automation
- Code Quality
- Build Automation
- Security
- Notifications
Automate git operations:Example:
- git-commit-formatter - Format commit messages automatically
- prevent-force-push - Block dangerous git operations
- auto-add-changes - Stage changes automatically
- commit-msg-validator - Enforce commit message standards
Real-World Examples
Example 1: Build on Change Hook
File: Component downloaded to settings file- Triggers after every file edit
- Detects build tool (npm, make, cargo, etc.)
- Runs appropriate build command
- Silent failures (|| true) prevent interruptions
Example 2: Git Commit Validator Hook
File: Component configuration- Runs before bash commands
- Checks if command is
git commit - Validates conventional commit format
- Blocks commit if format is invalid
Example 3: Python Script Hook
Some hooks reference external Python scripts: Hook Configuration:.claude/hooks/custom-formatter.py
The CLI automatically downloads supporting scripts when installing hooks.
Installing Hooks
Single Hook
- User settings (
~/.claude/settings.json) - All projects - Project settings (
.claude/settings.json) - Shared with team - Local settings (
.claude/settings.local.json) - Personal only
Multiple Hooks
With Category Prefix
Hooks merge with existing settings. Multiple hooks can trigger on the same tool.
Hook Execution Context
Hooks have access to context variables:Environment Variables
Example usage:
Hook Best Practices
1. Silent Failures
Prevent hooks from interrupting workflow:|| true ensures the hook never fails.
2. Conditional Execution
Only run when necessary:3. Fast Operations
Hooks should be quick:4. Idempotent Actions
Hooks should be safe to run multiple times:5. Error Handling
Handle errors gracefully:Hook Debugging
Debug hooks by adding logging:Disabling Hooks
Temporarily disable hooks:Per-Session
Set environment variable:Permanent
Remove hook configuration from settings file or comment out:Hook vs Command
Hooks automate repetitive tasks. Commands handle complex workflows that need user input.
Advanced Hook Patterns
Chained Hooks
Multiple hooks on same matcher:Conditional Hooks
Use shell conditionals:Hook with External Scripts
Call custom scripts:Common Hook Recipes
Auto-format Python
Run Tests on Change
Notify on Bash Execution
Next Steps
Browse Hooks
Explore 45+ available hooks
Create Custom Hooks
Build your own automation
Settings
Configure hook behavior
Commands
Learn about slash commands