c/agents › command
0votes

Claude Code hooks: format on save and block dangerous commands

Template
submitted by u/client to c/agents · 0 copies

A .claude/settings.json with hooks that auto-format edited files and stop risky shell commands.

Template · 2 variables
Add this to `.claude/settings.json` in your project:

```json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "jq -r '.tool_input.file_path' | xargs {{formatter_command}}" }
        ]
      }
    ],
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": ".claude/hooks/block-dangerous.sh" }
        ]
      }
    ]
  }
}
```

And create `.claude/hooks/block-dangerous.sh` (make it executable):

```bash
#!/usr/bin/env bash
cmd=$(jq -r '.tool_input.command')
if echo "$cmd" | grep -Eq 'rm -rf /|git push --force|DROP TABLE|{{extra_blocked_pattern}}'; then
  echo "Blocked by project hook: $cmd" >&2
  exit 2   # exit code 2 blocks the tool call and shows the message to Claude
fi
```

Replace `{{formatter_command}}` with e.g. `npx prettier --write` or `ruff format`. Test it by asking Claude to edit a file and check it was formatted.
0 commentsreport

How to use this command

  1. Fill in the blanks. Type your details into the boxes above. This command has 2 blanks:
    • {{formatter_command}} Formatter command
    • {{extra_blocked_pattern}} Extra blocked pattern
  2. Copy itwith the Copy button. Your answers are filled in automatically. Use “copy raw” to keep the blanks for later.
  3. Paste it into Claude, Cursor, Codex, ChatGPT or Gemini and send it.
  4. Tip: This produces a file or document. Save the filled-in result in your project (for example as CLAUDE.md or AGENTS.md) so you can reuse it.
  5. Careful: this one is marked medium risk. Review what the AI suggests before you run it on real systems or data.

More from c/agents

See all of c/agents →
0 comments

Log in to join the discussion.