Skip to content

Best Claude Code Hook Examples to Copy in 2026

Practical Claude Code hook examples worth copying — auto-format, block destructive commands, notify on stop, log activity, and load context on session start.

MGMCSA Guru Team June 8, 2026 5 min read
A collection of Claude Code hook configurations for formatting, guardrails, and notifications

Hooks are the most copy-paste-friendly feature in Claude Code. Once you’ve seen a few good ones, you’ll spot a dozen places in your own workflow where a small automatic command would save you a manual step or prevent a mistake. This is a collection of the hooks worth stealing — formatting, guardrails, notifications, logging, and context loading — each with the full config block.

If you haven’t set up a hook before, read how hooks work first for the events and the settings structure. Everything here drops into the hooks section of your .claude/settings.json.

One note before the list: hooks run shell commands as you, automatically. The script-based examples use Unix tools like jq, which means WSL on Windows. Test any hook by hand before you trust it.

1. Auto-format after every edit

The hook everyone should have. After Claude edits or writes a file, run the formatter:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "npx prettier --write \"$CLAUDE_FILE_PATH\"" }
        ]
      }
    ]
  }
}

Replace prettier with black, gofmt, rustfmt, or your tool of choice. Formatting stops being the agent’s responsibility and becomes guaranteed.

2. Block destructive commands

A PreToolUse guardrail that refuses the commands you never want run unattended:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": ".claude/hooks/guard.sh" }
        ]
      }
    ]
  }
}
#!/usr/bin/env bash
cmd=$(cat | jq -r '.tool_input.command // empty')
if echo "$cmd" | grep -Eq 'rm -rf /|git push .*--force|DROP TABLE|mkfs|:\(\)\{'; then
  echo "Blocked a destructive command." >&2
  exit 2
fi
exit 0

3. Notify you when the agent finishes

A Stop hook that pings you when Claude is done, so you can step away during a long task:

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "echo 'Claude Code finished' | notify-send 'Claude Code' 2>/dev/null || true" }
        ]
      }
    ]
  }
}

On WSL with a desktop, notify-send shows a notification. On macOS you’d swap in osascript; on Windows, a PowerShell toast. The point is the same: reclaim the time you’d spend watching the terminal.

4. Log every command the agent runs

An audit trail of Bash commands, handy for unattended runs or after-the-fact review:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "jq -r '.tool_input.command' >> ~/.claude/command-log.txt" }
        ]
      }
    ]
  }
}

Everything the agent proposes to run gets appended to a log. Combine it with the guardrail in example 2 — log first, then block — for both a record and a safety net.

5. Load git context at session start

A SessionStart hook so Claude begins each session knowing the branch and recent history:

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "git status --short && git log --oneline -5" }
        ]
      }
    ]
  }
}

Cheap orientation. The agent starts informed instead of asking, or worse, assuming.

6. Run tests after changes to a critical path

Scope a PostToolUse hook to a sensitive directory and run its tests automatically when it changes:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": ".claude/hooks/test-on-change.sh" }
        ]
      }
    ]
  }
}
#!/usr/bin/env bash
path=$(cat | jq -r '.tool_input.file_path // empty')
case "$path" in
  *src/auth/*) npm test -- auth ;;
  *) : ;;
esac

This keeps the agent honest on the code you care about most without running the whole suite on every trivial edit.

Which to start with

Not all of these are equal. A sensible adoption order:

Where to begin

First Auto-format (example 1) — pure upside, no risk
Second Block destructive commands (example 2) — real protection
Third Session-start context (example 5) — better-oriented agent
As needed Notifications, logging, targeted tests

Hook adoption checklist

  • Add the auto-format hook and confirm it runs on an edit
  • Add a destructive-command guard and test it with a safe pattern
  • Run jq-based hooks in WSL on Windows
  • Commit shared hooks; keep personal ones in user settings
  • Keep hook commands fast so they don't stall the agent

Wrapping up

The best hooks are small and boring: format on edit, block the obvious disasters, notify when done, log what ran. Start with the formatter, add the guardrail, and grow your set as you notice manual steps worth automating. Each one is a few lines of JSON that makes Claude Code a little more trustworthy and a little less work to supervise.

For how hooks fit with skills, subagents, and MCP, see the power-user guide, and for the mechanics behind these configs, the hooks how-to.

Frequently asked questions

What's the most useful Claude Code hook to set up first?

An auto-format hook on PostToolUse matched to Edit and Write. It runs your formatter on every file Claude changes, so you never get an unformatted diff. It's low-risk, immediately useful, and a good way to learn the hook structure.

Can a hook stop Claude from running a command?

Yes. A PreToolUse hook that exits non-zero blocks the tool call before it runs. That's how the destructive-command guardrail in this article works — it inspects the Bash command and refuses dangerous patterns like rm -rf or force pushes.

Do these hook examples work on Windows?

The format hooks work anywhere. The ones using jq, grep, or shell scripts assume Unix tools, which are present in WSL but not on bare Windows. Run Claude Code in WSL for the script-based examples, or rewrite them for PowerShell.

Where do I paste these hook configs?

Into your settings JSON — .claude/settings.json for project hooks committed to the repo, or your user settings in ~/.claude/ for personal ones. Each example shows the full block to merge into the hooks section.

Will hooks slow Claude Code down?

A fast hook like a formatter adds negligible time. A slow command will delay the event it's attached to, so keep hook commands quick. If a hook does heavy work, consider running it asynchronously rather than blocking the agent.

Sources & further reading

Official vendor documentation referenced while writing this guide.

MG

MCSA Guru Team

IT & Systems Administration

We are working IT pros and system administrators who spend our days in Windows Server, Microsoft 365, and the wider Microsoft stack. MCSA Guru is where we write down the fixes and walkthroughs we wish we had found the first time.

MCSA Guru provides independent, educational IT guidance. Microsoft, Windows, Windows Server, Microsoft 365, Exchange, and Microsoft Teams are trademarks of Microsoft Corporation; Docker is a trademark of Docker, Inc. MCSA Guru is not affiliated with or endorsed by Microsoft or Docker. Always test changes in a safe environment before applying them in production.

Related guides

Fixing something right now?

Jump straight into the guide library or search for the exact error or task you are dealing with.