Skills(技能)是 Claude Code 最强大的扩展机制——创建一个 SKILL.md 文件,Claude 就能将其加入工具箱,在合适时自动使用,或通过 /技能名 直接调用。
旧版
.claude/commands/自定义命令已合并到 Skills,现有文件继续有效,但 Skills 支持更多功能(支持文件目录、前置元数据、自动调用控制、子 Agent 执行)。
内置 Bundled Skills
Claude Code 自带 5 个 Bundled Skills,每次会话均可用:
| Skill | 调用方式 | 说明 |
|---|---|---|
/simplify | 用户调用 | 审查近期修改文件,并行启动 3 个 Agent(代码复用/质量/效率),汇总修复 |
/batch <指令> | 用户调用 | 大规模并行代码变更——分解为 5-30 个独立单元,每个单元一个 Git Worktree Agent,各自实现并开 PR |
/debug [描述] | 用户调用 | 读取 Session Debug 日志,排查当前 Claude Code 会话问题 |
/loop [间隔] <提示> | 用户调用 | 按时间间隔重复执行提示词,监控部署/PR/构建结果 |
/claude-api | 自动/用户 | 加载 Claude API 参考(Python/TS/Java/Go/Ruby/C#/PHP),代码导入 anthropic 时自动激活 |
创建第一个 Skill(3 步)
第一步:创�� Skill 目录
# 个人 Skill(所有项目可用)
mkdir -p ~/.claude/skills/explain-code
# 项目 Skill(仅本项目)
mkdir -p .claude/skills/explain-code第二步:编写 SKILL.md
---
name: explain-code
description: Explains code with visual diagrams and analogies. Use when explaining how code works, teaching about a codebase, or when the user asks "how does this work?"
---
When explaining code, always include:
1. **Start with an analogy**: Compare the code to something from everyday life
2. **Draw a diagram**: Use ASCII art to show the flow, structure, or relationships
3. **Walk through the code**: Explain step-by-step what happens
4. **Highlight a gotcha**: What's a common mistake or misconception?第三步:测试
# 自动调用(Claude 根据 description 判断)
How does this code work?
# 直接调用
/explain-code src/auth/login.ts
Skill 存储位置与优先级
| 位置 | 路径 | 作用范围 |
|---|---|---|
| Enterprise | 托管设置 | 组织所有用户 |
| Personal | ~/.claude/skills/<name>/SKILL.md | 你的所有项目 |
| Project | .claude/skills/<name>/SKILL.md | 本项目所有协作者 |
| Plugin | <plugin>/skills/<name>/SKILL.md | 插件启用的项目 |
同名 Skill 优先级:Enterprise > Personal > Project。Plugin Skill 使用 plugin-name:skill-name 命名空间,不会冲突。
Monorepo 自动发现:编辑 packages/frontend/ 中的文件时,Claude Code 自动发现 packages/frontend/.claude/skills/ 中的 Skills。
Frontmatter 配置参考
---
name: review-pr # Slash 命令名称(/review-pr)
description: | # 帮助 Claude 决定何时自动加载
Reviews pull requests for code quality.
Use when reviewing PRs or checking code changes.
disable-model-invocation: true # 禁止 Claude 自动调用,只能手动 /invoke
tools: [Read, Bash] # 限制此 Skill 可用的工具
---| 字段 | 默认值 | 说明 |
|---|---|---|
name | 文件目录名 | Slash 命令名称 |
description | 无 | 帮助 Claude 决定自动加载时机 |
disable-model-invocation | false | 为 true 时只能用户手动调用 |
tools | 全部 | 限制该 Skill 可使用的工具列表 |
两种 Skill 内容类型
参考型:添加 Claude 应用到当前工作的知识(规范、模式、风格指南)。以内联方式运行,可与对话上下文共用。
---
name: api-conventions
description: API design patterns for this codebase
---
When writing API endpoints:
- Use RESTful naming conventions
- Return consistent error formats
- Include request validation任务型:给 Claude 分步操作指令(部署、提交、代码生成)。通常手动调用,加 disable-model-invocation: true。
高级模式
字符串替换
在 SKILL.md 中可使用占位符:
$ARGUMENTS # 用户在 /skill-name 后输入的参数
$CURRENT_FILE # 当前打开的文件路径
$SELECTION # 当前选中的文本支持文件目录
explain-code/
├── SKILL.md # 主指令(必须)
├── template.md # Claude 填写的模板
├── examples/
│ └── sample.md # 示例输出
└── scripts/
└── validate.sh # Claude 可执行的脚本
在 SKILL.md 中引用这些文件,Claude 知道它们的位置和用途。
动态注入上下文
---
name: review-pr
description: Reviews pull requests
---
First, run: `git diff main`
Then review the output for:
- Security vulnerabilities
- Performance issuesClaude 执行命令并将输出纳入分析上下文。
在子 Agent 中运行 Skill
---
name: research
description: Deep research using isolated agent
---
Use the Explore agent to research this topic thoroughly:
1. Search for relevant files
2. Read and analyze key sections
3. Return a structured summary在子 Agent(Explore agent)中运行,避免污染主会话上下文。
共享 Skills
- 通过插件:打包为插件后分发,支持版本管理,可发布到市场
- 通过项目:提交
.claude/skills/到 git,团队共享 - 通过个人目录:
~/.claude/skills/仅本人使用
故障排查
| 问题 | 解决方案 |
|---|---|
| Skill 未触发 | 检查 description 是否清晰描述触发场景 |
| Skill 触发太频繁 | 加 disable-model-invocation: true,改为手动调用 |
| Claude 看不到所有 Skill | 检查目录结构(每个 Skill 一个目录,SKILL.md 在目录内) |
原文:Extend Claude with skills - Claude Code Docs | 来源:Anthropic 官方文档