for pattern in"${PROTECTED_PATTERNS[@]}"; do if [[ "$FILE_PATH" == *"$pattern"* ]]; then echo"Blocked: $FILE_PATH matches protected pattern '$pattern'" >&2 exit 2 fi done
{ "hooks":{ "SessionStart":[ { "matcher":"compact", "hooks":[ { "type":"command", "command":"echo 'Reminder: use Bun, not npm. Run bun test before committing.'" } ] } ] } }
{ "hookSpecificOutput":{ "hookEventName":"PreToolUse", "permissionDecision":"deny", "permissionDecisionReason":"Use rg instead of grep for better performance" } }
使用匹配器过滤 hooks
事件
匹配器过滤的内容
示例
PreToolUse、PostToolUse
工具名称
Bash、Edit|Write、mcp__.*
SessionStart
会话如何开始
startup、resume、compact
SessionEnd
会话为什么结束
clear、logout、prompt_input_exit
Notification
通知类型
permission_prompt、idle_prompt
SubagentStart
代理类型
Bash、Explore、Plan
配置 hook 位置
位置
范围
可共享
~/.claude/settings.json
所有你的项目
否
.claude/settings.json
单个项目
是(可以提交到仓库)
.claude/settings.local.json
单个项目
否(gitignored)
Plugin hooks/hooks.json
启用插件时
是
Skill frontmatter
技能活跃时
是
Prompt-based hooks
对于需要判断的决策:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
{ "hooks":{ "Stop":[ { "hooks":[ { "type":"prompt", "prompt":"Check if all tasks are complete. If not, respond with {\"ok\": false, \"reason\": \"what remains to be done\"}." } ] } ] } }
Agent-based hooks
当验证需要检查文件或运行命令时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{ "hooks":{ "Stop":[ { "hooks":[ { "type":"agent", "prompt":"Verify that all unit tests pass. Run the test suite and check the results. $ARGUMENTS", "timeout":120 } ] } ] } }