Set Up Hooks to Run Commands Automatically Before or After Tool Calls
Hooks let you attach shell commands to Claude's tool calls — they fire automatically before or after Claude reads a file, writes code, or runs a command. No prompting required.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "vendor/bin/pint --dirty --format agent"
}
]
}
}
Drop that in your .claude/settings.json and every time Claude edits or writes a PHP file, Pint runs automatically to fix formatting. Claude sees the hook output and adjusts if needed.
You can hook into different events:
- PreToolUse — runs before a tool executes (can block it by returning a non-zero exit code)
- PostToolUse — runs after a tool completes
- Notification — runs when Claude sends a notification
The matcher field filters which tool triggers the hook. Match specific tools like Write, Edit, Bash, or use a regex pattern to match several.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx eslint --fix $CLAUDE_FILE_PATH"
},
{
"matcher": "Bash",
"command": "echo \"$(date): $CLAUDE_TOOL_INPUT\" >> ~/.claude-commands.log"
}
],
"PreToolUse": [
{
"matcher": "Bash",
"command": "echo 'Review this command carefully' >&2"
}
]
}
}
Hooks run in your shell with access to environment variables like $CLAUDE_FILE_PATH and $CLAUDE_TOOL_INPUT, so you can build context-aware automation around Claude's actions.
Hooks turn Claude Code from an assistant you instruct into a workflow that enforces your standards automatically.
via Claude Code
Log in to leave a comment.
When Claude writes error messages, button labels, validation text, or onboarding flows, it defaults to generic developer-speak. Add a "Users" section to your CLAUDE.md describing who your actual users are — their technical level, industry jargon, and what they care about — so Claude writes copy that makes sense to THEM, not to developers.
Use the --agent flag with custom markdown files in .claude/agents/ to launch purpose-built Claude sessions with restricted tools and scoped system prompts.
Every project has traps — the billing module that silently fails if you forget to queue the job, the legacy table with column names that don't match the model, the config value that must be set before tests run. Document these gotchas in your CLAUDE.md so Claude avoids the same mistakes your team spent days debugging.