跳过正文
claude code教程:commands
  1. Posts/

claude code教程:commands

·1983 字·4 分钟
目录

commands提供了调用claude预定义能力的快捷方式。

内置命令
#

内置命令覆盖了绝大多数使用场景与功能。在claude中输入 /查看完整列表,或输入 /后跟任何字母进行过滤。

命令用途
/add-dir <path>添加工作目录
/agents管理代理配置
/branch [name]将对话分支到新会话(别名:/fork)。注意:/fork在v2.1.77中重命名为 /branch
/btw <question>不添加到历史的旁注问题
/chrome配置Chrome浏览器集成
/clear清除对话(别名:/reset/new
/color [color|default]设置提示栏颜色
/compact [instructions]使用可选聚焦指令压缩对话
/config打开设置(别名:/settings
/context以彩色网格可视化上下文使用情况
/copy [N]将助手响应复制到剪贴板;w写入文件
/cost显示token使用统计
/desktop在桌面应用中继续(别名:/app
/diff未提交更改的交互式diff查看器
/doctor诊断安装健康状态
/effort [low|medium|high|xhigh|max|auto]通过交互式箭头键滑块设置努力级别。级别:lowmediumhighxhigh(v2.1.111新增)→ max。Opus 4.7默认为 xhighmax仅限Opus 4.7
/exit退出REPL(别名:/quit
/export [filename]将当前对话导出到文件或剪贴板
/extra-usage配置速率限制的额外使用
/fast [on|off]切换快速模式
/feedback提交反馈(别名:/bug
/focus切换焦点视图(v2.1.110新增;替换 Ctrl+O用于焦点切换)
/help显示帮助
/hooks查看钩子配置
/ide管理IDE集成
/init初始化 CLAUDE.md。设置 CLAUDE_CODE_NEW_INIT=1以启用交互式流程
/insights生成会话分析报告
/install-github-app设置GitHub Actions应用
/install-slack-app安装Slack应用
/keybindings打开键绑定配置
/less-permission-prompts分析最近的Bash/MCP工具调用并将优先级允许列表添加到 .claude/settings.json以减少权限提示(v2.1.111新增)
/login切换Anthropic账户
/logout退出Anthropic账户
/mcp管理MCP服务器和OAuth
/memory编辑 CLAUDE.md,切换自动内存
/mobile移动应用的二维码(别名:/ios/android
/model [model]使用左右箭头选择模型以调整努力
/passes分享Claude Code免费周
/permissions查看/更新权限(别名:/allowed-tools
/plan [description]进入规划模式
/plugin管理插件
/proactive/loop的别名(v2.1.105新增)
/powerup通过带动画演示的交互式课程发现功能
/privacy-settings隐私设置(仅Pro/Max)
/release-notes查看更新日志
/recap返回会话时显示会话回顾/摘要(v2.1.108新增)
/reload-plugins重新加载活动插件
/remote-control从claude.ai远程控制(别名:/rc
/remote-env配置默认远程环境
/rename [name]重命名会话
/resume [session]恢复对话(别名:/continue
/review已弃用 — 安装 code-review插件代替
/rewind回溯对话和/或代码(别名:/checkpoint
/sandbox切换沙箱模式
/schedule [description]创建/管理云端计划任务
/security-review分析分支的安全漏洞
/skills列出可用技能
/stats可视化每日使用情况、会话、连续天数
/stickers订购Claude Code贴纸
/status显示版本、模型、账户
/statusline配置状态栏
/tasks列出/管理后台任务
/team-onboarding从项目的Claude Code设置生成队友入门指南(v2.1.101新增)
/terminal-setup配置终端键绑定
/theme更改颜色主题
/tui切换全屏TUI(文本用户界面)模式,无闪烁渲染(v2.1.110新增)
/ultraplan <prompt>在ultraplan会话中起草计划,在浏览器中审查
/ultrareview基于云的多代理分析全面代码审查(v2.1.111新增)
/undo/rewind的别名(v2.1.108新增)
/upgrade打开升级页面以获得更高计划层级
/usage显示计划使用限制和速率限制状态
/voice切换按住说话语音听询

自定义命令
#

最新版本Claude Code已经将commands合并到skills中,具体细节可参考 claude code教程:skills

方法位置状态
技能(推荐).claude/skills/<name>/SKILL.md当前标准
传统命令.claude/commands/<name>.md仍然工作

如果技能和命令共享相同名称,技能优先。例如,当同时存在 .claude/commands/review.md.claude/skills/review/SKILL.md时,使用技能版本。

以下几个示例将从格式约束、内容结构上展示如何创建自定义命令。

示例1:代码优化
#

---
description: Analyze code for performance issues and suggest optimizations
---

# Code Optimization

Review the provided code for the following issues in order of priority:

1. **Performance bottlenecks** - identify O(n²) operations, inefficient loops
2. **Memory leaks** - find unreleased resources, circular references
3. **Algorithm improvements** - suggest better algorithms or data structures
4. **Caching opportunities** - identify repeated computations
5. **Concurrency issues** - find race conditions or threading problems

Format your response with:
- Issue severity (Critical/High/Medium/Low)
- Location in code
- Explanation
- Recommended fix with code example

示例2:API文档生成
#

---
description: Create comprehensive API documentation from source code
---

# API Documentation Generator

Generate API documentation by:

1. Scanning all files in `/src/api/`
2. Extracting function signatures and JSDoc comments
3. Organizing by endpoint/module
4. Creating markdown with examples
5. Including request/response schemas
6. Adding error documentation

Output format:
- Markdown file in `/docs/api.md`
- Include curl examples for all endpoints
- Add TypeScript types

示例3:代码提交
#

---
description: Create git commit with dynamic context from repository
allowed-tools: Bash(git add:*), Bash(git commit:*), Bash(git status:*), Bash(git diff:*), Bash(git log:*)
---

# Git Commit with Context

Create a git commit with relevant repository context.

## Steps

1. Run in parallel to gather context:
   - `git status` - show working tree status
   - `git diff` - show both staged and unstaged changes
   - `git log -1 --oneline` - show recent commit message

2. Analyze all staged and newly added changes

3. Draft a concise commit message:
   - Summarize the nature of the changes (eg. new feature, enhancement to an existing feature, bug fix, refactoring, test, docs, etc.)
   - Ensure the message accurately reflects the changes and their purpose (i.e. "add" means a wholly new feature, "update" means an enhancement to an existing feature, "fix" means a bug fix, etc.)
   - Do not commit files that likely contain secrets (.env, credentials.json, etc). Warn the user if they specifically request to commit those files
   - Draft a concise (1-2 sentences) commit message that focuses on the "why" rather than the "what"
   - Ensure it accurately reflects the changes and their purpose

4. Run sequentially:
   - Add relevant untracked files
   - Create commit with message
   - Run git status after commit to verify success

参考链接
#

claude-howto