Create and configure Claude Code hooks for automating workflows and behavior control
Create and configure Claude Code hooks to automate workflows, control behavior, and set up notifications. Use when you want to automate repetitive tasks, enforce rules, or get alerts based on Claude's actions.
/plugin marketplace add majesticlabs-dev/majestic-marketplace/plugin install majestic-llm@majestic-marketplace[hook-description]meta//new-hookCreate and configure Claude Code hooks for automating workflows, notifications, formatting, and behavior control.
[hook-description] - Description of the hook you want to create# Create a logging hook
/new-hook "log all bash commands to a file"
# Create a protection hook
/new-hook "prevent editing .env files"
# Create a formatting hook
/new-hook "auto-format TypeScript files after editing"
# Create a notification hook
/new-hook "send desktop notification when Claude needs input"
Claude Code provides these hook events:
For comprehensive information, refer to:
When creating hooks, follow these steps:
* for all tools)AskUserQuestion to ask: user settings (global) or project settings (local)?Tool Matchers:
Bash - Shell commands onlyEdit|MultiEdit|Write - File editing operationsRead - File reading operations* - All toolsExit Codes (PreToolUse only):
0 - Allow tool execution2 - Block tool execution with feedback⚠️ CRITICAL: Hooks run automatically with your environment's credentials. Always:
Hooks are stored in JSON format in ~/.claude/settings.json (user) or <project>/.claude/settings.json (project):
{
"hooks": {
"<EventType>": [
{
"matcher": "<tool-pattern>",
"hooks": [
{
"type": "command",
"command": "<shell-command>"
}
]
}
]
}
}
Command Logging (PreToolUse):
jq -r '"\(.tool_input.command) - \(.tool_input.description // "No description")"' >> ~/.claude/command-log.txt
Code Formatting (PostToolUse):
jq -r '.tool_input.file_path' | { read file_path; if echo "$file_path" | grep -q '\.ts$'; then npx prettier --write "$file_path"; fi; }
File Protection (PreToolUse):
python3 -c "import json, sys; data=json.load(sys.stdin); path=data.get('tool_input',{}).get('file_path',''); sys.exit(2 if any(p in path for p in ['.env', 'package-lock.json', '.git/']) else 0)"
Desktop Notifications (Notification):
notify-send 'Claude Code' 'Awaiting your input'
After creating a hook, provide:
Always explain the hook's purpose, when it triggers, and what it accomplishes.
/hooks slash command in Claude Code to configure hooks interactively~/.claude/settings.json or <project>/.claude/settings.json