Hooks
Run shell commands automatically on Claude Code events.
Run shell commands automatically on Claude Code events.
What it is
Hooks are shell commands Claude Code runs automatically on specific events — PreToolUse, PostToolUse, SessionStart, UserPromptSubmit, and more. Configure once and they fire in the background without Claude having to remember. Perfect for linters, tests, notifications, backups.
Install / Setup
Hooks are configured in settings.json — NOT in CLAUDE.md. CLAUDE.md holds rules, hooks are real harness config.
Option 1 — Terminal:
# Global hooks
nano ~/.claude/settings.json
# Project hooks
nano .claude/settings.json
Example settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "npm run lint --silent" }
]
}
]
}
}
Option 2 — Prompt Claude Code:
Set up a hook: after every Edit/Write on .ts or .tsx files, run "npm run lint". Write it straight into ~/.claude/settings.json (or cleanly merge with what's already there). Then show me how to test it.
Anthropic's update-config skill helps with hook setup — the harness runs hooks, not Claude itself.
Event types (selection)
- PreToolUse — Before any tool call (Bash, Edit, Write…)
- PostToolUse — After a tool call
- SessionStart — When a Claude Code session starts
- UserPromptSubmit — When you send a prompt
- Stop — When Claude finishes a turn
Liking this?
Inside the community I show you how I run all of this day-to-day — live sessions, direct feedback on your setup, and my full configs.
Use cases
- Auto-lint on edit — PostToolUse hook on Edit/Write, runs the linter on the changed file.
- Tests on save — Changes to
src/**/*.test.tstrigger the test run immediately. - Session-start check — On start: verify the dev server is up, show git status, show the last commit.
- Commit guard — PreToolUse on Bash calls containing
git commit: enforces linter/tests first. - Telegram notification — PostToolUse: ping your phone on deploy commands.
Pro tip
Hooks can block tool calls (non-zero exit → call refused). Use that deliberately: a pre-commit hook that rejects commits on red tests is gold. But: over-eager hooks get annoying fast — if the linter blocks every edit for 5 seconds, you'll hate your setup within a day.
🚀 Inside the community we build complete automation systems and workflows that actually run your day. → Join the Claude Code Mastery Community
Updated regularly — follow @vine.codes for more.
Want more?