# 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.

Paste into: **Claude Code**, started in your project folder

## Prompt template

How the builder on the page fills in this template, so you can do the same:

- `{{key}}` becomes the text entered for that field, exactly as typed. Empty text, or text that is only whitespace (spaces, tabs or line breaks), counts as empty: a required field then becomes `[fill in: <label>]` with its label, and an optional field becomes nothing.
- For a choice field, `{{key}}` becomes the text of the chosen option, or of the default when none is chosen.
- Text between `{{#key}}` and `{{/key}}` stays only when that field is not empty; a choice field always counts as filled. The markers themselves are removed, and a marker that stands alone on its line takes its line break with it.
- Each value is inserted once, as it is: `{{…}}` inside a value stays as typed.

```text
Write a Claude Code hook that {{action}} after every {{event}}.
{{#command}}
Use this command: {{command}}
{{/command}}

Save it in {{location}}.

Requirements:
- Choose the hook event and matcher that fit, and narrow them so the hook runs only when it should. Explain the choice in one line.
- If the settings file already has hooks or other settings, add this one next to them. Do not replace or remove anything.
- Use the commands this project actually uses; look them up instead of guessing. If the hook needs a tool that is not installed, such as jq, tell me instead of installing it.
- Do not make the hook delete files, push, deploy or send data anywhere unless the description above asks for it.

Before you finish:
1. Show me the hook configuration, the file you put it in, and how to remove it.
2. Smoke-test the command on its own by piping a sample input into it, and show the result. This checks the command, not that the hook fires.
3. Have me confirm in /hooks that it appears under the intended event.
4. If it is safe, trigger one controlled matching event, such as a small change to a scratch file, and show that the hook ran. If you cannot, say that automatic triggering is unverified.

If what I asked for cannot be done reliably with a hook, say so and suggest another way.
```

## Fields

- `{{action}}` (required): What should happen.
- `{{event}}` (required): After what.
- `{{location}}`: Where to save it. Choose one:
  - .claude/settings.json, so the team gets it once it is committed (default)
  - .claude/settings.local.json, only for me in this project
  - ~/.claude/settings.json, for all my projects on this machine
- `{{command}}`: Command to run. Optional. Without it, Claude looks up the command this project uses.

This site doesn't run Claude or show model output. Results depend on your input and the model you use.

## 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](https://awesomeclaudeprompts.com/prompts/turn-task-into-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](https://awesomeclaudeprompts.com/prompts/capture-session-learnings/): 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](https://awesomeclaudeprompts.com/prompts/connect-tool-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](https://code.claude.com/docs/en/prompt-library) (Anthropic documentation)
- [Claude Code docs: Automate actions with hooks](https://code.claude.com/docs/en/hooks-guide) (Anthropic documentation)
- [Claude Code docs: Filter hooks with matchers](https://code.claude.com/docs/en/hooks-guide#filter-hooks-with-matchers) (Anthropic documentation)
- [Claude Code best practices: Set up hooks](https://code.claude.com/docs/en/best-practices#set-up-hooks) (Anthropic documentation)

---

Awesome Claude Prompts is an independent project. It is not affiliated with, endorsed by, or sponsored by Anthropic. Claude is a trademark of Anthropic, PBC.
