返回播客Claude
Claude Code 101
Claude Code 中的 Hooks
Hooks let you run commands at different points in Claude code's life cycle.
Hooks 让你在 Claude Code 生命周期的不同节点运行命令。
The key difference between hooks and everything else we covered is that hooks are deterministic.
hooks 与我们介绍的其他功能的核心区别在于:hooks 是确定性的。
They always run.
它们始终会运行。
So, put it this way.
换个说法。
You can tell Claude in your claude.md file to run prettier after every file edit and most of the time it will do that, but sometimes it won't.
你可以在 claude.md 文件中告诉 Claude 在每次文件编辑后运行 prettier,大多数时候它会这么做,但有时不会。
It's not perfect.
它并不完美。
But a hook makes it happen every single time with no exceptions.
但 hook 可以让这件事每次都毫无例外地发生。
Common use cases could include auto formatting after file edits, logging all executed commands for compliance, blocking dangerous operations like modifying production files, and sending yourself notifications when Claude finishes a task.
常见用例包括:文件编辑后自动格式化、记录所有执行命令以满足合规要求、阻止修改生产文件等危险操作,以及在 Claude 完成任务时向你发送通知。
Hooks are configured in your settings.json file.
hooks 在你的 settings.json 文件中配置。
You pick an event, optionally set a matcher for which tool it applies to, and provide a command to run.
你选择一个事件,可选择为其适用的工具设置匹配器,并提供要运行的命令。
User prompts submit runs when you submit a prompt before Claude processes it.
UserPromptSubmit 在你提交提示词后、Claude 处理之前运行。
Pre-tool use which runs before a tool call, post-tool use runs after a tool call completes.
PreToolUse 在工具调用之前运行,PostToolUse 在工具调用完成后运行。
Notification runs when Claude sends a notification, and stop runs when Claude finishes responding.
Notification 在 Claude 发送通知时运行,Stop 在 Claude 完成响应时运行。
The most common hook.
最常见的 hook。
Auto formatting after edits.
编辑后自动格式化。
You set a post-tool use hook with a matcher of edit or multi-edit, right?
你设置一个 PostToolUse hook,匹配器为 edit 或 multi-edit,对吗?
So, it fires whenever Claude modifies a file.
这样每当 Claude 修改文件时它就会触发。
The command checks the file extension and runs the appropriate formatter.
命令检查文件扩展名并运行相应的格式化工具。
This could be prettier for TypeScript, go format for go, rough for Python, whatever your project uses.
可以是 TypeScript 用 prettier、Go 用 go format、Python 用 ruff,或者你的项目所用的任何工具。
Pre-tool use hooks can block tool calls before they execute.
PreToolUse hooks 可以在工具调用执行前将其阻止。
So, your hook receives the tool name and input as JSON on stdin.
因此,你的 hook 通过 stdin 以 JSON 格式接收工具名称和输入。
If it exits with code two, the action is blocked and the STD error message gets fed back to Claude's feedback so Claude knows why it was blocked and can adjust.
如果它以退出码 2 退出,则该操作被阻止,stderr 消息会反馈给 Claude,让 Claude 知道被阻止的原因并做出调整。
Exit code zero means proceed.
退出码 0 表示继续。
Exit code two means block.
退出码 2 表示阻止。
This is how you enforce hard rules.
这就是你强制执行硬规则的方式。
Block writes to a production config directory, block bash commands that contain rm-rf, block commits to main, whatever your team needs to be guaranteed, not suggested.
阻止写入生产配置目录,阻止包含 rm-rf 的 bash 命令,阻止提交到 main,无论你的团队需要保证什么,而不仅仅是建议。
Hooks configured in .Claude/settings.json are project level and can be checked into your repo. This means that your entire team gets the same hooks automatically.
在 .Claude/settings.json 中配置的 hooks 是项目级别的,可以提交到你的代码仓库。这意味着你的整个团队自动获得相同的 hooks。
Use the Claude project dir environment variable in your commands to reference scripts stored in your project so they work regardless of Claude's current working directory.
在你的命令中使用 Claude 项目目录环境变量来引用存储在项目中的脚本,这样无论 Claude 的当前工作目录是什么,它们都能正常工作。
[music]
[music]
Hooks gives you deterministic control over Claude code's behavior.
hooks 让你对 Claude Code 的行为拥有确定性的控制。
[music]
[music]
Use post-tool-use for auto-formatting and logging. Use pre-tool-use to block dangerous operations. Configure them in the /hooks or in settings.
使用 PostToolUse 进行自动格式化和日志记录。使用 PreToolUse 阻止危险操作。在 /hooks 或 settings 中配置它们。
json
json
[music]
[music]
and check them into your repository so your team gets them, too.
并将它们提交到你的代码仓库,这样你的团队也能使用。
If something needs to happen every time without fail, don't put it in a prompt.
如果某件事需要每次都毫无例外地发生,就不要把它放在提示词中。
Put it in a hook.
把它放在 hook 里。
[music]
[music]