主题
Hooks 实战应用
应用1: 自动代码审查
创建 .claude/hooks/review.json:
json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "npx eslint --no-error-on-uncaught ${file}"
}
]
}
]
}
}效果: 每次编辑或写入文件后,自动运行 ESLint 检查
应用2: 自动格式化
创建 .claude/hooks/format.json:
json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "npx prettier --write ${file}"
}
]
}
]
}
}效果: 保存文件时自动格式化代码
应用3: 发送通知
创建 .claude/hooks/notify.json:
json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "echo '✅ 匽令执行完成: ${command}'"
}
]
}
]
}
}效果: 命令执行完成后显示通知
下一阶段: Skills 定制