Slash命令与Skills:快速构建工作流自动化
摘要:Slash命令与Skills工作流自动化 核心价值:Slash命令和Skills能显著提升工作效率,通过预定义工作流模板将重复任务执行时间从15-30分钟缩短至1-2分钟,年均可节省112小时。 关键区别: Slash命令:快捷菜单式调用(/.claude/commands/) Skills:可复用工作流模板(.claude/skills/) 典型应用场景: 代码审查自动化(/review)
Slash命令与Skills:快速构建工作流自动化
核心观点:Slash命令和Skills是Claude Code中最容易被忽视但最高效的功能。一套精心设计的Slash命令库可以将日常任务的成本降低70%。
关键词:Slash命令、Skills、工作流自动化、自定义命令、重复任务优化、团队协作
导读
你将学到:
- Slash命令与Skills的本质区别和联系
- 如何创建自定义Slash命令
- 如何创建可复用的Skills
- 参数化命令的高级用法
- 构建个人和团队命令库
- 常见的自动化模式
- 性能优化和最佳实践
适合人群:中级到高级开发者,想要优化日常工作流的使用者
阅读时间:22分钟 | 难度:中级 | 实用度:5/5
前置知识:
- 已阅读本系列前5篇文章
- 理解Markdown语法
- 熟悉YAML配置格式
问题场景
你发现自己每天重复做相同的任务:
- 创建新功能分支时,总是要重复解释项目结构
- 做代码审查时,总是要列举相同的检查项
- 部署前总是要运行相同的测试命令
- 创建bug修复分支时,总是要说明相同的上下文
你想:“能不能把这些常见任务自动化,只需要一个命令就能执行?”
这正是Slash命令和Skills的用途。
为什么这很重要?
总时间节省 = 每个任务节省时间 × 每周重复次数 × 52 周
例如:
- 代码审查任务:30分钟节省 × 2次/周 × 52周 = 52小时/年
- 创建功能分支:10分钟节省 × 5次/周 × 52周 = 43小时/年
- 部署验证:20分钟节省 × 1次/周 × 52周 = 17小时/年
总计:112小时/年 = 2.7周的工作时间
核心概念
Slash命令 vs Skills
这两个概念经常混淆。让我们澄清:
| 特性 | Slash命令 | Skills |
|---|---|---|
| 定义方式 | 命令文件 | 技能文件 |
| 触发方式 | /命令名 |
/命令名 或自动 |
| 定义位置 | .claude/commands/ |
.claude/skills/ |
| 主要用途 | 快速访问常用任务 | 提示模板和工作流 |
| 是否需要指定目标 | 通常需要 | 通常由Claude执行 |
| 可共享性 | 高(Git管理) | 高(Git管理) |
| 学习曲线 | 平缓 | 中等 |
简单理解:
- Slash命令 = 快捷菜单
- Skills = 工作流模板
为什么很强大
没有命令库:
每次都要重新表述需求
Claude重新分析上下文
重复的工作流设计
平均每个任务:15-30分钟
有命令库:
一个命令调用
Claude已知道流程
自动执行标准工作流
平均每个任务:1-2分钟
效率提升:10-15倍
Slash命令详解
什么是Slash命令?
Slash命令是以 / 开头的快捷调用,让你快速访问预定义的工作流。
基本结构:
.claude/commands/
├── review.md # /review 代码审查
├── deploy.md # /deploy 部署
├── create-feature.md # /create-feature 创建功能
└── test.md # /test 运行测试
创建Slash命令
最简单的命令
# .claude/commands/review.md
---
name: review
description: Review code for quality and best practices
---
Review the recent code changes for:
- Code quality and readability
- Performance issues
- Security vulnerabilities
- Best practices violations
Provide actionable feedback with priority levels.
使用方法:
claude
你:/review
# Claude会审查最近的改动
带参数的命令
# .claude/commands/fix-issue.md
---
name: fix-issue
description: Fix a GitHub issue
---
Fix GitHub issue $ARGUMENTS following these steps:
1. Understand the issue requirements
2. Analyze the related code
3. Implement the fix
4. Add tests if applicable
5. Verify the fix works
Current issue: $ARGUMENTS
使用方法:
你:/fix-issue 123
# Claude会修复issue #123
复杂命令:带条件和多步骤
# .claude/commands/prepare-release.md
---
name: prepare-release
description: Prepare a new release
disable-model-invocation: false
---
Prepare a new release for version $ARGUMENTS.
Steps:
1. Update version number
- package.json version field
- CHANGELOG.md
2. Run quality checks
- npm run lint
- npm run test
- npm run build
3. Create release commit
- git tag v$ARGUMENTS
- Create release notes
4. Prepare deployment checklist
- Pre-deployment checks
- Post-deployment verification
Version: $ARGUMENTS
使用方法:
你:/prepare-release 2.0.0
# Claude会指导整个发布流程
命令配置选项
---
name: command-name # 命令名(必需)
description: Command description # 描述
disable-model-invocation: false # 是否让Claude执行
user-invocable: true # 是否允许用户手动调用
model: inherit # 使用的模型(继承默认)
---
命令内容...
实战命令库
以下是一个完整团队的命令库设计:
命令1:功能开发模板
# .claude/commands/start-feature.md
---
name: start-feature
description: Start developing a new feature
---
Starting a new feature development. Follow these steps:
1. Create feature branch
````bash
git checkout -b feature/$ARGUMENTS
-
Update CLAUDE.md if needed
- Add any new context about this feature
-
Begin development
- Reference CLAUDE.md for coding standards
- Write tests first (TDD approach)
- Commit frequently
-
Before creating PR
- Run npm run lint
- Run npm run test
- Run npm run build
- Update CHANGELOG.md
Feature name: $ARGUMENTS
#### 命令2:代码审查模板
```markdown
# .claude/commands/code-review.md
---
name: code-review
description: Perform comprehensive code review
---
Reviewing code changes with the following checklist:
Functionality:
- Does code implement the intended feature?
- Are all edge cases handled?
- Is error handling appropriate?
Quality:
- Follows CLAUDE.md standards?
- Code is readable and maintainable?
- No obvious performance issues?
- Test coverage adequate?
Security:
- No SQL injection vulnerabilities?
- No XSS vulnerabilities?
- Authentication/authorization correct?
- Sensitive data properly handled?
Architecture:
- Consistent with project architecture?
- Not introducing technical debt?
- Appropriate abstractions?
Provide structured feedback with priority levels:
- CRITICAL: Must fix before merge
- IMPORTANT: Should fix
- SUGGESTION: Consider for future improvement
命令3:部署前检查
# .claude/commands/pre-deploy.md
---
name: pre-deploy
description: Verify code is ready for deployment
disable-model-invocation: true
---
Pre-deployment verification checklist:
1. Code Quality
- npm run lint (no errors/warnings)
- npm run type-check (no type errors)
- npm run test (all tests pass)
2. Build Verification
- npm run build (builds successfully)
- Build output size reasonable?
3. Security Check
- npm audit (no vulnerabilities)
- Dependencies up-to-date?
4. Git Status
- All changes committed?
- Branch is up-to-date with main?
- Ready for merge?
5. Release Notes
- CHANGELOG.md updated?
- Version number updated?
6. Final Approval
- Code owner approved?
- Tests automated?
- Rollback plan documented?
Once all checks pass, ready to deploy!
Skills详解
什么是Skill?
Skill是一个更复杂的工作流定义,包含完整的提示、工具链和执行步骤。
基本结构:
.claude/skills/
├── code-review/
│ └── SKILL.md
├── security-analysis/
│ └── SKILL.md
└── performance-optimization/
└── SKILL.md
创建Skill
基础Skill模板
# .claude/skills/code-review/SKILL.md
---
name: code-review
description: Comprehensive code review specializing in quality and best practices
category: development
allowed-tools: Read, Grep, Glob, Bash
---
You are a senior code reviewer specializing in code quality, maintainability, and best practices.
When reviewing code:
1. Analyze the code changes
- What problem does it solve?
- What's the approach?
2. Check code quality
- Readability
- Maintainability
- Adherence to standards
3. Identify issues
- Bugs or logic errors
- Performance problems
- Security issues
4. Provide structured feedback
- Critical issues (must fix)
- Important issues (should fix)
- Suggestions (nice to have)
Review the code thoroughly and provide actionable feedback.
高级Skill:带参数和条件
# .claude/skills/security-analysis/SKILL.md
---
name: security-analysis
description: Security-focused code analysis
category: security
allowed-tools: Read, Grep, Glob
context: fork
---
You are a security expert analyzing code for vulnerabilities.
Focus areas:
- Authentication and authorization
- Input validation
- SQL injection risks
- XSS vulnerabilities
- Data exposure
- Cryptography usage
Analysis process:
1. Identify security-sensitive components
2. Trace data flow
3. Identify potential vulnerabilities
4. Assess impact and likelihood
5. Recommend mitigations
Provide clear security assessment with:
- Vulnerability description
- Severity level (Critical/High/Medium/Low)
- Affected code location
- Recommended fix
Skill自动加载
# .claude/skills/perf-optimizer/SKILL.md
---
name: perf-optimizer
description: Performance optimization specialist
allowed-tools: Read, Bash, Grep
context: fork
---
You are a performance optimization specialist.
When analyzing performance:
1. Profile the code
- Identify bottlenecks
- Find hot paths
- Measure current metrics
2. Propose optimizations
- Expected improvement
- Implementation complexity
- Trade-offs
3. Provide implementation
- Code changes
- Verification steps
- Testing approach
Optimization priorities:
1. Algorithmic improvements
2. Caching strategies
3. Resource utilization
4. Parallelization
参数化和高级用法
参数传递
单参数
# .claude/commands/create-api-endpoint.md
---
name: create-api-endpoint
description: Create a new API endpoint
---
Create a new $ARGUMENTS endpoint following project standards.
Include:
- Route definition
- Request validation
- Response formatting
- Error handling
- Unit tests
- API documentation
Endpoint: $ARGUMENTS
使用:
你:/create-api-endpoint /api/users/profile
多参数
# .claude/commands/migrate-db.md
---
name: migrate-db
description: Create database migration
---
Create database migration: $ARGUMENTS
Migration type: (first argument)
- add-column
- create-table
- modify-constraint
Table/Column: (second argument)
Example:
/migrate-db add-column users email
使用:
你:/migrate-db add-column users email
条件执行
# .claude/commands/full-deploy.md
---
name: full-deploy
description: Complete deployment with validation
disable-model-invocation: false
---
Deploying version $ARGUMENTS with full validation.
Pre-deployment:
- Run all tests
- Code quality checks
- Security scan
Deployment:
- Build production bundle
- Upload to CDN
- Update DNS if needed
Post-deployment:
- Health checks
- Performance monitoring
- Rollback procedure
Version: $ARGUMENTS
命令和Skill的组织策略
按功能分类
.claude/commands/
├── development/
│ ├── start-feature.md
│ ├── create-component.md
│ └── setup-environment.md
├── review/
│ ├── code-review.md
│ ├── security-review.md
│ └── performance-review.md
├── deployment/
│ ├── pre-deploy.md
│ ├── deploy.md
│ └── post-deploy.md
└── maintenance/
├── update-dependencies.md
├── cleanup.md
└── refactor.md
按难度分类
简易命令(适合新手):
- /help - 获取帮助
- /test - 运行测试
- /lint - 代码检查
中等命令(需要理解):
- /code-review - 代码审查
- /create-feature - 创建功能
- /deploy - 部署
高级命令(需要专业知识):
- /refactor - 重构
- /optimize - 优化
- /migrate - 迁移
实战模式
模式1:开发工作流链
/start-feature
→ 创建功能分支
/code-review
→ 在完成前自审
/pre-deploy
→ 部署前检查
/deploy
→ 发布到生产
模式2:问题诊断链
/analyze-performance
→ 找出性能瓶颈
/profile-code
→ 详细的性能分析
/optimize
→ 生成优化建议
/implement-optimization
→ 实施优化
模式3:安全审查链
/security-scan
→ 扫描已知漏洞
/security-review
→ 手工安全审查
/fix-vulnerabilities
→ 修复发现的问题
/verify-fixes
→ 验证修复
团队协作
共享命令库
方式1:Git管理(推荐)
# 项目根目录
.claude/
├── commands/ # 团队共享的命令
├── skills/ # 团队共享的技能
└── CLAUDE.md # 项目配置
# 在Git中提交
git add .claude/
git commit -m "chore: add code review command"
git push
# 其他团队成员
git pull # 自动获得新命令
/code-review # 立即可用
方式2:全局命令库
# 放在用户主目录
~/.claude/skills/
├── my-code-review/
├── my-deploy/
└── my-optimize/
# 对所有项目可用
/my-code-review
方式3:混合方式
项目级别命令 (.claude/commands/)
- 项目特定的工作流
- 与Git同步
- 团队共享
全局Skill (~/.claude/skills/)
- 通用的分析工具
- 个人定制
- 跨项目使用
权限管理
# 基础权限级别
开发者可用命令:
- /start-feature
- /code-review(自审)
- /test
- /lint
审查员额外权限:
- /approve-pr
- /security-review
- /performance-review
部署权限(仅限):
- /pre-deploy
- /deploy
- /post-deploy
- /rollback
高级技巧
技巧1:命令中调用其他命令
# .claude/commands/full-feature-dev.md
---
name: full-feature-dev
description: Complete feature development workflow
disable-model-invocation: false
---
Full feature development for $ARGUMENTS:
Step 1: Start feature branch
/start-feature $ARGUMENTS
Step 2: Develop feature
(You will implement the feature)
Step 3: Review your work
/code-review
Step 4: Prepare for deployment
/pre-deploy
Ready for production!
技巧2:动态提示注入
# .claude/commands/analyze.md
---
name: analyze
description: Analyze code with dynamic context
---
Analyzing $ARGUMENTS using context from CLAUDE.md:
Current standards:
!`cat CLAUDE.md | grep "编码标准" -A 10`
Project structure:
!`find . -maxdepth 2 -type d | head -20`
Analyze the code and provide insights based on project context.
技巧3:条件Skill选择
基于项目类型自动选择Skill:
# .claude/commands/review.md
---
name: review
description: Review code (auto-selects reviewer based on project)
---
Detect project type...
If Python project:
Use /python-code-review
If JavaScript project:
Use /js-code-review
If Go project:
Use /go-code-review
Otherwise:
Use /generic-code-review
常见模式和最佳实践
最佳实践1:明确的命令目的
不好:
/do-stuff # 太模糊
好的:
/code-review # 清晰的目的
/security-audit # 具体的功能
/perf-optimize # 明确的输出
最佳实践2:参数验证
# .claude/commands/create-endpoint.md
---
name: create-endpoint
description: Create API endpoint
---
Creating endpoint: $ARGUMENTS
Validation:
- Endpoint path provided? (e.g., /api/users)
- Method specified? (GET/POST/PUT/DELETE)
Proceed with endpoint creation...
最佳实践3:文档化
# .claude/commands/deploy.md
---
name: deploy
description: Deploy to production
---
Deploy command usage:
Example:
/deploy v1.2.0
Prerequisites:
- All tests passing
- Code reviewed
- CHANGELOG updated
Output:
- Deployment status
- Rollback instructions
- Health check results
最佳实践4:错误处理
# .claude/commands/backup-db.md
---
name: backup-db
description: Backup database safely
---
Database backup procedure:
1. Pre-check
- Is database accessible?
- Is disk space sufficient?
- Are permissions correct?
2. Backup
- Create backup file
- Verify backup integrity
- Store securely
3. Verification
- Test restore from backup
- Confirm data integrity
- Log backup details
If any step fails, provide detailed error message.
与其他Claude Code功能的集成
与Plan Mode的结合
# 1. 先用Plan Mode规划
claude --plan
你:我需要重构认证系统。请规划。
Claude:[生成详细计划]
# 2. 创建对应的Skill
.claude/skills/auth-refactor/SKILL.md
(基于Plan Mode的计划)
# 3. 用Skill执行
claude
你:/auth-refactor
与Hooks的结合
# .claude/settings.json
{
"hooks": {
"PostCommandExecution": [
{
"command": "/code-review",
"action": "log",
"file": ".claude/review-history.log"
}
]
}
}
与MCP的结合
# .claude/commands/github-review.md
---
name: github-review
description: Review GitHub PR
---
使用GitHub MCP获取PR信息:
PR信息:
!`gh pr view $ARGUMENTS --json title,body,files`
执行代码审查...
性能优化
缓存优化
# .claude/commands/test-quick.md
---
name: test-quick
description: Run only changed tests
---
Quick test run (only changed files):
1. Detect changed files
2. Run related tests only
3. Report results
Much faster than full test suite!
成本优化
# .claude/commands/lint-check.md
---
name: lint-check
description: Local lint check (no Claude cost)
disable-model-invocation: true
---
Run local linting:
```bash
npm run lint
No Claude execution, instant feedback!
总结与要点
命令库的收益
| 投入 | 收益 | 回报周期 |
|---|---|---|
| 创建5个命令(10小时) | 每个任务节省15分钟 | 2-3周 |
| 创建Skill库(20小时) | 每个任务节省30分钟 | 3-4周 |
| 团队命令库(50小时) | 团队整体效率提升40% | 2个月 |
一句话总结
精心设计的Slash命令和Skill库是团队生产力的倍增器——一次性投入,终身受益。
下一步行动
- 立即:为你最常做的任务创建3个Slash命令
- 这周:建立个人Skill库(至少2个)
- 下周:与团队分享,收集反馈
- 持续:持续扩展和改进命令库
推荐阅读
本系列相关文章
- 上一篇:Plan Mode - 安全的探索
- 下一篇:Hooks - 事件驱动的自动化
- 后续:Subagents - 专业化的AI助手
官方资源
- Claude Code文档 - Commands: https://code.claude.com/docs
- Skills最佳实践: https://code.claude.com/docs/skills
社区资源
- GitHub: awesome-claude-code-commands
- Skill模板库: https://github.com/claude-code-skills
常见问题
Q: Slash命令和Skill有什么区别?
A: Slash命令是快捷调用,Skills是完整的工作流。命令用于快速任务,Skills用于复杂流程。
Q: 可以在命令中使用参数吗?
A: 可以,使用 $ARGUMENTS 来接收参数。例如:/create-api $ARGUMENTS
Q: 命令库应该提交到Git吗?
A: 是的。团队共享的命令应该提交到Git,确保所有人都使用最新版本。
Q: 如何在全局和项目级别共享命令?
A: 全局命令放在 ~/.claude/skills/,项目命令放在 .claude/commands/。两者都会被加载。
Q: 命令库的大小有限制吗?
A: 没有硬性限制,但建议保持在50个命令以内,避免混乱。可以分类组织。
最后的话
Slash命令和Skills代表了一种哲学:系统化地消除重复工作。
每个开发者每天都在重复相同的工作。如果你能花30分钟设计一个命令,就能节省几个小时的重复工作,这笔投资总是值得的。
而且,好的命令库不仅能提升个人效率,更能帮助团队建立标准化的工作流。
恭喜!你现在已经掌握了Claude Code的个人效率优化工具。下一篇将讲解Hooks——在后台自动执行检查和验证,进一步提升整个开发流程的自动化水平。
更多推荐


所有评论(0)