Hooks run a shell command automatically on Claude Code events — for example, format code after every edit, or block edits to protected files. They’re defined in your settings.
1. Where hooks live
In .claude/settings.json (project) or ~/.claude/settings.json (global).
2. Example: format after every edit
Run Prettier whenever Claude edits or writes a file:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "npx prettier --write \"$CLAUDE_FILE_PATHS\"" }
]
}
]
}
}
3. Common events
- PreToolUse — before a tool runs (can block it).
- PostToolUse — after a tool runs (format, lint, test).
- UserPromptSubmit — when you send a message.
- Stop — when Claude finishes responding.
4. Apply it
Restart Claude Code so it picks up the new settings. Test by making an edit and watching the hook fire.
Tip: start with one harmless hook (formatting) before adding blocking PreToolUse hooks — a bad matcher can get in your way.