关于 slash-commands
面向 Claude Code 用户的斜杠命令参考技能,介绍如何创建和使用用户触发的提示词,并说明命令文件、参数、Bash 执行、文件引用及其与 skills 的区别。
主要功能
- 01内置命令参考
- 02项目和个人命令设置
- 03YAML 前置元数据与参数
- 04在提示词中执行 Bash
- 05文件引用与命名空间
- 06MCP 服务提供的命令
适用场景
- 01创建可复用的项目或个人提示词
- 02为命令添加参数和文件引用
- 03在加载提示词前运行 Bash 命令
- 04选择使用斜杠命令或 skills
MCP 相关
说明 MCP 服务可将提示词作为斜杠命令提供,并给出调用示例。
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ parcadei/ continuous-claude-v3 --skill slash-commands安装后在 Claude Code 中输入/slash-commands确认已加载。
详细介绍
斜杠命令参考
使用 /command-name 语法创建并使用由用户触发的提示词。
适用场景
- “如何创建斜杠命令?”
- “有哪些斜杠命令可用?”
- “在我的命令中添加 bash”
- “在命令中使用文件引用”
- “斜杠命令与技能有什么区别?”
内置命令
| 命令 | 用途 |
|---|---|
/clear | 清除对话历史记录 |
/compact | 聚焦并压缩对话 |
/config | 打开设置界面 |
/cost | 显示 Token 用量 |
/agents | 管理子 Agent |
/mcp | 管理 MCP 服务 |
/memory | 编辑 CLAUDE.md 文件 |
/model | 选择 AI 模型 |
/review | 请求代码审查 |
/resume | 恢复会话 |
/help | 获取使用帮助 |
创建命令
项目命令
mkdir -p .claude/commands
cat > .claude/commands/optimize.md << 'EOF'
---
description: Analyze code for performance issues
---
Review this code for:
- Performance bottlenecks
- Memory leaks
- Caching opportunities
EOF
个人命令
mkdir -p ~/.claude/commands
cat > ~/.claude/commands/review.md << 'EOF'
---
description: Security-focused code review
---
Check for vulnerabilities:
- Input validation
- SQL injection
- XSS risks
EOF
命令文件格式
---
description: Brief description for /help
allowed-tools: [Bash, Read, Write] # Optional
argument-hint: "[file] [type]" # Optional
---
Your markdown instructions here.
Use $1, $2 for arguments or $ARGUMENTS for all.
执行 Bash
在加载提示词前,使用 ! 前缀运行 bash:
---
allowed-tools: Bash(git:*), Bash(grep:*)
description: Git commit helper
---
Current status: !`git status`
Staged changes: !`git diff --staged`
Recent commits: !`git log --oneline -5`
Based on these changes, suggest a commit message.
规则:
- 必须在 frontmatter 中声明
allowed-tools: Bash(...) - 使用反引号:
!`command` - 输出会包含在 Claude 的上下文中
文件引用
使用 @ 前缀包含文件:
Review against @.claude/STYLE_GUIDE.md
Compare:
- @src/old.js
- @src/new.js
Refactor files matching @src/**/*.util.ts
参数
---
argument-hint: "[pr-number] [priority]"
---
Review PR #$1 with priority: $2
# Or use all arguments:
Fix issue #$ARGUMENTS
用法:
/review-pr 456 high
# $1 = "456", $2 = "high"
命名空间
使用子目录进行组织:
.claude/commands/
├── frontend/
│ └── component.md → /component (project:frontend)
└── backend/
└── endpoint.md → /endpoint (project:backend)
MCP 斜杠命令
MCP 服务会将提示词作为命令公开:
/mcp__github__list_prs
/mcp__github__pr_review 456
/mcp__jira__create_issue "Bug" high
斜杠命令与技能的区别
| 方面 | 斜杠命令 | 技能 |
|---|---|---|
| 调用方式 | 显式:/command | 自动发现 |
| 文件 | 单个 .md 文件 | 包含 SKILL.md 的目录 |
| 使用场景 | 快速提示词 | 复杂工作流 |
使用斜杠命令的场景: 经常输入的提示词、简单模板
使用技能的场景: 复杂工作流、多个文件、自动发现
示例:完整的 Git 提交命令
---
description: Generate semantic commit message
allowed-tools: Bash(git:*), Read
argument-hint: "[type]"
---
# Semantic Commit Generator
Staged files: !`git diff --name-only --cached`
Diff preview:
!`git diff --cached | head -100`
Generate a conventional commit message.
Type: $1 (feat/fix/docs/style/refactor/perf/test/chore)
Format: `<type>(<scope>): <subject>`
用法: /commit feat
评论
支持 Markdown