Cette page est affichée en anglais. Une traduction française est en cours.
SkillsApr 6, 2026·2 min de lecture

Claude Code Hooks — Custom Automation Recipes

Collection of ready-to-use Claude Code hook recipes for automating code formatting, testing, notifications, and security checks. Copy-paste into settings.json. Community-maintained.

Introduction

Claude Code Hooks are shell commands that run automatically before or after specific tool uses — like git hooks but for your AI agent. This collection provides 20+ ready-to-use hook recipes for auto-formatting, test running, notifications, security scanning, and workflow automation. Copy any recipe into your settings.json and Claude Code gains new automated behaviors. Best for developers who want to enforce code quality and automate repetitive tasks around AI-generated code. Works with: Claude Code. Setup time: under 1 minute per hook.


Hook Types

Event When It Fires
PreToolUse Before a tool runs (can block)
PostToolUse After a tool completes
Notification When Claude sends a notification
Stop When Claude finishes a response

Popular Recipes

Auto-Format on Write

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null || true"
      }
    ]
  }
}

Run Tests After Code Changes

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "if echo $CLAUDE_FILE_PATH | grep -q '\\.test\\.'; then npx jest $CLAUDE_FILE_PATH --no-coverage 2>&1 | tail -5; fi"
      }
    ]
  }
}

Lint Python Files

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "if echo $CLAUDE_FILE_PATH | grep -q '\\.py$'; then ruff check --fix $CLAUDE_FILE_PATH 2>/dev/null; fi"
      }
    ]
  }
}

Block Sensitive File Edits

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "if echo $CLAUDE_FILE_PATH | grep -qE '\\.(env|pem|key)$'; then echo 'BLOCKED: Cannot edit sensitive files' >&2; exit 1; fi"
      }
    ]
  }
}

Slack Notification on Task Complete

{
  "hooks": {
    "Stop": [
      {
        "command": "curl -s -X POST $SLACK_WEBHOOK -d '{\"text\":\"Claude Code finished a task in '$(basename $(pwd))'\"}'  2>/dev/null || true"
      }
    ]
  }
}

Auto-Commit After Changes

{
  "hooks": {
    "Stop": [
      {
        "command": "if [ -n \"$(git diff --name-only)\" ]; then git add -A && git commit -m 'wip: Claude Code auto-save' --no-verify 2>/dev/null; fi"
      }
    ]
  }
}

Type Check TypeScript

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "command": "if echo $CLAUDE_FILE_PATH | grep -q '\\.tsx\\?$'; then npx tsc --noEmit 2>&1 | head -20; fi"
      }
    ]
  }
}

Security Scan

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "command": "if echo $CLAUDE_TOOL_INPUT | grep -qiE 'rm -rf|curl.*\\|.*sh|wget.*\\|.*bash'; then echo 'BLOCKED: Potentially dangerous command' >&2; exit 1; fi"
      }
    ]
  }
}

Key Stats

  • 20+ ready-to-use recipes
  • 4 hook event types
  • Pattern matching with regex
  • Environment variables available
  • Community-maintained

FAQ

Q: What are Claude Code Hooks? A: Shell commands that run automatically before or after Claude Code tool uses — like auto-formatting after every file write, or blocking edits to sensitive files.

Q: Can hooks block Claude Code actions? A: Yes, PreToolUse hooks can return exit code 1 to block the action.

Q: Do hooks slow down Claude Code? A: Hooks run synchronously, so keep them fast (<1 second). Use background processes for slow operations.


🙏

Source et remerciements

Community-maintained collection based on Claude Code documentation.

Contributions welcome — share your hook recipes!

Discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.