Claude Code Hooks Tutorial: 4 Hooks Worth Setting Up
This Claude Code hooks tutorial covers the four hooks I would set up first: a ping when Claude needs you, automatic formatting, a block on edits to secret files, and a reminder after the conversation gets compacted. Each one is a few lines of settings you can copy.
Claude Code hooks are shell commands that run automatically at set points in a session, such as before a tool runs, after a file is edited or when Claude needs your input. Unlike instructions in CLAUDE.md, a hook always runs, so it can enforce rules Claude might otherwise skip.
All examples follow Anthropic's hooks guide, which is the reference for every event and field.

What hooks are, and why they beat instructions
Everything you write in CLAUDE.md is advice. Claude reads it and usually follows it, but nothing guarantees it. A hook is different: Claude Code itself runs the command at the right moment, every time, whatever the model decides.
That makes hooks the right tool for three kinds of job:
- Things that must always happen: format every edited file, log every command.
- Things that must never happen: edits to
.env, deleting a folder, pushing to main. - Things you want to know about: Claude is waiting for approval, a task finished.
If it is a preference, put it in your CLAUDE.md. If it is a rule, make it a hook.
How a hook works
- 1
Event
Claude is about to edit a file, finishes a turn or needs input
- 2
Matcher
checks which tool or event it is, such as
Edit|Write - 3
Command
your script runs and reads the details as JSON
- 4
Result
exit 0 lets it continue; exit 2 blocks with a reason
The events you will use most:
| Event | Fires | Typical use |
|---|---|---|
PreToolUse | Before a tool runs; can block it | Protect files, block risky commands |
PostToolUse | After a tool succeeds | Format code, run a quick check |
Notification | When Claude needs your input or approval | Desktop alert |
Stop | When Claude finishes responding | Summary, sound, final checks |
SessionStart | When a session starts, resumes or compacts | Load context or reminders |
UserPromptSubmit | When you send a prompt, before Claude sees it | Add context, block secrets |
There are many more events in the reference, from subagents to model switches, but these six cover most setups.
Where hooks go
Hooks live in a hooks block inside a settings file. The file decides who gets the hook:
| File | Applies to | Shared |
|---|---|---|
~/.claude/settings.json | All your projects | No |
.claude/settings.json | This project | Yes, commit it to the repo |
.claude/settings.local.json | This project | No, kept out of git |
Type /hooks inside Claude Code to see every hook that is active, grouped by event. You can also just describe the hook you want and ask Claude to write it.
Hook 1: Get a ping when Claude needs you
The most useful hook for anyone who walks away while Claude works. Add this to ~/.claude/settings.json on a Mac:
{
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"
}
]
}
]
}
}On Windows, swap the command for a PowerShell message box (it opens a dialog rather than a corner notification):
powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Claude Code needs your attention', 'Claude Code')"Test it by asking Claude to do something that needs your permission, then switching to another window.
Hook 2: Format every file Claude edits
A PostToolUse hook with an Edit|Write matcher runs only after file edits. This one passes the edited file to Prettier. Put it in the project's .claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
]
}
}It needs jq installed. When it works you see nothing in the conversation; the file is simply formatted.
Hook 3: Block edits to protected files
This is the hook that turns "please never touch .env" into a rule. Save a script at .claude/hooks/protect-files.sh:
#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
FILE_PATH="${FILE_PATH//\\//}"
PROTECTED_PATTERNS=(".env" "package-lock.json" ".git/")
for pattern in "${PROTECTED_PATTERNS[@]}"; do
if [[ "$FILE_PATH" == *"$pattern"* ]]; then
echo "Blocked: $FILE_PATH matches protected pattern '$pattern'" >&2
exit 2
fi
done
exit 0Then register it as a PreToolUse hook in .claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh"
}
]
}
]
}
}Exit code 2 blocks the edit and hands Claude the message, so it can find another way. On Mac and Linux, make the script executable with chmod +x first.
Hook 4: Re-inject context after compaction
Long sessions get compacted: Claude summarises the conversation to free space, and details can get lost. A SessionStart hook with a compact matcher puts your key reminders back every time:
{
"hooks": {
"SessionStart": [
{
"matcher": "compact",
"hooks": [
{
"type": "command",
"command": "echo 'Reminder: use pnpm, not npm. Run the tests before committing. Current focus: the checkout page.'"
}
]
}
]
}
}Swap the echo for any command with useful output, such as git log --oneline -5 to show the latest commits.
Hooks for business folders, not only code
Hooks are not only for developers. In a Claude Code folder you use for business work, a few ideas:
- A
Notificationhook so you know the moment a long research job needs you. - A
PreToolUsehook that blocks edits to your price list, so only you can change prices. - A
Stophook that saves a one-line log of what was produced in each session.
Stay safe with hooks
Hooks run shell commands with your permissions, automatically. Read every hook before you add it, never paste one from a comment or a stranger's repo without understanding it, and keep shared project hooks in git so changes are visible. A hook that runs on every edit should also run fast.
Claude Code hooks tutorial: FAQ
Does Claude Code support hooks?
Claude Code supports hooks natively. You add them in a hooks block in a settings file, and /hooks shows everything that is active.
What are the most useful hooks in Claude Code?
The most useful hooks for most people are a notification when Claude needs input, auto-formatting after edits, a block on edits to sensitive files, and a reminder injected after compaction.
What is the difference between hooks and skills?
A skill is a set of instructions Claude chooses to follow for a job. A hook is a command Claude Code always runs at a set moment. Use skills for procedures and hooks for rules. I cover skills in how to build a Claude skill.
Do hooks work on Windows?
Hooks work on Windows. Use PowerShell commands, or run bash scripts through Git Bash or WSL, and test each command in your terminal before putting it in a hook.
Get a workspace that is already set up
The AI Operator Kit is a ready Claude Code workspace with 18 commands for business work and safety rules built in: drafts only, nothing sent or published without you.