Skills(技能) 是 Claude Code 的核心扩展机制,它为 Claude 赋予了”瑞士军刀”般的能力,使其能够按需加载特定功能、执行复杂任务并与外部工具无缝协作。本文将手把手教你如何创建、配置和使用 Skills,打造专属于你的 AI 编程助手。
什么是 Skills?
Skills 是 Claude Code 的智能扩展系统,具有以下特点:
- 📁 按需加载:Claude 只在需要时自动加载相关技能,节省上下文空间
- 🌐 跨平台通用:一次创建,可在 Claude Code、Claude.ai 和 API 中使用
- 🗄️ 组合性强:多个 Skills 可以像乐高积木一样组合使用
- 🛠️ 功能强大:支持包含可执行脚本、模板和参考文档
- 🔧 智能触发:Claude 会根据你的任务自动判断何时使用哪个 Skill
30秒快速上手
如果你赶时间,这是最快的创建方法:
# 创建个人 Skill 目录 mkdir -p ~/.claude/skills/explain-code # 编写 SKILL.md 文件 cat > ~/.claude/skills/explain-code/SKILL.md << 'EOF' --- name: explain-code description: Explains code with visual diagrams and analogies --- When explaining code, always include: 1. **Start with an analogy** 2. **Draw a diagram** using ASCII art 3. **Walk through the code** step-by-step 4. **Highlight a gotcha** or common mistake EOF # 测试 Skill /explain-code your-file.js
详细配置方法
方法一:Skill 目录结构
Skills 根据存储位置分为三种类型:
| 类型 | 存储位置 | 适用场景 |
|---|---|---|
| 个人 Skills | ~/.claude/skills/<skill-name>/ |
所有项目可用 |
| 项目 Skills | .claude/skills/<skill-name>/ |
仅当前项目,团队共享 |
| 插件 Skills | <plugin>/skills/<skill-name>/ |
插件启用时可用 |
方法二:SKILL.md 文件格式
每个 Skill 都需要一个 SKILL.md 文件,包含 YAML frontmatter 和说明内容:
--- name: my-skill description: What this skill does argument-hint: [filename] disable-model-invocation: true user-invocable: false allowed-tools: Read, Grep context: fork model: claude-3-5-sonnet --- Your skill instructions here...
重要提示:YAML frontmatter 必须位于文件顶部,用
---分隔。所有字段都是可选的,但建议提供description以便 Claude 知道何时使用该 Skill。
方法三:参数传递
Skills 支持动态参数传递:
--- name: fix-issue description: Fix a GitHub issue disable-model-invocation: true --- Fix GitHub issue $ARGUMENTS following our coding standards.
调用方式:
/fix-issue 123
高级功能
1. 添加支持文件
复杂的 Skills 可以包含多个文件:
my-skill/
├── SKILL.md # 主说明文件(必需)
├── template.md # Claude 填充的模板
├── examples/ # 示例输出
│ └── sample.md
└── scripts/ # 可执行脚本
└── validate.sh
2. 动态上下文注入
使用 !`command` 语法在发送给 Claude 前执行命令:
--- name: pr-summary description: Summarize changes in a pull request context: fork agent: Explore allowed-tools: Bash(gh *) --- ## Pull request context - PR diff: !`gh pr diff` - PR comments: !`gh pr view --comments` - Changed files: !`gh pr diff --name-only` ## Your task Summarize this pull request...
3. 在 Subagent 中运行
对于复杂任务,可以在隔离环境中运行 Skill:
--- name: deep-research description: Research a topic thoroughly context: fork agent: Explore --- Research $ARGUMENTS thoroughly: 1. Find relevant files using Glob and Grep 2. Read and analyze the code 3. Summarize findings with specific file references
实用 Skill 示例
1. 代码审查 Skill
--- name: code-review description: Perform thorough code review with security and performance focus disable-model-invocation: true --- Review the following code for: 1. **Security issues**: SQL injection, XSS, auth bypass 2. **Performance**: N+1 queries, inefficient algorithms 3. **Best practices**: Error handling, logging, documentation 4. **Code quality**: DRY principle, naming conventions Provide specific line-by-line feedback with suggestions.
2. 文档生成 Skill
--- name: generate-docs description: Generate comprehensive documentation from code --- Create documentation that includes: - **Overview**: What does this module/component do? - **API Reference**: Parameters, return types, examples - **Usage Guide**: Common patterns and gotchas - **Dependencies**: External libraries and versions Format as Markdown with proper headers and code blocks.
安装官方 Skills
除了创建自己的 Skills,还可以安装官方提供的 Skills:
# 添加 Skills 市场 /plugin marketplace add anthropics/skills # 安装文档处理 Skills /plugin install document-skills@anthropic-agent-skills # 安装示例 Skills /plugin install example-skills@anthropic-agent-skills
故障排除
1. Skill 未触发
- 检查
description是否包含用户会自然使用的关键词 - 验证 Skill 是否出现在
/skills列表中 - 尝试更精确地表述请求
2. Skill 触发过于频繁
- 使
description更具体 - 添加
disable-model-invocation: true限制自动调用
最佳实践
- 保持简洁:
SKILL.md控制在 500 行以内 - 明确描述:
description要具体说明使用场景 - 合理分组:复杂功能拆分为多个相关 Skills
- 版本控制:项目 Skills 提交到 Git 便于团队共享
- 安全考虑:对有副作用的操作设置
disable-model-invocation: true
通过合理使用 Skills,你可以将 Claude Code 从一个通用的 AI 编码助手转变为专门针对你工作流程的智能代理。现在就开始创建你的第一个 Skill 吧!
