Add a hook for repeat behavior
Describe an action and when it should run; Claude Code writes the hook into your settings, tries it once and shows you how to check or remove it.
Task: Add a hook for repeat behavior · Other tasks
Fill in the details
Optional. Without it, Claude looks up the command this project uses.
Copy your prompt
This site doesn't run Claude or show model output. Results depend on your input and the model you use.
You've edited the prompt, so changes to the fields aren't applied.
Replace what you've entered for this task with the example? This can't be undone.
What you type is kept in this tab's session storage so a reload doesn't lose it. Use "Clear this task" to remove it. Browsers can restore session data when they reopen tabs, so closing a tab isn't a guaranteed way to erase it.
When to use this template
- You keep asking Claude Code to do the same thing after it acts, such as formatting a file after each edit, and you want it to happen without asking.
- The action is a fixed command that needs no judgment, so a deterministic hook fits better than an instruction Claude may or may not follow.
- You want the team to share the same automatic behavior through the project settings file in the repository.
When not to use it
- The behavior needs judgment each time, such as deciding whether a change deserves a test. Write it in CLAUDE.md or a skill instead, or ask about prompt-based hooks.
- You only need the action once or twice. Ask for it in the session; a hook runs on every match until you remove it.
- The command is slow, such as a full test suite. Running it after every edit slows every step; run it at the end of a task instead.
Why this structure
- The opening sentence names the action and the trigger separately, which maps onto how hooks are configured: an event, a matcher that filters it, and a command.
- The location choice decides who gets the hook. Project settings travel with the repository; local and user settings stay on your machine.
- The rule to add next to existing settings tells Claude to keep the hooks and permissions already in the file rather than replace the whole file.
- The checks are split on purpose. Piping a sample input only shows that the command works; seeing the hook under the right event in /hooks and one controlled matching event show that it fires. A working hook usually shows nothing in the conversation, so Claude says plainly when firing is unverified.
- The first check asks for removal steps next to the configuration, so you know how to undo the hook if it misbehaves on a later edit.
Example input (fictional)
- What should happen
runs Prettier on the edited file
- After what
edit to a .ts or .tsx file
- Where to save it
.claude/settings.json, so the team gets it once it is committed
- Command to run
npx prettier --write
Follow-ups to send Claude
- Also cover files that change through shell commands, not only through the Edit and Write tools. What is the cleanest way?
- Add a second hook that blocks edits to files under migrations/ and tells Claude why.
- List every hook configured for this project and explain in one line what each one does.
Common mistakes
- Matching only the Edit and Write tools when Claude can also change files with shell commands. A formatter hook then misses those files.
- Leaving the matcher empty. With no matcher, the hook runs for every tool call of that event type, not just the edits you had in mind.
- Putting a personal hook in the shared project settings. Use the local or user settings file for anything that only fits your machine.
Related templates
- Turn a repeated task into a skill: Describe a workflow you repeat and have Claude Code save it as a skill, so you or your team can run it later as a slash command.
- Capture what to remember for next time: At the end of a session, have Claude Code summarize the work and propose short CLAUDE.md lines, each with a reason, before anything is edited.
- Connect a tool to Claude Code with MCP: Name a tool and the data you need; Claude Code finds a trustworthy MCP server, shows the add command first, connects it and checks it with one read.
Sources
- Claude Code docs: Prompt library (Anthropic documentation)
- Claude Code docs: Automate actions with hooks (Anthropic documentation)
- Claude Code docs: Filter hooks with matchers (Anthropic documentation)
- Claude Code best practices: Set up hooks (Anthropic documentation)