# Claude Code Hooks — Automate Your AI Workflow > Built-in automation system for Claude Code. Run shell commands on events like file edits, tool calls, and notifications. Lint on save, auto-test, and more. ## Install Save the content below to `.claude/skills/` or append to your `CLAUDE.md`: ## Quick Use Add hooks to `.claude/settings.json`: ```json { "hooks": { "PostToolUse": [ { "matcher": "Edit|Write", "command": "eslint --fix $CLAUDE_FILE_PATH" } ] } } ``` Now every file edit by Claude Code auto-runs ESLint. ## What are Claude Code Hooks? Claude Code Hooks are automation triggers that run shell commands in response to Claude Code events. They let you enforce coding standards, run tests, validate changes, and integrate with external tools — all automatically, without Claude needing to remember to do it. **Answer-Ready**: Claude Code Hooks are built-in automation triggers that run shell commands on events like file edits, tool calls, and notifications. Enforce linting, auto-testing, and custom validation in your Claude Code workflow. **Best for**: Developers who want automated quality checks in their Claude Code workflow. **Works with**: Claude Code CLI and IDE extensions. **Setup time**: Under 2 minutes. ## Core Features ### 1. Event Types | Event | Fires When | |-------|-----------| | `PreToolUse` | Before a tool executes | | `PostToolUse` | After a tool completes | | `Notification` | When Claude sends a notification | | `Stop` | When Claude finishes a turn | | `SubagentStop` | When a subagent completes | ### 2. Tool Matchers Target specific tools: ```json { "hooks": { "PostToolUse": [ { "matcher": "Edit|Write", "command": "prettier --write $CLAUDE_FILE_PATH" }, { "matcher": "Bash", "command": "echo 'Command executed: $CLAUDE_TOOL_INPUT'" } ] } } ``` ### 3. Common Hook Patterns **Auto-lint on file changes:** ```json { "matcher": "Edit|Write", "command": "eslint --fix $CLAUDE_FILE_PATH 2>/dev/null || true" } ``` **Run tests after code changes:** ```json { "matcher": "Edit", "command": "npm test -- --related $CLAUDE_FILE_PATH 2>&1 | tail -5" } ``` **Type-check TypeScript files:** ```json { "matcher": "Edit|Write", "command": "[[ $CLAUDE_FILE_PATH == *.ts ]] && npx tsc --noEmit 2>&1 | head -20 || true" } ``` **Block commits without tests:** ```json { "matcher": "Bash", "command": "if echo $CLAUDE_TOOL_INPUT | grep -q 'git commit'; then npm test || exit 1; fi" } ``` ### 4. Environment Variables | Variable | Content | |----------|---------| | `CLAUDE_FILE_PATH` | Path of the affected file | | `CLAUDE_TOOL_INPUT` | JSON input to the tool | | `CLAUDE_TOOL_OUTPUT` | JSON output from the tool | ### 5. Hook Output Hook stdout is fed back to Claude as context: ```json { "matcher": "Edit", "command": "python check_style.py $CLAUDE_FILE_PATH" } ``` If the style checker reports issues, Claude sees the output and can fix them. ## Advanced: PreToolUse Blocking ```json { "hooks": { "PreToolUse": [ { "matcher": "Edit", "command": "python validate_edit.py", "blocking": true } ] } } ``` If the command exits non-zero, the tool call is blocked. ## FAQ **Q: Do hooks slow down Claude Code?** A: Hooks run synchronously, so heavy commands add latency. Keep hooks fast (< 2s) or make them async. **Q: Can hooks modify Claude's behavior?** A: Yes, hook stdout is fed back as context. You can guide Claude's next action. **Q: Where do I put the settings file?** A: `.claude/settings.json` in your project root (project-level) or `~/.claude/settings.json` (global). ## Source & Thanks > Built into [Claude Code](https://docs.anthropic.com/en/docs/claude-code) by Anthropic. > > Documentation: [docs.anthropic.com/en/docs/claude-code/hooks](https://docs.anthropic.com/en/docs/claude-code/hooks) ## 快速使用 在 `.claude/settings.json` 中添加 hooks 配置,Claude Code 自动在文件编辑后运行 ESLint 等工具。 ## 什么是 Claude Code Hooks? Claude Code Hooks 是内置自动化触发器,在文件编辑、工具调用等事件时运行 shell 命令。自动执行代码检查、测试和验证。 **一句话总结**:Claude Code 内置自动化系统,在文件编辑和工具调用时自动运行 lint、测试和自定义验证。 **适合人群**:希望在 Claude Code 工作流中自动化质量检查的开发者。 ## 核心功能 ### 1. 事件类型 PreToolUse、PostToolUse、Notification、Stop、SubagentStop。 ### 2. 工具匹配器 用正则匹配特定工具(Edit、Write、Bash 等)。 ### 3. 常见模式 自动 lint、运行测试、类型检查、阻止无测试提交。 ### 4. 输出反馈 Hook 输出作为上下文反馈给 Claude,引导后续操作。 ## 常见问题 **Q: 会拖慢 Claude Code 吗?** A: 同步运行,保持命令 < 2 秒。 **Q: 配置文件放哪?** A: 项目级 `.claude/settings.json` 或全局 `~/.claude/settings.json`。 ## 来源与致谢 > Anthropic 内置于 [Claude Code](https://docs.anthropic.com/en/docs/claude-code) --- Source: https://tokrepo.com/en/workflows/587e4918-987f-4705-b4dd-e7abb7c7c9be Author: Skill Factory