ClaudeCode教程

markdown
2026年7月17日6 min read1,156 words

Updated 2026年7月17日

Claude Code 完整教程:终端 AI 编程助手

目录

  1. Claude Code 是什么?
  2. 安装与配置
  3. 核心概念
  4. 核心功能详解
  5. 命令与快捷键
  6. MCP 扩展
  7. Skills 与 Superpowers 集成
  8. 高级用法
  9. 对比总结

1. Claude Code 是什么?

1.1 一句话定义

Claude Code 是 Anthropic 官方推出的终端 AI 编程助手,能够理解整个代码库、编辑文件、运行命令,并与开发工具深度集成。

1.2 核心能力

能力 说明
代码理解 理解整个代码库,支持跨文件修改
文件编辑 读取、创建、编辑文件
命令执行 运行 Shell 命令、Git 操作
工具集成 通过 MCP 连接外部工具和服务
多端协作 终端、VS Code、Desktop、Web、JetBrains

1.3 支持的平台

平台 说明
Terminal 功能最完整的 CLI
VS Code 官方扩展,inline diffs
JetBrains IntelliJ、Pycharm 等插件
Desktop 桌面应用,多会话并行
Web 浏览器版,无本地安装
Chrome 实时调试 Web 应用

2. 安装与配置

2.1 Terminal 安装

macOS / Linux(推荐):

curl -fsSL https://claude.ai/install.sh | bash

Windows PowerShell:

irm https://claude.ai/install.ps1 | iex

Windows CMD:

curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd

其他方式:

# Homebrew
brew install --cask claude-code

# Winget
winget install Anthropic.ClaudeCode

# apt (Debian/Ubuntu)
sudo apt install claude-code

# dnf (Fedora)
sudo dnf install claude-code

2.2 VS Code 安装

  1. 在 Extensions 中搜索 "Claude Code"
  2. 安装官方扩展
  3. Command Palette (Cmd+Shift+P / Ctrl+Shift+P) → 输入 "Claude Code"

2.3 首次使用

cd your-project
claude

首次使用会提示登录,支持:

  • Claude 订阅账号
  • Anthropic Console API Key
  • 第三方模型(需配置)

3. 核心概念

3.1 CLAUDE.md — 项目记忆文件

Claude Code 在每个项目根目录读取 CLAUDE.md,用于:

  • 设置编码规范
  • 定义架构决策
  • 指定偏好库
  • 评审清单

示例:

# 项目规范

## 技术栈
- React 18 + TypeScript
- Vite 构建
- Tailwind CSS

## 代码规范
- 组件文件大写开头:`UserProfile.tsx`
- Hooks 以 `use` 开头
- 样式用 Tailwind,不用内联 CSS

## Git 工作流
- commit 前必须运行 `npm run lint && npm run test`
- PR 需要至少 1 个 approval

3.2 自动记忆 (Auto Memory)

Claude Code 自动保存学习到的信息:

  • 构建命令
  • 调试技巧
  • 项目特定知识

无需手动维护,跨会话保留。

3.3 会话概念

┌─────────────────────────────────────────────────────────────┐
│                      Session 会话                           │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  Project Directory                                           │
│  ├── CLAUDE.md           ← 项目配置(你写)                 │
│  ├── .claude/            ← 自动记忆(AI 维护)              │
│  │   └── memories/       ← 跨会话知识点                      │
│  └── ...                                                   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

4. 核心功能详解

4.1 自动化重复工作

# 写测试、运行、修复
claude "write tests for the auth module, run them, and fix any failures"

# 修复 lint 错误
claude "fix all lint errors across the project"

# 处理 merge 冲突
claude "resolve merge conflicts"

4.2 构建功能和修复 Bug

# 描述你想要的功能
claude "add a user profile page with avatar upload"

# 描述 bug 症状
claude "the login button doesn't work on mobile"

Claude Code 会:

  1. 理解你的意图
  2. 制定方案
  3. 跨文件实现
  4. 验证结果

4.3 Git 操作

# 提交变更
claude "commit my changes with a descriptive message"

# 创建 PR
claude "create a pull request for this feature branch"

# Code Review
claude "review the changes in this branch"

4.4 MCP 工具连接

MCP (Model Context Protocol) 让 Claude Code 连接外部工具:

# Google Drive 读取设计文档
# Jira 更新工单
# Slack 发送消息
# 自定义工具

详见 第 6 节 MCP 扩展

4.5 调度子代理

# 并行处理多个任务
claude "spawn 3 agents to work on: API, frontend, and tests"

5. 命令与快捷键

5.1 CLI 参数

