Claude Code 完整使用指南


1. 安装与更新

安装

macOS/Linux:

npm install -g @anthropic-ai/claude-code
# 或
curl -sL https://claude.ai/claude-code/install.sh | sh

Windows:

npm install -g @anthropic-ai/claude-code
# 或
winget install anthropic.claude-code

版本检查与更新

claude --version    # 查看版本
npm update -g @anthropic-ai/claude-code   # npm 更新
claude update       # 或使用此命令更新

2. 常用指令 (Slash Commands)

核心命令

命令 功能
/help 显示帮助信息
/clear 清除当前会话上下文
/quit 退出 Claude Code
/model 切换使用的模型
/save 保存当前会话
/screenshot 截取屏幕截图

版本控制命令

命令 功能
/commit 创建 git 提交
/branch 创建或切换分支
/review 启动 PR 审查
/diff 显示未提交的更改
/status 显示 git 状态

文件操作命令

命令 功能
/read 读取文件内容
/write 写入文件
/edit 编辑文件
/glob 查找文件
/grep 搜索文件内容
/mkdir 创建目录
/rm 删除文件/目录

开发流程命令

命令 功能
/test 运行测试
/build 执行构建
/lint 运行代码检查
/debug 启用调试模式
/explain 解释代码

快捷键

快捷键 功能
Ctrl+C 取消当前操作
Ctrl+L 清屏
Tab 自动补全
↑/↓ 浏览历史命令

3. 权限模式

Claude Code 采用分级权限系统,配置在 settings.json 中。

权限级别

  • Read-only - 只读,禁止修改文件
  • Standard - 标准,允许常见操作
  • Developer - 开发者,允许危险操作
  • Admin - 管理员,完全访问

交互模式

{
  "permissionMode": "ask"  // ask | allow | deny
}
模式 行为
ask 执行危险操作前询问(默认
allow 自动允许所有操作
deny 自动拒绝所有操作

快速切换命令

# 查看当前权限模式
claude config get permissionMode

# 启动时指定(单次会话)
claude --permission-mode allow

# 完全绕过模式(危险!)
claude --dangerously-skip-permissions

权限配置示例

{
  "permissions": {
    "allow": ["Read", "Write", "Bash:git", "Bash:npm"],
    "deny": ["Bash:rm", "Bash:format", "Write:/etc/**"]
  }
}

允许/禁止特定命令

{
  "permissions": {
    "allowCommands": ["/help", "/clear", "/commit"],
    "denyCommands": ["/sudo", "/shell"]
  }
}

auto 模式(需 Team 套餐)

在 CLI 中按 Shift+Tab 循环切换:defaultacceptEditsplanauto

  • 需要 --enable-auto-mode 参数启动
  • 还需要 Team 套餐(或 Enterprise/API 套餐)
claude --enable-auto-mode

4. 使用技巧

  1. 明确任务描述 - 包含具体目标和约束
  2. 分步骤操作 - 将复杂任务分解
  3. 善用自动补全 - Tab 键加速输入
  4. 上下文管理 - 定期 /clear 清除不需要的上下文
  5. 文件 glob 模式 - 使用 **/*.js 等通配符匹配
  6. 定期保存会话 - /save 保存重要会话

常用工作流

快速修复工作流:

描述问题 → 获取修复建议 → 预览更改 → 确认应用

开发新功能工作流:

/branch 新功能分支 → 实现代码 → /test 运行测试 → /commit 提交 → /review 代码审查

5. MCP 服务器配置

什么是 MCP

MCP (Model Context Protocol) 允许 Claude Code 连接外部数据源和工具。

配置示例

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-server-filesystem"],
      "env": { "ALLOWED_DIRECTORIES": "/home/user/projects" }
    },
    "github": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-server-github"],
      "env": { "GITHUB_TOKEN": "your-token" }
    },
    "brave-search": {
      "command": "npx",
      "args": ["-y", "@anthropic/mcp-server-brave-search"],
      "env": { "BRAVE_API_KEY": "your-key" }
    }
  }
}

常用 MCP 服务器

服务器 用途
@anthropic/mcp-server-filesystem 文件系统访问
@anthropic/mcp-server-github GitHub API 集成
@anthropic/mcp-server-brave-search 网络搜索
@anthropic/mcp-server-memory 持久化记忆
@anthropic/mcp-server-slack Slack 集成

6. Hooks 配置

Hooks 允许在特定事件发生时自动执行脚本。

Hook 类型

Hook 触发时机
pre-command 命令执行前
post-command 命令执行后
pre-commit Git 提交前
post-commit Git 提交后
pre-build 构建前
post-build 构建后

配置示例

{
  "hooks": {
    "pre-command": [{ "command": "npm run lint", "description": "运行 lint" }],
    "post-commit": [{ "command": "git push", "description": "自动推送" }]
  }
}

7. IDE 集成

VS Code

安装 “Claude Code” 扩展后配置:

{
  "claude.enabled": true,
  "claude.apiKey": "your-api-key",
  "claude.autocomplete": true
}

JetBrains

安装 “Claude Code” 插件,配置 API Key 即可。

其他 IDE

IDE 集成方式
Vim/Neovim claude.nvim 插件
Emacs claude.el

8. 配置文件位置

配置文件 路径
全局设置 ~/.claude/settings.json
项目设置 ./.claude/settings.json
本地覆盖 ~/.claude/settings.local.json
密钥 ~/.claude/credentials.json
缓存 ~/.claude/cache/

9. 环境变量

export ANTHROPIC_API_KEY="your-api-key"
export ANTHROPIC_BASE_URL="https://api.anthropic.com"
export ANTHROPIC_MODEL="claude-opus-4"
export CLAUDE_DEBUG=true

10. 故障排除

claude --help           # 显示帮助
claude --version        # 版本信息
claude config --show    # 检查配置
claude doctor           # 诊断问题

附录:启动参数大全

参数 作用 风险
--permission-mode acceptEdits 默认自动接受编辑
--allowedTools "工具列表" 预授权无需确认的工具
--dangerously-skip-permissions 完全跳过所有权限检查 极高
--disallowedTools "工具列表" 禁用指定工具 安全
--model claude-sonnet 指定使用的模型 -
--session-id UUID 指定会话ID -
--add-dir 目录 添加工作目录 -
--output-format json 指定输出格式 -
--debug 调试模式 -
--max-turns 数字 限制AI交互轮次 -
--system-prompt 内容 覆盖默认系统提示 -
--enable-auto-mode 启用 auto 交互模式 -