Files
claude-howto/zh/06-hooks
Luong NGUYENandGitHub b9a973bf32 docs: accuracy pass against Claude Code v2.1.220 (#155)
Internal accuracy pass against v2.1.220 — no missing upstream features, but broken example code, disagreeing counts, and metadata drift.

Functional fixes: pre-commit.sh now exits 2 so it actually blocks; dependency-check.sh reads file_path from stdin JSON instead of $1; database-mcp.json uses ${DATABASE_URL}; broken fences repaired; three command templates had invalid skill names.

Factual corrections: /fork and /subtask unswapped and /subtask added; /fewer-permission-prompts; permissions.defaultMode; dontAsk/auto unreversed; 31 hook events verified name-by-name; subagent depth 3; skill precedence enterprise > project > personal; /output-style removed not deprecated; permissionDecision gained defer.

Follow-up review fixed defects the pass left behind: zh/vi headers claiming 31 events above 25-name lists, a surviving hardcoded DB credential in the MCP README examples, an unbalanced fence swallowing a metadata footer, and non-canonical tool names. All four translated CATALOG summary tables were recounted so their arithmetic holds.

Full detail in CHANGELOG.md under v2.1.220-r2.
2026-08-04 15:41:12 +07:00
..

Claude How To

Hooks 参考

Hooks 是在 Claude Code 事件发生时自动执行的 shell 命令,用来做格式化、校验、通知、审计等自动化工作。

概览

Hooks 是事件驱动的自动化机制。它们会在 Claude Code 发生某些动作时自动运行,不需要你手动触发。

常见用途:

  • 写文件前自动格式化
  • 提交前运行测试
  • 扫描安全问题
  • 记录 bash 命令
  • 校验用户提示词
  • 发送团队通知

Hook 类型

Claude Code 支持 31 个 hook 事件,按用途分为以下 4 组。事件的 handler 类型共 5 种:commandhttpmcp_toolpromptagent

  • Tool HooksPreToolUsePostToolUsePostToolUseFailurePostToolBatchPermissionRequestPermissionDenied
  • Session HooksSessionStartSetupSessionEndStopStopFailureSubagentStartSubagentStop
  • Task HooksUserPromptSubmitUserPromptExpansionMessageDisplayTaskCompletedTaskCreatedTeammateIdle
  • Lifecycle HooksConfigChangeCwdChangedDirectoryAddedFileChangedPreCompactPostCompactWorktreeCreateWorktreeRemoveNotificationInstructionsLoadedElicitationElicitationResult

安装

mkdir -p ~/.claude/hooks
cp 06-hooks/*.sh ~/.claude/hooks/
chmod +x ~/.claude/hooks/*.sh

然后在 ~/.claude/settings.json 里配置:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/format-code.sh"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/security-scan.sh"
          }
        ]
      }
    ]
  }
}

使用方法

Hooks 会在匹配到事件时自动执行。你可以把它理解成 Claude Code 的事件回调。

常见示例

  • format-code.sh - 写入前自动格式化
  • pre-commit.sh - 提交前跑测试
  • security-scan.sh - 做安全扫描
  • log-bash.sh - 记录 bash 命令
  • validate-prompt.sh - 校验输入
  • notify-team.sh - 发通知

最佳实践

  • 把 hooks 保持短小明确
  • 只做单一职责
  • 先在本地测试
  • 不要在 hook 里放复杂业务逻辑
  • 对副作用保持谨慎

故障排查

  • 检查文件路径和权限
  • 确认脚本可执行
  • 检查 settings.json 语法
  • 查看 Claude Code 版本兼容性

相关概念

更多资源