Black Friday sale: 85 Claude Code agents for $29 one-time · lifetime updatesGet access →
Free Tool

Claude Code Hooks Generator

Hooks run your own shell commands at set points in a Claude Code session: before a tool runs, after a file is edited, when Claude needs you, or when it finishes. Tick the hooks you want and copy a valid hooks block for your settings file.

Add your own hook (optional)
hooks block
.claude/settings.json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "f=$(jq -r '.tool_input.file_path // empty'); [ -n \"$f\" ] && npx --no-install prettier --write --ignore-unknown \"$f\" >/dev/null 2>&1; exit 0"
          }
        ]
      }
    ],
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.command' | grep -qE 'rm[[:space:]]+-[a-zA-Z]*r[a-zA-Z]*f|rm[[:space:]]+-[a-zA-Z]*f[a-zA-Z]*r' && { echo 'Blocked by hook: rm -rf is not allowed' >&2; exit 2; } || exit 0"
          }
        ]
      },
      {
        "matcher": "Read|Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.file_path // empty' | grep -qE '(^|/)\\.env($|\\.)|\\.pem$|id_rsa' && { echo 'Blocked by hook: secret files are off limits' >&2; exit 2; } || exit 0"
          }
        ]
      }
    ]
  }
}

These hooks need: jq, Node.js, jq.

How to add the hooks

  1. Open (or create) .claude/settings.json.
  2. If the file is empty, paste the whole output. If it already has settings, add the "hooks" key next to them.
  3. Restart Claude Code or run /hooks to check they loaded.
  4. Test each hook once, for example ask Claude to run rm -rf tmp in a scratch folder and confirm it is blocked.
Skip the setup: 85 ready-made agents for Claude Code

Coding, SEO and marketing agents, 52 skills and 53 slash commands. Installs with one line.

All free tools →

Frequently asked questions

In a settings file under the "hooks" key: .claude/settings.json to share with your team, .claude/settings.local.json for just you in this project, or ~/.claude/settings.json for all projects. If the file already has other settings, merge the hooks block into it rather than replacing the file.

Related guides and tools