# Claude Code hooks examples for notifications, auto-formatting and protected files

Canonical: https://cwyn.com/pages/claude-code-guide-hooks-examples.html
Last updated: 2026-11-23T07:34:54-05:00

Claude Code setup guide

Use a `Notification` hook for a desktop alert, `PostToolUse` to format a file after an edit, and `PreToolUse` to block edits to a protected file. Put each event in the `hooks` object in your Claude Code settings. Run `/hooks` to check registration and open the transcript with Ctrl+O to see what happened when the hook ran.

Last updated November 23, 2026

By CWYN · Published Nov 23, 2026 · Hook behavior as of Nov 22, 2026 · [Anthropic’s Claude Code hooks guide](https://code.claude.com/docs/en/hooks-guide)

## 1. Where hooks live

Use `~/.claude/settings.json` for a hook you want across your projects. Use `.claude/settings.json` in the project root for a hook that belongs to that project. To choose a file when both exist, see [how Claude Code settings.json precedence works](https://cwyn.com/pages/claude-code-guide-settings-json-precedence.html).

Each key inside the outer `hooks` object names an event. Under that event, a `matcher` selects which occurrences count. The inner `hooks` array holds the commands to run when the matcher applies. You can also ask Claude to write the hook for you.

Each JSON block below shows a complete settings file for one event. To use both project examples, keep a single `hooks` object and add `PreToolUse` alongside `PostToolUse` as a sibling key. That preserves the formatter when you add the file guard.

## 2. Notify when Claude needs input

For a macOS desktop alert, put this `Notification` hook in `~/.claude/settings.json`. An empty matcher covers every notification type.

```
{"hooks":{"Notification":[{"matcher":"","hooks":[{"type":"command","command":"osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"}]}]}}
```

Try it. Open `/hooks` and look under `Notification`. Leave a permission prompt unanswered for about six seconds. Look for a desktop notification titled “Claude Code” with the message “Claude Code needs your attention.”

For alerts only when a permission prompt is waiting, set the matcher to `permission_prompt`. For a reminder after Claude finishes, use `idle_prompt`. It fires when about 60 seconds have passed since Claude finished and you have yet to type. For repeated permission requests, see [why Claude Code still asks after you allow a command](https://cwyn.com/pages/claude-code-guide-permission-allowlist-prompts.html).

If the Mac stays quiet, run `osascript -e 'display notification "test"'` once. Then allow notifications for Script Editor in System Settings > Notifications.

On Linux, replace the value of `command` with the line below. It needs a notification daemon. On Debian or Ubuntu, install `libnotify-bin` for `notify-send`.

```
notify-send 'Claude Code' 'Claude Code needs your attention'
```

## 3. Auto-format a file after an edit

Put this `PostToolUse` hook in the project’s `.claude/settings.json`. After `Edit` or `Write`, `jq` reads the edited file’s path from the hook input and passes it to Prettier. Install `jq` with `brew install jq` or `apt-get install jq`.

```
{"hooks":{"PostToolUse":[{"matcher":"Edit|Write","hooks":[{"type":"command","command":"f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\""}]}]}}
```

Try it. Find the hook under `PostToolUse` in `/hooks`. Ask Claude to use `Write` to put deliberately uneven spacing in a scratch file that Prettier formats. Open the file after the write. The spacing should follow the project’s Prettier rules. A successful run is silent.

If a Bash command rewrites the file, use a `FileChanged` hook to cover that change. The matcher above covers the `Edit` and `Write` tools.

## 4. Block edits to protected files

Save this script as `.claude/hooks/protect-files.sh` in the project. It reads the proposed path with `jq` and checks for each protected string. A match sends the reason to stderr and exits with code 2, which blocks the action and gives Claude the message.

```
#!/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 0
```

Hook scripts must be executable on macOS and Linux. Run this command from the project root.

```
chmod +x .claude/hooks/protect-files.sh
```

Register the script under `PreToolUse` in `.claude/settings.json` so the check runs before an `Edit` or `Write`.

```
{"hooks":{"PreToolUse":[{"matcher":"Edit|Write","hooks":[{"type":"command","command":"\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh"}]}]}}
```

Try it. In a scratch project with this hook, check `/hooks` for `PreToolUse`. Ask Claude to use `Write` to create an empty `.env.example`. Its name contains `.env`, so the write should stop. Check that the file is absent and press Ctrl+O to see the blocked path and matched pattern.

## 5. Hooks vs skills vs subagents

| Extension | When to use it |
| --- | --- |
| Hook | A step should run at a particular event, such as formatting after every matching edit. For a judgment call at that point, use a prompt-based or agent-based hook. |
| Skill | Claude needs extra instructions or commands for a task, such as your project’s formatting instructions. |
| Subagent | A task should run in an isolated context, such as a separate review of the edit. |

To share extensions together, package them in a plugin.

## 6. Check and debug a hook

Type `/hooks` to check registration. To see what happened when the event ran, press Ctrl+O to open the transcript. A blocking error shows the hook’s message, for example `Blocked: rm commands are not allowed`. A non-blocking error shows a hook error notice containing `Failed with non-blocking status code:`.

For more detail, start Claude with the first command below. Run the second in another terminal to follow the log as you trigger the hook. In an existing session, run `/debug`.

```
claude --debug-file /tmp/claude.log
tail -f /tmp/claude.log
```

If a hook returns JSON, put `permissionDecision` or `additionalContext` inside `hookSpecificOutput`. Claude silently ignores those fields at the top level. Look for `Hook JSON output had unrecognized keys` in the output of `claude --debug`.

Still missing a notification, format or block? [Troubleshoot a Claude Code hook that isn’t firing](https://cwyn.com/pages/claude-code-guide-hooks-not-firing.html).

## 7. Claude Code hooks FAQ

### Where do I put Claude Code hooks?

Put personal hooks in `~/.claude/settings.json`. For a project hook, use `.claude/settings.json` at the project root. Add each event as a key in the same `hooks` object.

### How do I block a command?

Match the tool in a `PreToolUse` hook. When the script detects an action to block, write the reason to stderr and use `exit 2`. Claude receives that reason. The protected-file example applies this check to `Edit` and `Write`.

### Why does my hook do nothing?

Find it under its event in `/hooks`, then trigger an action its matcher covers. For the formatter, inspect the file for the expected change. For an error, open the transcript with Ctrl+O and read the hook’s message.

### Do I need jq?

The formatter and protected-file script use `jq` to extract the file path. The desktop notification example can run without it.

## Next steps

- [All 34 Claude Code guides](https://cwyn.com/guides/claude-code/)
- [Claude Code setup walkthrough](https://cwyn.com/setup/claude-code/)
- [Free setup file checker](https://cwyn.com/score/setup-file-checker/)
- [Claude Code Complete Setup Pack ($12.95)](https://cwyn.com/pages/pack-claude-code-complete-setup-pack.html)

### Check your project instructions

[Check your CLAUDE.md with the free setup file checker](https://cwyn.com/score/setup-file-checker/)

## Email me the CLAUDE.md + settings starter

Get the editable CLAUDE.md template and a project setting you can merge into .claude/settings.json.

Email address

Email me this file. Also send up to 4 short follow-up emails with tips and one related CWYN product. Unsubscribe any time. [Privacy policy](https://cwyn.com/pages/privacy.html)

### Set up a Claude Code project

Get the [Claude Code Complete Setup Pack ($12.95)](https://cwyn.com/pages/pack-claude-code-complete-setup-pack.html).

Claude Code Complete Setup Pack $12.95

Set up project instructions, permission rules, hooks, a reviewer skill and subagent, and MCP connections for Claude Code.
