跳到主要内容

Claude Code 入门

Anthropic 的 AI 编码代理住在你的终端。不是聊天窗口、不是自动补全 — 一个完整的代理,它读取你的代码库、写代码、运行命令并提交 commit。以下是真正的使用方法。

Claude Code 与其他 AI 编程工具不同。Cursor 和 Copilot 活在编辑器里,在你打字时逐行给出建议;Claude Code 活在终端里,像坐在你旁边的一位同事:能读懂你的整个项目、写文件、跑测试、搜索网络、执行 shell 命令——而你只需用平常话描述想要什么。

本指南带你从零到上手。不讲废话、不讲理论——只讲今天就开始高效使用它需要知道的事。

安装 Claude Code

Claude Code 需要 Node.js 18+ 和一个 Anthropic API 密钥(或 Max 订阅)。全局安装:

npm install -g @anthropic-ai/claude-code

然后进入任意一个项目目录并启动它:

cd your-project
claude

就这样。Claude Code 会启动、扫描你的项目结构并给出提示符,你已经可以开始了。

首次启动:Claude Code 会要求你完成认证。如果你有 Anthropic API 密钥,它会提示你输入;如果你订阅了 Claude Max,也可以直接用它登录。工具会记住你的凭据,供以后的会话使用。

你的第一次对话

学习 Claude Code 最好的方式是交给它一个真实任务。别从“你好”开始——从你确实需要完成的事开始。

好的第一条提示词

Explain the structure of this project. What does each directory do?

Find all TODO comments in the codebase and summarize them

Add input validation to the signup form in src/auth/register.ts

The tests in tests/api/ are failing. Diagnose and fix.

Claude Code 会读取相关文件、理解上下文、提出修改,并且——经你许可——落实它们。每个动作都由你把关:读文件自动进行,但写文件和 shell 命令会先请求确认。

权限系统

Claude Code 采用分层的权限模型。理解它可以让你少点一百次“批准”。

  • 始终允许:读文件、搜索代码、列出目录。Claude Code 需要理解你的项目——让它读。
  • 只问一次:写文件、新建文件。第一次会询问,你批准之后,本次会话内它会记住。
  • 始终询问:shell 命令、破坏性操作。Claude Code 运行 npm install、git push 或任何会改动文件之外状态的操作之前,你都要确认。
小技巧:按 Shift+Tab 可以在权限模式之间切换。“自动接受编辑”模式让 Claude Code 无需逐次确认即可写文件——在范围清晰、你信得过的任务上很实用。随时可以切回来。

CLAUDE.md 文件

这是大多数人不知道的最重要的文件。在项目根目录建一个 CLAUDE.md,写进跨会话持续生效的指示:

# CLAUDE.md

## Project context
This is a Next.js 14 app with TypeScript, Prisma ORM, and Tailwind CSS.
The API routes are in app/api/. Auth uses NextAuth v5.

## Conventions
- Use server components by default, client components only when needed
- All database queries go through the service layer in src/services/
- Tests use Vitest, run with `npm test`
- Never modify the migration files directly

## Current work
- We're migrating from Pages Router to App Router
- The /dashboard route is the active focus

Claude Code 在每个会话开始时都会自动读取这个文件。它是你为 Claude 提供项目持久上下文的方式,不必每次重复。可以把它想成写给新团队成员的入职文档。

必备斜杠命令

在提示符里输入 / 可以看到全部命令。你最常用的是:

/help 显示全部可用命令和键盘快捷键 /clear 清空对话、重新开始(保留 CLAUDE.md 上下文) /compact 压缩对话以节省上下文空间 /cost 显示当前会话已消耗的 API tokens 费用 /commit 用自动生成的提交信息创建 git commit /review 审阅你当前的改动(类似代码评审)

重要的键盘快捷键

Enter 发送你的消息 Shift+Enter 换行(用于多行提示词) Shift+Tab 切换权限模式 Ctrl+C 取消当前生成 Escape 打断并给出新指示 Up arrow 调出先前的消息

如何有效提示

Claude Code 理解你的代码库,所以不必从零介绍项目。但措辞方式会显著影响结果。

对结果要具体

模糊

Make the API better

具体

Add rate limiting to the /api/search endpoint. Use a sliding window of 60 requests per minute per API key. Return 429 with a Retry-After header when exceeded.

不明显时给上下文

好的上下文

The login page is showing a blank screen on mobile Safari. Desktop works fine. The component is in src/app/login/page.tsx. I think it might be related to the CSS grid layout but I'm not sure.

你不需要描述项目结构(Claude Code 自己能读),但应该分享它看不到的信息:报错信息、浏览器特定行为、你已经试过什么、你怀疑什么。

使用迭代式精炼

不要指望一条提示词把所有事都说对。从主任务开始,检查结果,再逐步细化:

  1. Add a dark mode toggle to the settings page
  2. (审阅这份实现)
  3. Good, but persist the preference in localStorage and apply it on page load before render to prevent flash
  4. Now add a system preference detection fallback for first-time users

