Hooks are the most copy-paste-friendly feature in Claude Code. Once you’ve seen a few good ones, you’ll spot a dozen places in your own workflow where a small automatic command would save you a manual step or prevent a mistake. This is a collection of the hooks worth stealing — formatting, guardrails, notifications, logging, and context loading — each with the full config block.
If you haven’t set up a hook before, read how hooks work first for the events and the settings structure. Everything here drops into the hooks section of your .claude/settings.json.
One note before the list: hooks run shell commands as you, automatically. The script-based examples use Unix tools like jq, which means WSL on Windows. Test any hook by hand before you trust it.
1. Auto-format after every edit
The hook everyone should have. After Claude edits or writes a file, run the formatter:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "npx prettier --write \"$CLAUDE_FILE_PATH\"" }
]
}
]
}
}
Replace prettier with black, gofmt, rustfmt, or your tool of choice. Formatting stops being the agent’s responsibility and becomes guaranteed.
2. Block destructive commands
A PreToolUse guardrail that refuses the commands you never want run unattended:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": ".claude/hooks/guard.sh" }
]
}
]
}
}
#!/usr/bin/env bash
cmd=$(cat | jq -r '.tool_input.command // empty')
if echo "$cmd" | grep -Eq 'rm -rf /|git push .*--force|DROP TABLE|mkfs|:\(\)\{'; then
echo "Blocked a destructive command." >&2
exit 2
fi
exit 0
3. Notify you when the agent finishes
A Stop hook that pings you when Claude is done, so you can step away during a long task:
{
"hooks": {
"Stop": [
{
"hooks": [
{ "type": "command", "command": "echo 'Claude Code finished' | notify-send 'Claude Code' 2>/dev/null || true" }
]
}
]
}
}
On WSL with a desktop, notify-send shows a notification. On macOS you’d swap in osascript; on Windows, a PowerShell toast. The point is the same: reclaim the time you’d spend watching the terminal.
4. Log every command the agent runs
An audit trail of Bash commands, handy for unattended runs or after-the-fact review:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "jq -r '.tool_input.command' >> ~/.claude/command-log.txt" }
]
}
]
}
}
Everything the agent proposes to run gets appended to a log. Combine it with the guardrail in example 2 — log first, then block — for both a record and a safety net.
5. Load git context at session start
A SessionStart hook so Claude begins each session knowing the branch and recent history:
{
"hooks": {
"SessionStart": [
{
"hooks": [
{ "type": "command", "command": "git status --short && git log --oneline -5" }
]
}
]
}
}
Cheap orientation. The agent starts informed instead of asking, or worse, assuming.
6. Run tests after changes to a critical path
Scope a PostToolUse hook to a sensitive directory and run its tests automatically when it changes:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": ".claude/hooks/test-on-change.sh" }
]
}
]
}
}
#!/usr/bin/env bash
path=$(cat | jq -r '.tool_input.file_path // empty')
case "$path" in
*src/auth/*) npm test -- auth ;;
*) : ;;
esac
This keeps the agent honest on the code you care about most without running the whole suite on every trivial edit.
Which to start with
Not all of these are equal. A sensible adoption order:
Where to begin
| First | Auto-format (example 1) — pure upside, no risk |
|---|---|
| Second | Block destructive commands (example 2) — real protection |
| Third | Session-start context (example 5) — better-oriented agent |
| As needed | Notifications, logging, targeted tests |
Hook adoption checklist
- Add the auto-format hook and confirm it runs on an edit
- Add a destructive-command guard and test it with a safe pattern
- Run jq-based hooks in WSL on Windows
- Commit shared hooks; keep personal ones in user settings
- Keep hook commands fast so they don't stall the agent
Wrapping up
The best hooks are small and boring: format on edit, block the obvious disasters, notify when done, log what ran. Start with the formatter, add the guardrail, and grow your set as you notice manual steps worth automating. Each one is a few lines of JSON that makes Claude Code a little more trustworthy and a little less work to supervise.
For how hooks fit with skills, subagents, and MCP, see the power-user guide, and for the mechanics behind these configs, the hooks how-to.