36
Claude Code 全面拆解
Claude Code 完整使用指南
Claude Code 不是又一个 AI IDE,而是革命性的 CLI 智能编程助手!本指南整理自官方文档,涵盖安装、配置、MCP 集成、安全管理等所有使用技巧。
Claude Code vs AI IDE 对比
| 核心痛点 | Cursor 等 AI IDE | Claude Code (CLI) |
|---|---|---|
| AI 能力 | 成本考虑导致降智 | 原生 Claude 4 Sonnet/Opus,无任何阉割 |
| 工具调用 | 仅 25 次,复杂任务被中断 | 无限工具调用,一气呵成 |
| 上下文 | 窗口小,大项目理解不全 | 200K+ 上下文,整个项目一览无余 |
| Agent 能力 | 长任务会被中断 | 完全自主 Agent,从头到尾自动执行 |
| 调试能力 | 只能看代码 | 直接读取系统日志,实时 debug |
安装教程
方式一:镜像站安装(推荐国内用户)
macOS:
curl -fsSL https://aicodewith.com/claudecode/resources/install-script-macos | bash
Linux:
curl -fsSL https://aicodewith.com/claudecode/resources/install-script-linux | bash
Windows (WSL):
# 安装 WSL2
wsl --install
# 在微软商店安装 Ubuntu,然后运行
curl -fsSL https://aicodewith.com/claudecode/resources/install-script-wsl | bash
方式二:NPM 官方安装
# 全局安装
npm install -g @anthropic-ai/claude-code
# 验证版本
claude --version
首次配置
API 配置(镜像站用户可跳过)
# 从官网获取 API Key
export ANTHROPIC_API_KEY="sk-your-key-here"
# 根据你的 shell 选择
# Bash
echo 'export ANTHROPIC_API_KEY="sk-your-key-here"' >> ~/.bashrc
source ~/.bashrc
# Zsh
echo 'export ANTHROPIC_API_KEY="sk-your-key-here"' >> ~/.zshrc
source ~/.zshrc
基本配置
# 修改默认设置
claude config set -g model claude-sonnet-4
claude config set -g verbose true
claude config set -g outputFormat text
# 测试安装
claude "Hello, Claude!"
claude /doctor
安全设置(可选)
export DISABLE_TELEMETRY=1 # 不发送统计信息
export DISABLE_ERROR_REPORTING=1 # 不发送错误日志
export DISABLE_NON_ESSENTIAL_MODEL_CALLS=1 # 节约 token
# 限制工具权限
claude config set allowedTools "Edit,View"
基本命令
基础交互
claude # 启动软件
claude "帮我修复这个 bug" # 一次性命令执行
claude -p "<prompt>" # 单次打印模式
cat file | claude -p "<prompt>" # 大文件读取
claude update # 更新客户端
claude mcp # 启动 MCP 向导
对话命令
claude -c # 继续上次对话
claude -r <id> # 按会话 ID 恢复
claude --resume <id> # 长对话恢复
claude --resume <name> # 按名字恢复对话
快捷命令(斜杠命令)
| 命令 | 功能 |
|---|---|
/help | 列出所有斜线命令 |
/add-dir | 添加更多工作目录 |
/clear | 清除聊天记录 |
/compact | 压缩上下文 |
/config | 配置菜单 |
/cost | Token 花费统计 |
/doctor | 客户端完整性检查 |
/exit | 退出 Claude Code |
/init | 初始化项目,生成 CLAUDE.md |
/mcp | 查看 MCP 列表和状态 |
/memory | 编辑记忆 |
/model | 更换模型 |
/permissions | 修改工具权限 |
/review | 请求代码审查 |
/sessions | 列出 sessions 列表 |
/status | 系统/账户状态 |
/vim | 切换 vim 模式 |
MCP 集成
MCP 通过连接外部服务、数据库、API 扩展 Claude 的功能。
基本 MCP 命令
claude mcp list # 列出现有的 MCP 服务
claude mcp add <name> <cmd> # 添加新的 MCP
claude mcp remove <name> # 移除 MCP
配置文件位置:~/.claude.json
常用 MCP 服务
Git MCP:
npm install -g git-mcp-server
claude mcp add git "git-mcp-server"
claude mcp add github "github-mcp-server --token $GITHUB_TOKEN"
数据库 MCP:
npm install -g postgres-mcp-server
export POSTGRES_URL="postgresql://user:password@localhost:5432/mydb"
claude mcp add postgres "postgres-mcp-server --url $POSTGRES_URL"
MCP 工具权限
# 赋予特定 MCP 权限
claude --allowedTools "mcp__git__commit,mcp__git__push"
# 范围权限赋予
claude --allowedTools "mcp__postgres__*"
# 将 MCP 与内置工具结合
claude --allowedTools "Edit,View,mcp__git__*"
配置系统
全局配置文件
位置:~/.claude.json
{
"model": "claude-sonnet-4",
"verbose": true,
"outputFormat": "text",
"allowedTools": ["Edit", "View"],
"disallowedTools": []
}
项目配置文件
位置:.claude/settings.json
{
"model": "claude-sonnet-4",
"systemPrompt": "You are a senior developer working on this project",
"allowedTools": ["Edit", "View", "Bash(git:*)", "Bash(npm:*)"]
}
环境变量
| 变量名 | 默认值 | 效果 |
|---|---|---|
DISABLE_NON_ESSENTIAL_MODEL_CALLS | 0 | 跳过自动摘要,节约 token |
MAX_THINKING_TOKENS | ~30-40k | 设定最大思考 token |
DISABLE_TELEMETRY | 0 | 不发送统计信息 |
CLAUDE_CODE_USE_BEDROCK | 0 | 通过 AWS Bedrock 验证 |
CLAUDE_CODE_USE_VERTEX | 0 | 通过 Google Vertex AI 验证 |
HTTP_PROXY | unset | HTTP 代理 |
HTTPS_PROXY | unset | HTTPS 代理 |
安全和权限管理
权限级别
| 级别 | 描述 | 风险 |
|---|---|---|
| Interactive | 每次操作提示 | 低 |
| Allowlist | 仅限预批准工具 | 中 |
| Dangerous | 跳过所有权限 | 高 |
工具权限管理
# 给予具体工具权限
claude --allowedTools "Edit,View"
# 范围权限(仅 git 操作)
claude --allowedTools "Bash(git:*)"
# 多范围权限
claude --allowedTools "Bash(git:*),Bash(npm:*)"
# 完全权限模式(危险!)
claude --dangerously-skip-permissions
推荐安全策略
1. 限制具体权限
# Good: 具体权限
claude --allowedTools "Edit,View,Bash(git:status)"
# Bad: 过于宽泛
claude --allowedTools "Bash"
2. 保护敏感数据
# Good: 使用环境变量
export DATABASE_URL="postgresql://user:pass@host/db"
# Bad: 硬编码凭证
claude "connect to postgresql://user:password123@host/db"
3. 定期审查权限
claude config get allowedTools
claude config get disallowedTools
claude config list
思考模式
通过设置 prompt 关键词加深思考深度:
| 思考深度 | Prompt 关键词 |
|---|---|
| 1 | think |
| 2 | think about it, think a lot, think deeply, think hard |
| 3 | think harder, think intensely, ultrathink |
示例:
claude -p "We have a tricky concurrency bug. ultrathink and propose a fix."
- 关键字可以出现在提示中的任何位置
- 如果存在多个关键字,最高层级优先
Config 命令详解
claude config list # 显示所有当前设置
claude config get <key> # 显示具体设置
claude config set -g <key> <value> # 设置全局值
claude config add -g <key> <value> # 附加到数组设置
claude config remove -g <key> <value> # 从数组中删除
注:-g 参数表示全局设置,不加则仅为当前项目设置
常用配置项
| Key | 常用值 | 功能 |
|---|---|---|
theme | dark, light | CLI 主题 |
verbose | true/false | 显示完整输出 |
autoUpdates | true/false | 自动更新 |
autoCompactEnabled | true/false | 自动压缩聊天记录 |
diffTool | meld, diff | Diff 工具 |
parallelTasksCount | 1-8 | 并发任务数 |
autoConnectIde | true/false | 自动连接 IDE |
团队协作与 CI/CD
GitHub Actions 集成
name: Claude Code Review
on:
pull_request:
branches: [main, develop]
jobs:
claude-review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js 18
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install Claude Code
run: npm install -g @anthropic-ai/claude-code
- name: Review PR
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
claude -p "Review changes for security issues and bugs" \
--allowedTools "View" \
--output-format json > review-results.json
- name: Upload results
uses: actions/upload-artifact@v4
with:
name: claude-review
path: review-results.json
本地 Git Pre-commit Hook
#!/usr/bin/env bash
# .git/hooks/pre-commit (chmod +x)
staged=$(git diff --cached --name-only --diff-filter=ACM)
[ -z "$staged" ] && exit 0
payload=$(echo "$staged" | xargs cat)
analysis=$(echo "$payload" | \
claude -p "Review these changes for issues before commit" \
--allowedTools "View" \
--output-format json)
if echo "$analysis" | jq -e '.critical_issues[]' >/dev/null 2>&1; then
echo "❌ Critical issues found – commit blocked"
exit 1
fi
echo "✅ Claude analysis passed"
高级特性
持久化记忆 (CLAUDE.md)
由 /init 生成,位于项目根目录:
# Project: My Application
## Overview
This is a React/Node.js application with PostgreSQL database.
## Architecture
- Frontend: React 18 with TypeScript
- Backend: Node.js with Express
- Database: PostgreSQL 14
## Development Guidelines
- Use TypeScript for all new code
- Follow ESLint configuration
- Write tests for new features
Memory 命令
claude /memory # 编辑项目记忆
claude /memory view # 查看当前记忆
多目录工作区
# 添加多个目录
claude --add-dir ../frontend ../backend ../shared
# 项目级分析
claude "analyze the entire application architecture"
总结
Claude Code 是一个强大的 CLI 智能编程助手,核心优势:
- 原生 Claude 4 - 无任何阉割
- 无限工具调用 - 复杂任务一气呵成
- 200K+ 上下文 - 整个项目一览无余
- 完全自主 Agent - 从头到尾自动执行
- MCP 扩展 - 连接外部服务和数据库
- CI/CD 集成 - 自动化代码审查
建议将本指南作为工具书使用,遇到问题时查阅对应章节。
原文来源:知乎 @Cluade2Claude | 整理于 2025-10-18