参数 说明
claude [message] 直接发送消息
claude -p [message] Pipe 模式(无交互)
claude --teleport 从其他端拉取会话
claude --model [model] 指定模型
claude --no-input 只读模式

5.2 交互命令

命令 说明
/help 显示帮助
/compact 压缩对话上下文
/model 切换模型
/permissions 管理权限
/clear 清除当前会话
/exit 退出

5.3 快捷键(VS Code 扩展)

快捷键 功能
Cmd+Shift+C 打开 Claude Code 面板
Cmd+Shift+/ 添加注释到选择
@ 引用文件/符号
# 搜索符号

5.4 管道操作

# 分析日志
tail -200 app.log | claude -p "Slack me if you see any anomalies"

# 批量审查文件
git diff main --name-only | claude -p "review these changed files"

# CI 自动化
claude -p "translate new strings into French and raise a PR"

6. MCP 扩展

6.1 MCP 是什么

MCP (Model Context Protocol) 是连接 AI 工具与外部数据源的开放标准。

6.2 常用 MCP 服务器

MCP 服务器 功能
mcp-server-filesystem 文件系统操作
mcp-server-git Git 操作
mcp-server-github GitHub API
mcp-server-slack Slack 消息
mcp-server-brave 网页搜索
mcp-server-everything 本地文件搜索

6.3 MCP Quickstart

Step 1: 创建项目目录

mkdir my-mcp-server
cd my-mcp-server
npm init -y

Step 2: 安装 MCP SDK

npm install @modelcontextprotocol/sdk

Step 3: 创建服务器

// server.js
import { McpServer } from "@modelcontextprotocol/sdk/server";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio";

const server = new McpServer({
  name: "example-server",
  version: "1.0.0"
});

server.tool(
  "greet",
  "Greet someone",
  { name: { type: "string" } },
  async ({ name }) => ({ content: `Hello, ${name}!` })
);

const transport = new StdioServerTransport();
await server.connect(transport);

Step 4: 配置 Claude Code

~/.claude/settings.json:

{
  "mcpServers": {
    "example": {
      "command": "node",
      "args": ["/path/to/server.js"]
    }
  }
}

7. Skills 与 Superpowers 集成

7.1 安装 Superpowers

/plugin install superpowers@claude-plugins-official

7.2 可用技能

技能 功能
/brainstorming 需求澄清
/using-git-worktrees Git worktree 隔离开发
/writing-plans 任务计划
/executing-plans 执行计划
/test-driven-development TDD 开发
/requesting-code-review 代码审查
/finishing-a-development-branch 分支收尾

7.3 工作流程

/brainstorming        ← 需求澄清
    ↓
/writing-plans        ← 任务分解
    ↓
/test-driven-development  ← TDD 实现
    ↓
/requesting-code-review   ← 代码审查
    ↓
/finishing-a-development-branch  ← 分支收尾

详见 Superpowers 教程


8. 高级用法

8.1 后台代理 (Agent Teams)

# 启动后台代理
claude "work on the API in the background"

# 查看代理状态
claude "show me what the agents are working on"

8.2 定时任务 (Routines)

# 创建定时任务
/schedule

# 配置:每天早上审查 PR

8.3 CI/CD 集成

GitHub Actions:

- name: Claude Code Review
  uses: anthropic/claude-code-action@v1
  with:
    message: "Review PR #${{ github.event.pull_request.number }}"

GitLab CI/CD:

claude-review:
  script:
    - claude "review the changes in this MR"

8.4 Slack 集成

  1. 配置 Slack MCP
  2. 提及 @Claude 发送任务
  3. 自动回复 PR

8.5 自定义 Hooks

~/.claude/settings.json:

{
  "hooks": {
    "PostEdit": ["npm run lint"],
    "PreCommit": ["npm run test"]
  }
}

9. 对比总结

9.1 Claude Code vs 其他工具

维度 Claude Code Codex CLI OpenCode
公司 Anthropic OpenAI 开源社区
语言 Node.js Rust Go
开源 ❌ 闭源 ✅ Apache 2.0 ✅ MIT
核心哲学 深度推理 精细控制 简洁高效
记忆文件 CLAUDE.md AGENTS.md 无固定格式
MCP 支持 ✅ 原生
多代理
定时任务 ✅ Routines

9.2 选型建议

场景 推荐工具
需要 Claude 模型 + 深度推理 Claude Code
需要自定义工作流 + 极客体验 Codex CLI
需要轻量 + 多模型支持 OpenCode
需要完整 AI 工程方法论 Claude Code + Superpowers

9.3 官方资源


本教程基于 Claude Code 官方文档编写,最后更新于 2026 年 7 月。