Skills2026年4月8日·1 分钟阅读

Claude Code Hooks — Automate Pre/Post Task Actions

Complete guide to Claude Code hooks for automating actions before and after tool calls. Set up linting, testing, notifications, and custom validation with shell commands.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Claude Code Hooks — Automate Pre/Post Task Actions
先审查命令
npx -y tokrepo@latest install ba645a85-3df0-40ab-8c75-15aec5c8a2af --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
Claude Code hooks run custom scripts before or after tool calls for linting, testing, and validation.
§01

What it is

Claude Code hooks let you define custom scripts that run automatically before or after specific tool calls. When Claude Code writes a file, you can trigger ESLint to fix it. When Claude Code finishes a task, you can run your test suite. Hooks are configured in ~/.claude/settings.json and apply to all projects or per-project in .claude/settings.json.

Hooks are designed for developers who want to enforce code quality standards, automate repetitive checks, and integrate external tools into their Claude Code workflow without manual intervention.

§02

How it saves time or tokens

Without hooks, you manually run linters, formatters, and tests after Claude Code modifies files. Hooks automate this entirely. Every file write triggers your configured checks, catching issues immediately rather than at the end of a long coding session.

Hooks also reduce token waste. If a linter catches an error right after the file is written, Claude Code can fix it in the next turn instead of generating more code on top of a broken file.

§03

How to use

  1. Open your Claude Code settings file:
# Global: ~/.claude/settings.json
# Per-project: .claude/settings.json
  1. Add hook configurations:
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write",
        "hooks": ["eslint --fix $CLAUDE_FILE_PATH"]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": ["prettier --write $CLAUDE_FILE_PATH"]
      }
    ]
  }
}
  1. Claude Code will now run these hooks automatically when the matched tool is used.
§04

Example

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": ["echo 'About to run a bash command'"]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Write",
        "hooks": [
          "eslint --fix $CLAUDE_FILE_PATH",
          "prettier --write $CLAUDE_FILE_PATH"
        ]
      },
      {
        "matcher": "Bash",
        "hooks": ["echo 'Bash command completed'"]
      }
    ],
    "PostTask": [
      {
        "matcher": ".*",
        "hooks": ["npm test 2>&1 | tail -20"]
      }
    ]
  }
}

This configuration lints and formats after every file write, logs bash command execution, and runs the test suite after each completed task.

§05

Related on TokRepo

§06

Common pitfalls

  • Hook scripts must be available on the system PATH. If ESLint is installed locally in node_modules, use npx eslint instead of bare eslint in the hook command.
  • Long-running hooks block Claude Code's execution. Keep hooks fast (under 5 seconds). Move heavy operations like full test suites to PostTask rather than PostToolUse.
  • The $CLAUDE_FILE_PATH variable is only available for file-related tools like Write. Using it with non-file tools like Bash will produce an empty string or an error.

常见问题

What hook events are available in Claude Code?+

Claude Code supports PreToolUse (before a tool call), PostToolUse (after a tool call), and PostTask (after an entire task completes). Each event type can have multiple hooks with different matchers. The matcher field specifies which tool triggers the hook.

Can I use hooks for notifications?+

Yes. You can configure PostTask hooks to send notifications via curl to Slack, Discord, or email services. For example, a hook like 'curl -X POST https://hooks.slack.com/... -d ...' sends a message when a task completes.

Do hooks run in the background or block execution?+

Hooks run synchronously and block Claude Code until they complete. This is by design -- it ensures that lint errors are caught before Claude Code proceeds. For long-running operations, consider using background processes or moving them to PostTask.

Can I disable hooks for a specific session?+

You can override hooks by using a per-project .claude/settings.json with an empty hooks configuration. There is no built-in flag to skip hooks for a single session, but you can temporarily rename the settings file.

How do I debug hook failures?+

Hook output (stdout and stderr) is displayed in the Claude Code terminal. If a hook fails, the error message appears inline. Test your hook commands manually in the terminal first to ensure they work before adding them to the configuration.

引用来源 (3)
🙏

来源与感谢

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产