Back to list
alexander-grieco

hooks

by alexander-grieco

My configuration dotfiles

0🍴 0📅 Jan 16, 2026

SKILL.md


name: hooks description: | Write effective Claude Code hooks for automation, validation, and workflow control. Use when creating hooks, configuring PreToolUse/PostToolUse events, or automating Claude behavior. Triggers: hooks, PreToolUse, PostToolUse, PermissionRequest, SessionStart, Stop, hook configuration

Claude Code Hooks Developer Guide

What Are Hooks?

Hooks are user-defined shell commands that execute at specific points in Claude Code's lifecycle. They provide deterministic control over Claude's behavior—ensuring certain actions always happen rather than relying on Claude to choose to do them.

Key Principle: Hooks turn suggestions into automated actions. Instead of asking Claude to format code, a hook makes it happen every time.


Quick Reference: Which Hook to Use

GoalHook EventDetails
Validate/block before tool runsPreToolUseReceives tool parameters, can modify input
Auto-approve tool callsPreToolUseSet permissionDecision: "allow"
Format code after editsPostToolUseMatch Edit|Write tools
Log/audit commandsPreToolUseLog to file before execution
Prevent sensitive file editsPreToolUseCheck file path, exit 2 to block
Add context to promptsUserPromptSubmitInject context via stdout or JSON
Force Claude to keep workingStopSet decision: "block" with reason
Desktop notificationsNotificationUse notify-send or similar
Setup environmentSessionStartSet env vars via $CLAUDE_ENV_FILE
Cleanup on exitSessionEndRemove temp files, save state

For detailed event documentation, see events-reference.md.


Hook Configuration Locations

Hooks can be configured in four scopes (highest to lowest priority):

ScopeLocationShared?
ManagedSystem directories (IT-deployed)All users
Local Project.claude/settings.local.jsonYou only (gitignored)
Project.claude/settings.jsonTeam (committed)
User~/.claude/settings.jsonYou across all projects

Configuration in settings.json

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "./scripts/validate-bash.sh",
            "timeout": 30
          }
        ]
      }
    ]
  }
}

Configuration in Skill Frontmatter

Skills can embed hooks that activate when the skill is used:

---
name: secure-operations
description: Perform operations with security validation
hooks:
  PreToolUse:
    - matcher: "Bash"
      hooks:
        - type: command
          command: "./scripts/security-check.sh"
---

Hook Types

Execute shell commands with JSON input via stdin:

{
  "type": "command",
  "command": "python3 ~/.claude/hooks/validate.py",
  "timeout": 60
}

Prompt Hooks

Use LLM evaluation for context-aware decisions:

{
  "type": "prompt",
  "prompt": "Evaluate if Claude should stop. Check if tasks are complete.",
  "timeout": 30
}

Supported for: Stop, SubagentStop, UserPromptSubmit, PreToolUse, PermissionRequest


Core Patterns

Exit Codes

CodeMeaningBehavior
0Successstdout parsed as JSON
2Blocking errorstderr shown to Claude, execution blocked
OtherNon-blocking errorstderr shown in verbose mode, continues

JSON Output Schema

All hooks can return:

{
  "continue": true,
  "stopReason": "string",
  "suppressOutput": true,
  "systemMessage": "Warning to show user"
}

PreToolUse Permission Control

{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "allow|deny|ask",
    "permissionDecisionReason": "Auto-approved: safe operation",
    "updatedInput": {
      "command": "modified command"
    }
  }
}

Matcher Patterns

Matchers are case-sensitive and support regex:

PatternMatches
BashOnly Bash tool
Edit|WriteEither Edit OR Write
Notebook.*All Notebook tools
mcp__server__.*All tools from an MCP server
"" or *All tools

Environment Variables

Available in hook commands:

VariableValue
$CLAUDE_PROJECT_DIRAbsolute path to project root
$CLAUDE_CODE_REMOTE"true" if web, empty if CLI
$CLAUDE_ENV_FILE(SessionStart only) Path for persisting env vars

Debugging Hooks

  1. View registered hooks: Run /hooks command
  2. Test manually: Run hook commands in terminal first
  3. Debug mode: Use claude --debug to see execution
  4. Check permissions: Ensure scripts are executable (chmod +x)

Additional Resources

TopicFileWhen to Use
All 10 Eventsevents-reference.mdDetailed event documentation
Examplesexamples.mdCopy-paste implementations
Securitysecurity.mdValidation and safety patterns

Security Warning

Hooks execute with your user credentials. Always:

  • Review hook code before adding
  • Test in safe environment first
  • Validate and sanitize all inputs
  • Use absolute paths in scripts

See security.md for comprehensive security guidance.

Score

Total Score

50/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon