Free generator

Claude Code Hooks Generator

Pick ready-made recipes (format on save, block rm -rf, protect .env, desktop notifications) or build your own hook for any of the 33 events, and copy the JSON into settings.json.

Pick as many as you like. Hooks with the same event and matcher are grouped.

.claude/settings.json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
          }
        ]
      }
    ],
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "f=$(jq -r '.tool_input.file_path // empty'); case \"$f\" in *.env|*.env.*|*package-lock.json|*pnpm-lock.yaml|*yarn.lock|*/.git/*) echo \"Blocked: $f is protected\" >&2; exit 2 ;; esac; exit 0"
          }
        ]
      }
    ],
    "Notification": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"
          }
        ]
      }
    ]
  }
}
Merge the hooks key into your settings file, or ~/.claude/settings.json for every project. The recipes need jq. Run /hooks in Claude Code to check they loaded.

How the recipes work

Claude Code sends each hook a JSON object on stdin with the session, the event and, for tool events, tool_name and tool_input. The recipes read fields with jq, act, and exit. Exit code 0 means carry on; exit code 2 blocks the action on events that support it and sends your stderr to Claude.

For longer logic, save a script in .claude/hooks/, make it executable, and point the command at "$CLAUDE_PROJECT_DIR"/.claude/hooks/your-script.sh. Every event, matcher and exit code is listed in the hook events reference, and you can find community hooks in the mods directory.

Frequently asked questions

What are Claude Code hooks?

Hooks are shell commands (or HTTP calls, MCP tools or model prompts) that Claude Code runs at fixed points: before or after a tool call, when you submit a prompt, when Claude stops, when a session starts and more. Unlike instructions in CLAUDE.md, a hook always runs, so it is the right place for formatting, guardrails and notifications.

How do I format code automatically after Claude edits a file?

Add a PostToolUse hook with the matcher Edit|Write that pipes the file path into your formatter, for example jq -r '.tool_input.file_path' | xargs npx prettier --write. Tick the Prettier, ESLint, Ruff or gofmt recipe above to get it.

How do I block a command with a hook?

Use a PreToolUse hook on the Bash matcher. Read the command from stdin with jq, and when it should be blocked write the reason to stderr and exit with code 2. Claude sees the reason and tries something else.

How do I get a notification when Claude Code needs me?

Add a Notification hook that runs osascript on macOS, notify-send on Linux or a PowerShell message box on Windows. Add the same command to a Stop hook to be told when Claude finishes.

Why is my hook not running?

Run /hooks to check Claude Code loaded it, make sure jq is installed if you use it, check the matcher is spelt exactly like the tool name (Edit|Write, not edit), and that script files are executable. Invalid JSON in settings.json disables the whole file.

Sources: Hooks reference, Hooks guide. Claude Code Agents is an independent site, not affiliated with Anthropic.

More free Claude Code tools

All tools →