Claude代码备忘单-命令、快捷方式、提示和工作流模式
  
完整的参考文献 克劳德代码Anthropic的代理编码CLI。每个命令、键盘快捷键、CLI标志、配置选项和工作流模式都在一个地方。在Claude Code的最新版本上进行了测试和验证。
带有截图和扩展示例的整篇文章: computingforgeks.com/claude编码壳
______________________________________________________________________
目录
- 安装
- 键盘快捷键
- 斜杠命令
- CLI标志
- CLAUDE.md-项目说明
- 权限模式
- 模型和努力水平
- MCP服务器
- 定制技能
- 自定义子代理
- Hooks-自动化触发器
- 上下文窗口管理
- Git 工作树
- 设置文件参考
- 环境变量
- 文件结构参考
- 实用的工作流程技巧
- 提示提示
- 代币保存策略
- 诊断
- IDE集成
- 自动模式
- 自动化脚本
- 快速参考卡
- 子命令参考
______________________________________________________________________
安装
在macOS、Linux或Windows(WSL)上安装Claude Code:
curl -fsSL https://claude.ai/install.sh | bash在macOS上通过Homebrew:
brew install --cask claude-code在Windows(PowerShell)上:
irm https://claude.ai/install.ps1 | iex验证和身份验证:
claude --version
claude auth login______________________________________________________________________
键盘快捷键
这些在交互式Claude Code终端中工作。
基本快捷方式
| 快捷方式 | 操作 |
|---|---|
Ctrl+C | 取消当前生成或输入 |
Ctrl+D | 退出克劳德代码 |
Ctrl+L | 清除终端屏幕 |
Ctrl+R | 反向搜索命令历史记录 |
Esc + Esc | 倒退到上一个检查点(撤消) |
Shift+Tab | 循环权限模式(默认/计划/自动) |
Ctrl+G | 在外部编辑器(vim等)中打开输入 |
Ctrl+O | 切换详细输出 |
Ctrl+T | 切换任务列表 |
Ctrl+F | 杀死所有后台特工(按两次) |
输入和编辑
| 快捷方式 | 操作 |
|---|---|
\ + Enter | 新行(多行输入)-适用于macOS、Linux和Windows |
Option+Enter (Mac)/ Alt+Enter (Linux/Win) | 新行(平台默认) |
Shift+Enter | 新品(iTerm2、WezTerm、Ghostty、Kitty、Windows终端) |
Ctrl+K | 删除到行尾 |
Ctrl+U | 删除整行 |
Ctrl+Y | 粘贴已删除的文本 |
Alt+B / Alt+F | 将光标前后移动一个单词 |
Cmd+V (Mac)/ Ctrl+V (Linux/Win) | 从剪贴板粘贴图像 |
Up/Down | 浏览命令历史记录 |
型号和模式切换
| 快捷方式 | 操作 |
|---|---|
Option+P (Mac)/ Alt+P (Linux/Win) | 交换机型号(Sonnet/Opus/Haiku) |
Option+T (Mac)/ Alt+T (Linux/Win) | 切换扩展思维 |
Shift+Tab / Alt+M | 循环许可模式 |
? | 显示所有可用快捷方式 |
快速前缀
| 前缀 | 动作 |
|---|---|
/ | 打开斜线命令菜单 |
! | 直接运行bash命令 |
@ | 自动完成文件路径提及 |
______________________________________________________________________
斜杠命令
会话和上下文管理
| 命令 | 它做什么 |
|---|---|
/clear | 擦除对话历史记录和自由上下文窗口 |
/compact [focus] | 将对话总结为自由语境,可选择关注特定主题 |
/cost | 显示当前会话的令牌使用情况和API成本 |
/context | 可视化什么在消耗你的上下文窗口 |
/resume | 选择并继续上一个会话 |
/rename [name] | 为当前会话命名,以便以后轻松恢复 |
/rewind | 倒退到上一个检查点 |
/diff | 查看所有更改的交互式差异 |
/copy | 将上次响应复制到剪贴板 |
/export [file] | 将对话导出为文本文件 |
配置和型号
| 命令 | 它做什么 |
|---|---|
/model [name] | 切换模式(十四行诗、小品、俳句) |
/effort [level] | 设定推理努力(低、中、高、最大) |
/config | 打开设置界面 |
/permissions | 查看和管理工具权限 |
/mcp | 管理MCP服务器连接 |
/terminal-setup | 为您的shell配置终端键绑定 |
/theme | 更改颜色主题 |
/vim | 切换vim键绑定模式 |
项目和代码工具
| 命令 | 它做什么 |
|---|---|
/init | 初始化项目,使用项目上下文生成CLAUDE.md |
/memory | 查看和编辑CLAUDE.md文件和自动内存 |
/security-review | 扫描待定更改以查找安全漏洞 |
/simplify | 审查更改的代码是否存在质量问题,并提出改进建议 |
/pr-comments [PR] | 获取并处理GitHub PR评论 |
/add-dir [path] | 将另一个目录添加到工作上下文中 |
/plan | 进入计划模式,进行更改前进行只读探索 |
/batch [task] | 在5-30个并行git工作树上运行大规模更改 |
/debug [description] | 排除当前会话中的问题 |
系统和帐户
| 命令 | 它做什么 |
|---|---|
/help | 显示所有可用命令 |
/doctor | 诊断安装、设置、身份验证和MCP问题 |
/insights | 生成会话分析报告 |
/status | 显示版本、型号和帐户信息 |
/login / /logout | 登录或退出 |
/bug | 提交带有会话日志的错误报告 |
/stats | 查看使用模式、每日记录和会话历史 |
/usage | 显示计划使用情况和费率限制 |
/release-notes | 查看当前版本的更新日志 |
/loop [interval] [prompt] | 按定期计划运行提示或命令 |
/voice | 切换按键通话语音听写(按住空格键) |
/schedule | 创建、更新、列出或运行计划的远程代理(cron任务) |
/fast | 切换快速模式(相同型号,更快输出) |
/hooks | 查看和管理已配置的挂钩 |
/desktop | 将当前会话切换到桌面应用程序 |
/teleport | 将web/iOS会话拉入您的终端 |
______________________________________________________________________
CLI标志
开始会话
# Start interactive session
claude
# Start with initial prompt
claude "refactor the auth module"
# Continue most recent session
claude -c
# Resume specific session by name
claude -r "auth-refactor"
# Name a session for later
claude -n "feature-payments"
# Run in isolated git worktree
claude -w feature-branch
# Run in worktree with tmux panes
claude -w feature-branch --tmux
# Resume session linked to a GitHub PR
claude --from-pr 42
# Fork a session (new ID, keeps context)
claude -c --fork-session
# Enable auto mode (AI decides permissions)
claude --permission-mode auto
# Bare mode (skip hooks, plugins, auto-memory)
claude --bare打印模式(非交互式/管道)
这 -p flag以非交互方式运行Claude Code。它处理提示并退出。这是将Claude Code集成到脚本、CI/CD管道和Unix管道中的关键:
# Simple query
claude -p "explain this error in server.log"
# Pipe input
cat error.log | claude -p "what caused this crash?"
# Git diff review
git diff main | claude -p "review for security issues"
# JSON output for scripting
claude -p "list all TODO comments" --output-format json
# Structured output with schema validation
claude -p "extract function names from auth.py" \
--output-format json \
--json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}}}'
# Auto-approve tools (careful with this)
claude -p "run tests and fix failures" --allowedTools "Bash,Read,Edit"
# Set spending limit
claude -p "refactor the API layer" --max-budget-usd 5.00
# Limit turns
claude -p "fix the bug" --max-turns 3
# Fallback model when primary is overloaded
claude -p "review this code" --fallback-model haiku
# Restrict available tools
claude -p "analyze this" --tools "Read,Grep,Glob"
# Block specific tools (deny list)
claude -p "refactor auth module" --disallowedTools "Bash(rm:*),Bash(sudo:*)"
# Override system prompt entirely
claude -p "review this code" --system-prompt "You are a security auditor. Focus only on vulnerabilities."
# Append to default system prompt
claude -p "explain this function" --append-system-prompt "Always include time complexity analysis."
# Load MCP servers from external config file
claude -p "check open issues" --mcp-config ./ci-mcp.json
# Disable session persistence (ephemeral, for CI/CD)
claude -p "run lint check" --no-session-persistence
# Debug mode (logs API calls, tool usage, timings)
claude -p "fix the build" --debug
# Debug with category filter
claude -p "fix the build" --debug "api,hooks"
# Write debug output to a file
claude -p "fix the build" --debug-file /tmp/claude-debug.log
# Select a specific agent
claude -p "review auth.py" --agent code-reviewer
# Enable Chrome debugging integration
claude --chrome模型和努力标志
# Use specific model
claude --model opus
claude --model sonnet
claude --model haiku
# Set reasoning effort
claude --effort high # More thorough (costs more)
claude --effort low # Faster, cheaper
# Custom system prompt
claude --append-system-prompt "Always use TypeScript. Never use any."
# Load system prompt from file
claude --append-system-prompt-file ./coding-rules.txt______________________________________________________________________
CLAUDE.md-项目说明
CLAUDE.md是在每个项目的基础上定制CLAUDE代码行为的最重要的文件。它在每个会话开始时自动加载。
文件位置和范围
| 位置 | 范围 | 是否与团队共享? |
|---|---|---|
./CLAUDE.md | 仅此项目 | 是(提交到git) |
./.claude/CLAUDE.md | 仅此项目 | 是(提交到git) |
~/.claude/CLAUDE.md | 所有项目 | 否(个人) |
.claude/rules/*.md | 路径特定规则 | 是(提交到git) |
CLAUDE.md示例
# My Project
## Build and Test
- Run tests: `npm test`
- Build: `npm run build`
- Lint: `npm run lint`
## Code Style
- Use TypeScript strict mode
- Prefer functional components in React
- All API responses must include error handling
- Never commit .env files
## Architecture
- Backend: Express.js + PostgreSQL
- Frontend: React + TailwindCSS
- Auth: JWT tokens stored in httpOnly cookies
See @README.md for project overview.
See @package.json for available scripts.这 @filename 语法从其他文件导入内容。克劳德会自动阅读它们。
路径特定规则
仅适用于某些文件的规则。创建 .claude/rules/api.md:
---
paths:
- "src/api/**/*.ts"
---
# API Rules
- All endpoints must validate input with Zod
- Return proper HTTP status codes
- Include rate limiting on public endpoints为具有以下条件的任何项目自动生成CLAUDE.md /init.
______________________________________________________________________
权限模式
Claude Code在运行命令或编辑文件之前会请求权限。通过以下方式在模式之间切换 Shift+Tab:
| 模式 | 行为 | 用例 |
|---|---|---|
| 默认 | 首次使用每个工具的提示 | 正常的互动工作 |
| 计划 | 只读,无需编辑,无需命令 | 安全探索和规划 |
| 接受编辑 | 自动批准文件编辑,提示命令 | 受信任的编辑会话 |
| 别问 | 除非事先批准,否则自动拒绝 | 严格控制 |
| 自动 | AI分类器决定每次工具调用的允许/拒绝 | 具有明确CLAUDE.md规则的受信任项目 |
| 绕过权限 | 跳过所有提示(危险) | 仅限容器/沙盒 |
预先批准特定工具
通过CLI:
# Allow git commands and file reads without prompting
claude --allowedTools "Read,Bash(git *)"
# Block dangerous tools explicitly
claude --disallowedTools "Bash(rm:*),Bash(sudo:*),Bash(chmod:*)"
# Combine both: allow some, block others
claude --allowedTools "Bash(git:*),Read,Edit" --disallowedTools "Bash(rm -rf:*)"或在 .claude/settings.json:
{
"permissions": {
"allow": [
"Read",
"Bash(git *)",
"Bash(npm run *)",
"Bash(docker compose *)"
],
"deny": [
"Bash(rm -rf *)"
]
}
}______________________________________________________________________
模型和努力水平
| 型号 | 最适合 | 速度 |
|---|---|---|
| 十四行诗 (Claude Sonnet 4.6) | 日常编码、编辑、重构、测试 | 快速 |
| 作品 (Claude Opus 4.6) | 复杂的架构、调试、多文件更改 | 速度较慢 |
| 俳句 (克劳德·海库4.5) | 简单任务,快速提问 | 最快 |
在会话中期切换型号 /model opus 或 Option+P (Mac)/ Alt+P (Linux/Windows)。
努力程度控制着克劳德运用的推理程度:
| 级别 | 用例 |
|---|---|
/effort low | 简单的问题,快速查找 |
/effort medium | 默认、平衡速度和质量 |
/effort high | 复杂的调试、架构决策 |
/effort max | 最难的问题(仅限Opus,无限思维预算) |
______________________________________________________________________
MCP服务器-扩展克劳德代码
MCP(模型上下文协议) 允许Claude Code连接到外部工具和数据源。在中配置服务器 .mcp.json 在项目根或 ~/.claude/.mcp.json 对于所有项目。
GitHub和PostgreSQL服务器的示例配置:
{
"mcpServers": {
"github": {
"type": "stdio",
"command": "npx",
"args": ["@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "$GITHUB_TOKEN"
}
},
"postgres": {
"type": "stdio",
"command": "npx",
"args": ["@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://user:pass@localhost:5432/mydb"
}
}
}
}管理MCP连接 /mcp 在交互式会话中,或在CLI上传递配置文件:
# Load MCP servers from an external config
claude --mcp-config ./team-mcp.json
# Load multiple configs
claude --mcp-config ./github-mcp.json ./db-mcp.json
# Ignore all other MCP sources, only use this config
claude --mcp-config ./ci-mcp.json --strict-mcp-config这 --mcp-config 标志在CI/CD中很有用,因为您需要跨运行进行一致的MCP服务器配置。 --strict-mcp-config 确保只加载指定的服务器,忽略任何来自 .mcp.json 或用户设置。
______________________________________________________________________
自定义技能-你自己的杀戮命令
技能是可重复使用的提示,显示为斜线命令。创建它们 .claude/skills/ (项目)或 ~/.claude/skills/ (个人)。
示例:创建部署技能:
mkdir -p .claude/skills/deploy创建 .claude/skills/deploy/SKILL.md:
---
name: deploy
description: Deploy the application to production
argument-hint: "[environment]"
user-invocable: true
---
Deploy the application to the $0 environment (default: staging).
Steps:
1. Run the test suite
2. Build the production bundle
3. Deploy using the deploy script
4. Verify the deployment health check现在使用它: /deploy production
______________________________________________________________________
自定义子代理
子代理是Claude可以委派任务的专用AI代理。在以下位置创建它们 .claude/agents/:
mkdir -p .claude/agents/code-reviewer创建 .claude/agents/code-reviewer/AGENT.md:
---
name: code-reviewer
description: Expert code reviewer. Use proactively after code changes.
tools: Read, Grep, Glob, Bash
model: sonnet
---
You are a senior code reviewer. When reviewing code:
- Check for security vulnerabilities (SQL injection, XSS, etc.)
- Verify error handling is complete
- Flag any hardcoded secrets or credentials
- Suggest performance improvements
- Check for proper input validationClaude会自动将代码审查任务委托给此代理,或直接使用 @code-reviewer check my latest changes.
从CLI中,为整个会话选择一个代理:
# Use a named agent from .claude/agents/
claude --agent code-reviewer
# In print mode (CI/CD)
claude -p "review the latest commit" --agent code-reviewer
# Define agents inline (no files needed)
claude --agents '{"qa": {"description": "QA tester", "prompt": "You are a QA engineer. Write test cases for every change."}}'______________________________________________________________________
Hooks-自动化触发器
Hooks会自动运行shell命令以响应Claude Code事件。在中配置 .claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "npx prettier --write $(jq -r '.tool_input.file_path')"
}
]
}
],
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude needs input\" with title \"Claude Code\"'"
}
]
}
]
}
}常见挂钩事件: SessionStart, PreToolUse, PostToolUse, Stop, Notification, UserPromptSubmit.
______________________________________________________________________
上下文窗口管理
Claude Code有一个有限的上下文窗口。管理好它是富有成效的会议和碰壁之间的区别。
关键命令
| 命令 | 何时使用 |
|---|---|
/compact | 在任务中期总结对话以释放空间 |
/compact Focus on auth module | 总结,但保持具体背景不变 |
/clear | 完成无关任务之间的重置 |
/context | 看看是什么在消耗你的上下文 |
长时间会话的提示
- 使用
/clear在不相关的任务之间。不要把旧的背景带入新的工作中 - 跑
/compact当你注意到速度减慢时,主动出击 - 使用子代理进行详细操作(测试运行、日志分析)。他们的输出保持在子代理环境中
- 将详细说明转移到技能上,而不是每次都打字
- 将CLAUDE.md保持在200行以下。将详细信息移动到单独的文件并导入
@filename
______________________________________________________________________
Git工作树-并行工作
同时处理多个任务,而不会发生分支切换冲突:
# Start Claude in a new worktree
claude -w feature-auth
# Batch changes across many files in parallel
/batch migrate all components from class-based to functional这 -w flag创建了一个独立的git工作树。更改发生在工作树中,不会影响您的主工作目录。代理完成后,将工作树分支合并回去。
______________________________________________________________________
设置文件参考
| 文件 | 范围 |
|---|---|
.claude/settings.json | 项目(通过git共享) |
.claude/settings.local.json | 项目(匿名、个人) |
~/.claude/settings.json | 所有项目(个人) |
常用设置:
{
"model": "sonnet",
"effortLevel": "medium",
"defaultMode": "default",
"autoMemoryEnabled": true,
"permissions": {
"allow": ["Read", "Bash(git *)"],
"deny": ["Bash(rm -rf *)"]
}
}______________________________________________________________________
环境变量
| 变量 | 目的 |
|---|---|
ANTHROPIC_MODEL | 默认模型(例如。, opus) |
ANTHROPIC_API_KEY | 用于直接访问API的API密钥 |
CLAUDE_CODE_EFFORT_LEVEL | 默认努力(低/中/高/最大) |
CLAUDE_CODE_DISABLE_AUTO_MEMORY | 设置为 1 禁用自动记忆 |
CLAUDE_CODE_DISABLE_1M_CONTEXT | 设置为 1 禁用1M令牌上下文 |
MAX_THINKING_TOKENS | 自定义思维预算(默认值不同) |
HTTPS_PROXY | 企业网络的HTTP代理 |
______________________________________________________________________
文件结构参考
# User-level (personal, all projects)
~/.claude/
settings.json # Global settings
keybindings.json # Custom keyboard shortcuts
CLAUDE.md # Personal instructions
rules/ # Personal rules
agents/ # Personal subagents
skills/ # Personal skills
.mcp.json # Global MCP servers
# Project-level (shared with team)
.claude/
settings.json # Project settings (git tracked)
settings.local.json # Local overrides (gitignored)
CLAUDE.md # Project instructions
rules/ # Path-specific rules
agents/ # Project subagents
skills/ # Project skills
.mcp.json # Project MCP servers
CLAUDE.md # Project instructions (alternative location)______________________________________________________________________
实用的工作流程技巧
正确启动每个项目
# In your project root
claude
/init这将扫描您的项目,并生成一个包含构建命令、测试指令和代码约定的CLAUDE.md。查看并编辑它。此文件决定了未来的每一次交互。
建造前计划
按 Shift+Tab 在要求克劳德进行更改之前进入计划模式。在计划模式下,克劳德会阅读和分析,但无法编辑任何内容。一旦你批准了计划,切换回默认模式并说“执行计划”。
立即撤销错误
按 Esc + Esc 打开倒带菜单。选择“还原代码和对话”以撤消代码更改和导致更改的对话。比...更快 git stash 用于快速实验。
管道一切
# Analyze logs
tail -500 /var/log/app.log | claude -p "find the root cause of errors"
# Review a PR
gh pr diff 42 | claude -p "security review this PR"
# Generate commit message
git diff --staged | claude -p "write a conventional commit message" --model haiku
# Translate
cat README.md | claude -p "translate to Japanese" > README.ja.md
# Docker container analysis
docker ps --format json | claude -p "which containers are using the most resources?"
# Kubernetes pod debugging
kubectl logs deployment/api --tail=200 | claude -p "why are requests failing?"
# Database structure review
pg_dump --schema-only mydb | claude -p "suggest index improvements"
# Summarize recent commits
git log --oneline -20 | claude -p "write release notes from these commits"
# Find config issues
cat /etc/nginx/nginx.conf | claude -p "check for misconfigurations"附带问题/btw
需要在不干扰当前任务的情况下提出问题吗?使用 /btw:
/btw what was the name of that config file we edited earlier?响应是短暂的(不保存到上下文中),低成本(使用缓存),并且不使用任何工具。
成本控制
- 使用
/cost随时检查支出 - 大多数工作使用Sonnet,Opus仅用于复杂问题
- 使用
/effort low用于简单任务 - 跑
/compact减少上下文大小(每个请求的令牌更少) - 在提示中要具体。模糊的请求会导致更多的来回
- 使用
--max-budget-usd 5.00在CI/CD中防止成本失控
______________________________________________________________________
提示提示-获得更好的结果
Claude Code输出的质量在很大程度上取决于你如何提示它。这些模式始终如一地产生更好的结果。
要具体,不要含糊
| 不良提示 | 良好提示 |
|---|---|
| “修复bug” | “当电子邮件包含加号时,登录端点返回500,修复src/auth/login.ts中的输入验证” |
| “让它更快” | “/api/users端点在10k行上占用3s,添加数据库索引和分页” |
| “编写测试” | “为src/presing.ts中的calculateDiscount函数编写单元测试,涵盖边缘情况:零价格、负数量、过期优惠券” |
| “清理代码” | “将src/api/orders.ts和src/api/returns.ts中的重复验证逻辑提取到共享验证器中” |
首先使用计划模式
对于复杂任务,切换到计划模式(Shift+Tab)并要求Claude在编码前进行探索:
# In plan mode:
"I need to add OAuth2 login with Google. Look at the existing auth flow
in src/auth/ and propose where to add the Google provider without
breaking the current email/password login."查看计划,然后切换回默认模式并说“执行计划”。
给出验证目标
告诉克劳德如何验证自己的工作:
"Add rate limiting to the /api/auth endpoints. After implementing,
run npm test to make sure nothing breaks, and verify the rate limiter
works by checking the middleware is registered in the Express app."使用@file引用作为上下文
将Claude指向特定文件,而不是进行搜索:
"look at @src/db/schema.ts and @src/api/users.ts - the User model
has a new 'role' field in the schema but the API doesn't expose it.
Add the role field to the GET /users/:id response."课程早点纠正
如果你看到克劳德走错了方向,按 Ctrl+C 停止并重定向。不要等到它完成了一个大的错误更改。如果它已经进行了错误的更改,请按 Esc + Esc 倒带。
将大型任务分解为步骤
与其“构建用户管理系统”,不如将其拆分:
- 使用id、电子邮件、姓名、角色、created_at创建用户数据库架构
- “为具有输入验证的用户添加CRUD API终结点”
- “为用户端点编写测试”
- “添加基于角色的访问控制中间件”
每一步在进入下一步之前都会经过验证。这避免了在糟糕的基础上浪费代币。
______________________________________________________________________
代币保存策略
发送给克劳德的每条消息都包含完整的对话内容。更大的上下文意味着更高的成本和更慢的响应。
环境卫生
| 策略 | 方法 | 节省 |
|---|---|---|
| 任务之间清晰 | /clear 切换到无关工作时 | 影响最大,重置为零 |
| 主动压缩 | /compact Focus on the auth module changes | 保持相关上下文,降低噪音 |
| 检查上下文使用情况 | /context 查看是什么在消耗空间 | 识别膨胀源 |
| 使用@文件提及 | @src/auth.ts 而不是“查找身份验证文件” | 避免探索性文件读取 |
| 使用子代理 | 委派详细任务(测试运行、日志分析) | 输出保留在子代理上下文中 |
模型选择
| 任务 | 模型 | 努力 |
|---|---|---|
| 简单的编辑、重命名、格式化 | Sonnet | /effort low |
| 标准编码、测试、重构 | Sonnet | /effort medium |
| 复杂的调试、架构 | Opus | /effort high |
| 快速提问 | 俳句(通过 /btw) | 无 |
CLAUDE.md优化
- 将主CLAUDE.md保持在200行以下。它加载在每条消息上
- 将详细说明转化为技能。它们仅在调用时加载
- 使用
@file导入大型参考文档,而不是粘贴内容 - 将路径特定规则放入
.claude/rules/。它们仅在Claude触摸匹配文件时加载
MCP和工具优化
- 使用禁用未使用的MCP服务器
/mcp每个服务器都将工具定义添加到上下文中 - 使用CLI工具(
gh,aws,gcloud)当单个命令工作时,使用MCP - 集
ENABLE_TOOL_SEARCH=auto:5当工具定义超过上下文的5%时,自动推迟工具定义
CI/CD预算控制
# Hard spending limit
claude -p "fix failing tests" --max-budget-usd 2.00
# Limit agentic turns
claude -p "review this file" --max-turns 3
# Use cheapest model for simple CI tasks
claude -p "check for linting errors" --model haiku______________________________________________________________________
诊断
--debug-详细日志记录
当您需要确切了解Claude Code正在做什么时(API调用、工具调用、计时):
# Full debug output to stderr
claude --debug
# Filter to specific categories
claude --debug "api,hooks"
# Write debug logs to a file (keeps terminal clean)
claude --debug-file /tmp/claude-debug.log当MCP服务器未连接、挂钩未启动或API调用以静默方式失败时很有用。
/医生-健康检查
跑 /doctor 当事情不正常时。它检查:
- Claude代码版本和自动更新程序状态
- 身份验证状态和令牌有效性
- API连接
- 设置文件语法和冲突
- MCP服务器运行状况
- 权限配置
/见解-会话分析
跑 /insights 在长时间的会议后,获得一份分析报告,内容包括:
- 按类别划分的代币使用情况
- 会话中最昂贵的操作
- 使用Claude代码的模式
- 提高效率的建议
- 等待时间与积极工作时间
使用此功能可以识别浪费代币的习惯并调整您的工作流程。
______________________________________________________________________
IDE集成
Claude Code与VS Code和JetBrains IDE集成,用于内联差异、上下文共享和并排对话。
VS代码
- 从VS Code市场安装“Claude Code”扩展
- 打开命令选项板(
Cmd+Shift+P在Mac上/Ctrl+Shift+P在Linux/Windows上)并搜索“克劳德代码” - 也适用于光标编辑器
JetBrains(IntelliJ、PyCharm、WebStorm等)
- 从JetBrains市场安装“Claude Code”
- 适用于所有JetBrains IDE
- 支持交互式差异和上下文共享
使用IDE自动检测启动Claude:
claude --ide______________________________________________________________________
自动模式
自动模式使用AI分类器来决定每个工具调用是否需要您的批准。它不是在每个命令上都提示,而是允许安全操作(读取、git状态、测试运行)并自动阻止有风险的操作(强制推送、rm-rf、生产部署)。启用它 --permission-mode auto 或按 Shift+Tab 骑自行车去。
检查分类器规则:
# View current auto-mode config
claude auto-mode config
# View default allow/deny rules
claude auto-mode defaults
# Get AI feedback on your custom rules
claude auto-mode critique在中自定义规则 .claude/settings.json 或 ~/.claude/settings.json 在...之下 autoMode 钥匙与 allow, soft_deny,以及 environment 阵列。
______________________________________________________________________
自动化脚本
链式多 claude -p 在bash脚本中调用可重复工作流:
#!/bin/bash
# Automated PR review pipeline
git diff HEAD~1 > /tmp/diff.txt
cat /tmp/diff.txt | claude -p "review for security issues" --model sonnet > security_review.md
cat /tmp/diff.txt | claude -p "check for performance regressions" --model sonnet > perf_review.md
cat /tmp/diff.txt | claude -p "write a one-paragraph summary" --model haiku > summary.md批量处理多个文件:
#!/bin/bash
# Generate docstrings for all Python files missing them
for f in $(grep -rL '"""' src/*.py); do
claude -p "add docstrings to functions missing them in $f" \
--allowedTools "Read,Edit" --max-turns 3
done从git标签生成更改日志:
git log v1.0.0..v2.0.0 --oneline | \
claude -p "create a changelog grouped by feature, fix, and chore" \
--output-format text > CHANGELOG.md______________________________________________________________________
/循环-重复任务
这 /loop slash命令在会话中以重复的间隔运行提示符或另一个slash命令:
# Check deploy status every 5 minutes
/loop 5m check if the deploy on staging is complete
# Run tests every 10 minutes (default interval)
/loop /test
# Custom interval with a slash command
/loop 2m /security-review循环在后台运行,直到您停止它或结束会话。每次迭代都有自己的上下文,因此长时间运行的循环不会使对话膨胀。
______________________________________________________________________
快速参考卡
您每天将使用的20个命令和快捷键:
| 行动 | 如何 |
|---|---|
| 开始新会话 | claude |
| 继续上次会话 | claude -c |
| 初始化项目 | /init |
| 切换到计划模式 | Shift+Tab |
| 交换机型号 | /model opus 或 Option+P / Alt+P |
| 撤消更改 | Esc + Esc |
| 自由上下文空间 | /compact |
| 重新开始 | /clear |
| 检查成本 | /cost |
| 审查变更 | /diff |
| 安全扫描 | /security-review |
| 命名会话 | /rename my-feature |
| 恢复会话 | /resume 或 claude -r |
| 提及一个文件 | @path/to/file |
| 直接运行bash | !ls -la |
| 多行输入 | \ + Enter |
| 取消生成 | Ctrl+C |
| 退出 | Ctrl+D |
| 非交互式 | claude -p "query" |
| 显示所有命令 | /help |
______________________________________________________________________
子命令参考
| 子命令 | 它做什么 |
|---|---|
claude agents | 列出已配置的代理及其描述 |
claude auth | 管理身份验证(登录、注销、令牌信息) |
claude auto-mode config | 打印有效的自动模式分类器规则 |
claude auto-mode defaults | 显示默认允许/拒绝规则 |
claude auto-mode critique | 获取有关自定义自动模式规则的AI反馈 |
claude doctor | 自动更新程序、身份验证、MCP、设置的健康检查 |
claude install [target] | 安装特定版本(稳定、最新或版本号) |
claude mcp | 从CLI配置和管理MCP服务器 |
claude plugins | 管理Claude代码插件 |
claude setup-token | 设置长期身份验证令牌(需要订阅) |
claude update | 检查并安装更新 |
______________________________________________________________________
相关资源
______________________________________________________________________
贡献
发现缺少的命令或过时的标志?打开一个问题或提交PR。欢迎所有投稿。
许可证
该项目根据MIT许可证获得许可。看 许可证 了解详情。
