来源:code.claude.com/docs/en/skills · hooks-guide | 整理时间:2026-04-04
概述
Claude Code 的 Skills 和 Hooks 是两大扩展机制:
- Skills(技能):将重复性的复杂工作流封装成可复用的"技能",通过
/command一键调用 - Hooks(钩子):在 Claude Code 执行工具操作的前后自动触发自定义脚本
两者结合,可以把 Claude Code 从一个通用助手定制成专属于你团队的工作流引擎。
Skills 技能系统
什么是 Skill?
Skill 是一个 .md 文件,包含 YAML 元数据和使用说明。Claude 会自动发现并加载这些技能。
<!-- .claude/skills/fix-issue/SKILL.md -->
---
name: fix-issue
description: Fix a GitHub issue end-to-end
---
1. Use `gh issue view` to get the issue details
2. Search the codebase for relevant files
3. Create a plan for the fix
4. Implement the fix
5. Write and run tests to verify
6. Create a descriptive commit and PR referencing the issue
使用方式:/fix-issue 1234
Skill 的目录结构
.claude/skills/
├── fix-issue/
│ └── SKILL.md ← 修复 GitHub issue 的技能
├── code-review/
│ └── SKILL.md ← 代码审查技能
├── deploy-check/
│ └── SKILL.md ← 部署前检查
└── generate-test/
└── SKILL.md ← 自动生成测试
自动发现机制
- Claude 会扫描
.claude/skills/目录下所有SKILL.md文件 - 支持 monorepo:子目录中的技能仅在该目录上下文中激活
- 技能优先级高于普通的
.claude/commands/自定义命令
实用技能示例
示例 1:代码审查技能
<!-- .claude/skills/code-review/SKILL.md -->
---
name: review
description: Comprehensive code review with security focus
---
1. Read the current git diff (staged and unstaged)
2. Analyze for:
- Logic errors and edge cases
- Security vulnerabilities (injection, XSS, auth)
- Performance concerns
- Code style consistency with CLAUDE.md rules
3. Rate severity: 🔴 Critical / 🟡 Warning / 🔵 Suggestion
4. Provide fix suggestions for each finding
示例 2:数据库迁移技能
<!-- .claude/skills/migrate/SKILL.md -->
---
name: migrate
description: Generate and run a database migration
---
1. Understand the schema change from user description
2. Generate both UP and DOWN migration files
3. Review for data loss risks
4. Run the migration in development
5. Verify the schema is correct
社区技能资源
| 资源 | 描述 | 链接 |
|---|---|---|
| awesome-claude-code | 社区技能、钩子、命令合集 | https://github.com/hesreallyhim/awesome-claude-code |
| Claude Code Templates | 400+ 可安装模板 | https://aitmpl.com |
| agnix | AI 编码配置 linter(385 条规则) | https://github.com/agent-sh/agnix |
Hooks 钩子系统
三种钩子类型
| 类型 | 描述 | 适用场景 |
|---|---|---|
| Shell | 执行 shell 命令 | lint、格式化、通知 |
| Prompt | 单次 LLM 调用 | 智能审查、自动生成 |
| Agent | 完整子 Agent(带工具) | 复杂检查、自动修复 |
四个生命周期事件
| 事件 | 触发时机 | 典型用途 |
|---|---|---|
PreToolUse |
工具执行前 | 拦截高风险命令、注入额外上下文 |
PostToolUse |
工具执行后 | 自动 lint、格式化、通知 |
Notification |
Claude 发送通知时 | 转发到 Slack/Telegram |
Stop |
会话停止时 | 清理、生成报告 |
配置方式
在 .claude/settings.json 中配置:
{
"hooks": {
"PostToolUse": [
{
"type": "shell",
"command": "npx eslint --fix $CLAUDE_FILE_PATH",
"matcher": "Edit|Write"
}
],
"PreToolUse": [
{
"type": "shell",
"command": "echo 'About to modify: $CLAUDE_FILE_PATH'",
"matcher": "Edit|Write"
}
]
}
}
实用钩子示例
自动 lint(PostToolUse)
{
"hooks": {
"PostToolUse": [
{
"type": "shell",
"command": "npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null",
"matcher": "Edit|Write"
}
]
}
}
安全审查(PreToolUse)
{
"hooks": {
"PreToolUse": [
{
"type": "prompt",
"prompt": "Review this tool call for security risks. Block if it: deletes production data, exposes secrets, or modifies critical infrastructure.",
"matcher": "Bash"
}
]
}
}
通知到 Slack(Notification)
{
"hooks": {
"Notification": [
{
"type": "shell",
"command": "curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"Claude Code: $CLAUDE_NOTIFICATION\"}' $SLACK_WEBHOOK_URL"
}
]
}
}
自动测试(PostToolUse)
{
"hooks": {
"PostToolUse": [
{
"type": "agent",
"prompt": "If the modified file is a source file (not test), find and run the related test file. If tests fail, report the failures.",
"matcher": "Edit|Write"
}
]
}
}
让 Claude 帮你写钩子
你不需要手写钩子配置,直接告诉 Claude:
Create a hook that automatically runs prettier on files after each edit
Set up a PreToolUse hook that blocks any Bash command containing 'rm -rf' or 'DROP TABLE'
Claude 会自动生成正确的 settings.json 配置。
Skills + Hooks 组合模式
模式 1:技能激活钩子
{
"hooks": {
"PostToolUse": [
{
"type": "agent",
"prompt": "If the user just ran /deploy-check skill and all checks passed, remind them to update CHANGELOG.md",
"matcher": "Edit|Write"
}
]
}
}
模式 2:TDD 工作流
{
"hooks": {
"PostToolUse": [
{
"type": "shell",
"command": "cd $PROJECT_ROOT && npm test -- --related $CLAUDE_FILE_PATH --watch=false 2>&1 | tail -20",
"matcher": "Edit|Write"
}
]
}
}
写测试 → Claude 实现 → 自动运行测试 → 反馈结果,形成闭环。
模式 3:自动安全审查
{
"hooks": {
"PreToolUse": [
{
"type": "prompt",
"prompt": "Check for: SQL injection, XSS, command injection, hardcoded secrets, and insecure defaults. If found, block the tool call.",
"matcher": "Edit|Write"
}
]
}
}
最佳实践
| 建议 | 说明 |
|---|---|
| 团队统一 | 把 .claude/settings.json 和 .claude/skills/ 提交到 Git |
| 分层配置 | 全局 hooks 在 ~/.claude/settings.json,项目 hooks 在本地 |
| 保持简单 | 钩子不要太重,否则每次编辑都会变慢 |
| 用 Claude 写 | 直接让 Claude 生成配置,比自己手写更快更准确 |
| 先试后提交 | 在本地测试钩子效果,确认无误后再提交到团队仓库 |