Claude Code guide in three levels, Beginner to Advanced: hands-on lessons with checked exercises, every volatile fact cited to its source. Independent community guide.
Hooks for Intermediate lesson 5
Three teaching hooks for the Intermediate lesson "Enforce a rule with a hook", and the settings that register them. Each script's header says what it does, when it runs, its side effects, platforms, how to remove it and its test line.
| Script | Event and matcher | What it does |
|---|---|---|
dot-claude/hooks/protect-files.sh |
PreToolUse, Edit|Write |
Blocks edits to .env, package-lock.json and .git/, with exit code 2. The hooks guide's script, unchanged |
dot-claude/hooks/format-after-edit.sh |
PostToolUse, Edit|Write |
Removes trailing spaces and tabs and leaves one final newline in the source file Claude just changed. Only common source-code extensions, such as .js, .py and .sh: Markdown, patches and data files stay as they are |
dot-claude/hooks/desktop-notify.sh |
Notification, every type |
Shows a desktop notification for every Claude Code notification, such as a permission prompt that has waited |
Use them
From your project's root:
mkdir -p .claude/hooks
cp <path-to-this-folder>/dot-claude/hooks/protect-files.sh <path-to-this-folder>/dot-claude/hooks/format-after-edit.sh .claude/hooks/
chmod +x .claude/hooks/*.sh
Then merge settings.snippet.json into your project's .claude/settings.json. If the file already has a hooks or permissions key, add these entries beside the existing ones instead of replacing them.
desktop-notify.sh is about you, not the project, so it goes in your user settings, where the hooks guide registers its notification examples. Copy it to ~/.claude/hooks/, make it executable, and add this to ~/.claude/settings.json, beside any keys already there. It uses shell form, so the shell expands ~:
"hooks": {
"Notification": [
{ "matcher": "", "hooks": [ { "type": "command", "command": "~/.claude/hooks/desktop-notify.sh" } ] }
]
}
settings.snippet.json:
- What: registers
protect-files.shandformat-after-edit.shin exec form, so the${CLAUDE_PROJECT_DIR}path needs no quoting, with"onFailure": "block"onprotect-files.sh, so a protection script that can't start, crashes or times out blocks the edit instead of letting it through (Block the action when a hook fails). It also adds aRead(.env)deny rule, which also stops Claude's Edit and Write tools from changing a.envfile (Configure permissions). - When: Claude Code reads it when a session starts in the project, and reloads it when the file changes (Settings).
- Side effects: none of its own; the scripts' headers list theirs.
- Platforms: macOS, Linux and WSL2. The scripts need bash and jq, and
format-after-edit.shalso needs python3. - Remove: delete the entries from
.claude/settings.json.
Hooks in a repository's .claude/settings.json run with your user's rights: in an interactive session once you trust the folder, and in a claude -p run without asking (Hooks reference). Read every script before you register it.
Smoke test, without Claude: bash test.sh.
The scripts: Tested in Claude Code v2.1.285 (stable) on macOS, 2026-10-06settings.snippet.json: Not yet tested in a session at this edition's version
Sources: Automate actions with hooks, Hooks reference, Configure permissions, Settings