每一次追问都建立在 Claude Code 刚做完的事情之上。这种迭代方式比一条巨大的提示词效果更好。

真实工作流

修复 bug

把报错贴进去,或描述症状。Claude Code 会在代码库里搜索相关代码、定位根因并提出修复。

Users are reporting that the checkout total shows $0 when they have
items in cart. Here's the error from the console:

TypeError: Cannot read property 'price' of undefined
at calculateTotal (src/utils/cart.ts:42)

Claude Code 会读取 cart.ts,追踪数据流,找出 price 为什么是 undefined,并修复它。

功能实现

描述你想要什么,并把范围说清楚——只动一个文件的功能,比牵扯二十个文件的更容易做对。

Add a "forgot password" flow:
1. A "Forgot password?" link on the login page
2. A form that accepts email and sends a reset link
3. A reset page where users enter a new password
4. Use the existing email service in src/services/email.ts
5. Tokens should expire after 1 hour

代码审查

/review 命令会审阅你未提交的改动。也可以更具体:

Review the changes in src/api/ for security issues.
Focus on: input validation, SQL injection, auth bypass, and rate limiting.

重构

Refactor src/utils/helpers.ts — it's 800 lines of unrelated functions.
Split it into focused modules: string-utils.ts, date-utils.ts,
validation.ts, and formatting.ts. Update all imports across the codebase.

Claude Code 擅长跨文件重构,因为它能在同一次会话里读取并修改所有受影响的文件。

理解陌生代码

I just joined this project. Walk me through how a request flows from
the API endpoint in src/app/api/orders/route.ts to the database.
What middleware does it pass through? How is auth handled?

Claude Code 在哪些方面出色

  • 感知代码库的改动:它读的是你的真实代码,而不只是当前文件。它理解 import、类型、模式与约定。
  • 多文件编辑:重命名函数、在文件之间搬移代码、更新 import——整条依赖链它都能处理。
  • 样板与套路:CRUD 接口、表单校验、测试脚手架、迁移文件——一切有清晰模式的活儿。
  • 调试:贴一个报错,它读相关代码、追问题、给修复。
  • 讲解代码:“这个正则在做什么?”或“鉴权中间件是怎么工作的?”
  • Git 操作:带规范信息的提交、审阅 diff、创建分支。

需要注意什么

  • 大规模架构调整:Claude Code 处理文件级改动很好;系统级重构(“从单体迁到微服务”)需要拆成更小、可验证的步骤。
  • 幻觉 API:Claude Code 可能引用不存在的库 API 或过时的语法。不熟悉的方法调用,务必对照库的真实文档核实。
  • 过度自信的修复:有时它“修复”bug 的方式是压住报错而不是解决根因。审阅它的改动时,请拿出审阅初级开发者 PR 的那份警惕。
  • 上下文上限:很长的会话会累积上下文。如果回答质量下降,用 /compact 压缩对话,或 /clear 重新开始(CLAUDE.md 的上下文会保留)。

进阶:hooks

Hooks 让你在 Claude Code 的动作前后自动运行自定义命令。它们定义在设置里,由特定事件触发。

示例:每次文件编辑之后自动运行 linter:

// In .claude/settings.json
{
  "hooks": {
    "afterEdit": {
      "command": "npx eslint --fix ${file}",
      "description": "Auto-lint after edits"
    }
  }
}

Hooks 很适合用来落实项目规范,不必依赖 Claude Code 自己记住。

Claude Code vs. 其他 AI 编码工具

这个领域竞争激烈。对比如下:

  • GitHub Copilot:专注自动补全。住在编辑器里,边打字边逐行建议。最适合:补全你已经动笔的代码。
  • Cursor:编辑器向(VS Code 分支)。行内编辑、多文件改动、感知代码库的对话。最适合:想把 AI 深度融入编辑器的开发者。
  • Windsurf:编辑器向(VS Code 分支)。与 Cursor 类似,另有做多步工作流的“Cascade”。最适合:想要免费档 AI 编辑器的团队。
  • Claude Code:终端向。完整的智能体,具备工具使用、shell 访问和自主多步执行。最适合:习惯终端、想要一个能独立调研、实现、测试并提交的 AI 的开发者。

两者并不互斥。很多开发者用 Copilot 做行内补全、用 Claude Code 做更大的任务——就像同时有拼写检查器和写作搭档。

简短版

  1. 安装:npm install -g @anthropic-ai/claude-code
  2. 在项目里启动:cd your-project && claude
  3. 创建 CLAUDE.md,写清项目背景与约定
  4. 从真实任务开始,别从“你好”开始
  5. 把想要的东西说具体
  6. 迭代:第一稿、审阅、再细化
  7. 上下文变长时用 /compact
  8. 信任但要核验——像审阅任何 PR 一样审阅改动

把 Claude Code 当作一位能干但初级的同事来用,它才最强大:给清晰的指示、审阅它的产出、逐步建立信任。你提供的上下文越多(通过 CLAUDE.md 和具体的提示词),结果就越好。

光标正在闪烁。你想构建什么?