跳过正文
Claude Code教程系列:命令- commands
  1. Posts/

Claude Code教程系列:命令- commands

·2620 字·6 分钟

命令(commands)是claude code中最基础的交互方式,用户通过 /命令名快速调用预定义功能。claude提供了超60+内置命令,也支持用户自定义。

内置命令
#

内置命令覆盖了绝大多数使用场景与功能。在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时,使用技能版本。

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

代码优化
#

---
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

PR准备
#

---
description: Clean up code, stage changes, and prepare a pull request
allowed-tools: Bash(git add:*), Bash(git status:*), Bash(git diff:*), Bash(npm test:*), Bash(npm run lint:*)
---

# Pull Request Preparation Checklist

Before creating a PR, execute these steps:

1. Run linting: `prettier --write .`
2. Run tests: `npm test`
3. Review git diff: `git diff HEAD`
4. Stage changes: `git add .`
5. Create commit message following conventional commits:
   - `fix:` for bug fixes
   - `feat:` for new features
   - `docs:` for documentation
   - `refactor:` for code restructuring
   - `test:` for test additions
   - `chore:` for maintenance
6. Generate PR summary including:
   - What changed
   - Why it changed
   - Testing performed
   - Potential impacts

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

代码提交
#

---
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

代码推送
#

---
description: Stage all changes, create commit, and push to remote (use with caution)
allowed-tools: Bash(git add:*), Bash(git status:*), Bash(git commit:*), Bash(git push:*), Bash(git diff:*), Bash(git log:*), Bash(git pull:*)
---

# Commit and Push Everything

CAUTION: Stage ALL changes, commit, and push to remote. Use only when confident all changes belong together.

## Workflow

### 1. Analyze Changes
Run in parallel:
- `git status` - Show modified/added/deleted/untracked files
- `git diff --stat` - Show change statistics
- `git log -1 --oneline` - Show recent commit for message style

### 2. Safety Checks

STOP and WARN if detected:
- Secrets: `.env*`, `*.key`, `*.pem`, `credentials.json`, `secrets.yaml`, `id_rsa`, `*.p12`, `*.pfx`, `*.cer`
- API Keys: Any `*_API_KEY`, `*_SECRET`, `*_TOKEN` variables with real values (not placeholders like `your-api-key`, `xxx`, `placeholder`)
- Large files: >10MB without Git LFS
- Build artifacts: `node_modules/`, `dist/`, `build/`, `__pycache__/`, `*.pyc`, `.venv/`
- Temp files: `.DS_Store`, `thumbs.db`, `*.swp`, `*.tmp`

Verify:
- `.gitignore` properly configured
- No merge conflicts
- Correct branch (warn if main/master)
- API keys are placeholders only

### 3. Request Confirmation
Present summary and wait for explicit "yes" before proceeding.

### 4. Execute (After Confirmation)
Run sequentially:
  git add .
  git status  # Verify staging

文档重构
#

---
name: Documentation Refactor
description: Restructure project documentation for clarity and accessibility
tags: documentation, refactoring, organization
---

# Documentation Refactor

Refactor project documentation structure adapted to project type:

1. **Analyze project**: Identify type (library/API/web app/CLI/microservices), architecture, and user personas
2. **Centralize docs**: Move technical documentation to `docs/` with proper cross-references
3. **Root README.md**: Streamline as entry point with overview, quickstart, modules/components summary, license, contacts
4. **Component docs**: Add module/package/service-level README files with setup and testing instructions
5. **Organize `docs/`** by relevant categories:
   - Architecture, API Reference, Database, Design, Troubleshooting, Deployment, Contributing (adapt to project needs)
6. **Create guides** (select applicable):
   - User Guide: End-user documentation for applications
   - API Documentation: Endpoints, authentication, examples for APIs
   - Development Guide: Setup, testing, contribution workflow
   - Deployment Guide: Production deployment for services/apps
7. **Use Mermaid** for all diagrams (architecture, flows, schemas)

Keep docs concise, scannable, and contextual to project type.

CI/CD
#

---
name: Setup CI/CD Pipeline
description: Implement pre-commit hooks and GitHub Actions for quality assurance
tags: ci-cd, devops, automation
---

# Setup CI/CD Pipeline

Implement comprehensive DevOps quality gates adapted to project type:

1. **Analyze project**: Detect language(s), framework, build system, and existing tooling
2. **Configure pre-commit hooks** with language-specific tools:
   - Formatting: Prettier/Black/gofmt/rustfmt/etc.
   - Linting: ESLint/Ruff/golangci-lint/Clippy/etc.
   - Security: Bandit/gosec/cargo-audit/npm audit/etc.
   - Type checking: TypeScript/mypy/flow (if applicable)
   - Tests: Run relevant test suites
3. **Create GitHub Actions workflows** (.github/workflows/):
   - Mirror pre-commit checks on push/PR
   - Multi-version/platform matrix (if applicable)
   - Build and test verification
   - Deployment steps (if needed)
4. **Verify pipeline**: Test locally, create test PR, confirm all checks pass

Use free/open-source tools. Respect existing configs. Keep execution fast.

参考链接
#