学习中心/Claude Code/Skills 与自定义命令
中级

Skills 与自定义命令

学会创建和使用 Skills,为 Claude Code 添加可复用的自定义工作流

Anthropic25 min

学习笔记

Skills 与自定义命令

Skills 是 Claude Code 的扩展能力——可以是参考资料(如 API 风格指南),也可以是可调用的工作流(如 /deploy/fix-issue)。通过自定义 Skills,你可以将常用的操作封装为一条命令,大幅提升效率。

你将学到

  • Skills 的工作原理和加载机制
  • 如何创建自定义 Skill(SKILL.md)
  • 内置 Skills 的使用
  • 参数传递和高级配置
  • 在子代理中使用 Skills

适合谁

  • 想要定制 Claude Code 工作流的开发者
  • 希望在团队中共享标准化操作的技术负责人

1. 什么是 Skills

Skills 是 Claude 工具箱中的额外能力。每个 Skill 是一个 Markdown 文件,包含两部分:

  1. YAML Frontmatter--- 之间):告诉 Claude 何时使用该 Skill
  2. Markdown 正文:Claude 调用该 Skill 时遵循的指令

Skill 存放位置

位置 说明
.claude/skills/ 项目级,通过 Git 共享
~/.claude/skills/ 个人全局,跨所有项目

2. 创建你的第一个 Skill

示例:代码解释 Skill

创建 .claude/skills/explain-code/SKILL.md

---
name: explain-code
description: Explains code with visual diagrams and analogies. Use when explaining how code works.
---

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 or relationships
3. **Walk through the code**: Explain step-by-step what happens
4. **Highlight a gotcha**: What's a common mistake or misconception?

Keep explanations conversational.

使用:输入 /explain-code 或当你问"这段代码怎么工作的"时,Claude 会自动加载。


3. Frontmatter 配置项

字段 说明 示例
name 斜杠命令名称 fix-issue/fix-issue
description 帮助 Claude 判断何时自动加载 描述 Skill 的用途
disable-model-invocation true = 仅用户可触发 有副作用的操作(如部署)
user-invocable false = 仅 Claude 可触发 背景知识类 Skill
context fork = 在子代理中运行 不污染主对话上下文
agent 指定运行的代理类型 ExplorePlan、自定义

4. 参数传递

基本参数

使用 $ARGUMENTS 接收用户输入:

---
name: fix-issue
description: Fix a GitHub issue
disable-model-invocation: true
---

Fix GitHub issue $ARGUMENTS following our coding standards.

1. Use `gh issue view` to get details
2. Understand the requirements
3. Implement the fix
4. Write tests
5. Create a commit

使用:/fix-issue 123

位置参数

支持 $ARGUMENTS[0]$ARGUMENTS[1] 或简写 $0$1

---
name: migrate-component
description: Migrate a component from one framework to another
---

Migrate the $0 component from $1 to $2.
Preserve all existing behavior and tests.

使用:/migrate-component UserForm React Vue


5. 实用 Skill 示例

部署 Skill(手动触发)

---
name: deploy
description: Deploy the application to production
context: fork
disable-model-invocation: true
---

Deploy the application:
1. Run the test suite
2. Build the application
3. Push to the deployment target
4. Verify the deployment is healthy

PR Review Skill

---
name: review-pr
description: Review a pull request for code quality and issues
disable-model-invocation: true
---

Review PR $ARGUMENTS:

1. Read all changed files
2. Check for:
   - Security vulnerabilities
   - Performance issues
   - Missing error handling
   - Test coverage
3. Provide specific, actionable feedback

安全审计 Skill

---
name: security-audit
description: Run security audit on the codebase
context: fork
disable-model-invocation: true
---

Perform a security audit:
1. Check for hardcoded secrets or credentials
2. Review input validation (SQL injection, XSS, command injection)
3. Check authentication and authorization logic
4. Review dependency versions for known vulnerabilities
5. Report findings with severity levels and fix suggestions

6. 内置 Skills

Claude Code 自带的 Skills,开箱即用:

Skill 用途
/simplify 审查已修改代码的质量和效率,自动修复问题
/batch 大规模并行修改(多文件批量变更)

7. Skills 加载机制

理解加载机制有助于优化上下文使用:

  • 默认行为:Skill 的 description 在会话开始时加载(很轻量),完整内容在使用时才加载
  • 用户专属 Skillsdisable-model-invocation: true):调用前不加载任何内容,零上下文开销
  • Claude 选择:根据当前任务匹配 Skill description 决定加载哪些

在子代理中使用

  • Skills 传给子代理时会完整预加载到其上下文中
  • 子代理不继承主会话的 Skills
  • 必须在 Skill 定义中或调用时显式指定

8. 团队协作

共享 Skills

.claude/skills/ 提交到 Git,团队成员自动获得:

.claude/
└── skills/
    ├── fix-issue/
    │   └── SKILL.md
    ├── deploy/
    │   └── SKILL.md
    └── review-pr/
        └── SKILL.md

Skill vs CLAUDE.md

需求 放在哪里
所有任务都要遵循的规则 CLAUDE.md
特定场景才需要的工作流 Skills
偶尔使用的复杂操作 Skills(disable-model-invocation: true
背景知识参考 Skills(user-invocable: false

小结

  • Skills 是 Claude Code 的可复用扩展能力
  • 通过 SKILL.md 定义 Skill,支持参数传递和多种配置
  • disable-model-invocation: true 确保有副作用的 Skill 只在手动触发时执行
  • 将 Skills 提交到 Git 实现团队共享
  • 偶尔使用的工作流放 Skills,高频规则放 CLAUDE.md

延伸阅读

内容来源:Anthropic 官方文档