学习中心/Claude Code/Claude Code 安装与快速上手
入门

Claude Code 安装与快速上手

从安装到第一次使用,20 分钟掌握 Claude Code 的核心交互方式

Anthropic20 min

学习笔记

Claude Code 安装与快速上手

Claude Code 是 Anthropic 推出的代理式编程工具(agentic coding tool),运行在终端中,能理解你的整个代码库,通过自然语言帮助你编写代码、修复 Bug、管理 Git 工作流。它不只是代码补全——它可以自主探索代码、执行命令、创建文件,像一个真正的编程助手。

你将学到

  • 在 macOS / Windows / Linux 上安装 Claude Code
  • 启动第一个会话并认证
  • Claude Code 的核心交互方式
  • 基本命令和快捷键
  • 权限管理机制

适合谁

  • 从未使用过 Claude Code 的开发者
  • 想从 IDE 插件迁移到终端 AI 工具的工程师
  • 对 AI 辅助编程感兴趣的技术人员

1. 安装 Claude Code

Claude Code 支持多种安装方式,选择最适合你系统的:

macOS / Linux(推荐)

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

macOS Homebrew

brew install --cask claude-code

Windows

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

# 或使用 WinGet
winget install Anthropic.ClaudeCode

npm(全局安装)

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

安装完成后,在终端输入 claude --version 确认安装成功。


2. 启动与认证

进入你的项目目录,输入 claude 启动:

cd your-project
claude

首次启动会引导你完成认证。Claude Code 支持两种认证方式:

  • Claude.ai 账号:直接用浏览器登录
  • API Key:适合 CI/CD 等非交互场景

认证成功后,你就进入了交互式会话。


3. 核心交互方式

Claude Code 的交互非常直观——直接用自然语言描述你想做的事:

探索代码

explain the architecture of this project

这个项目的入口文件在哪里?

how does the authentication system work?

修改代码

add input validation to the signup form

fix the TypeError in src/utils/parser.ts

refactor the database connection to use connection pooling

执行命令

run the test suite and fix any failures

install axios and create an HTTP client module

build the project and check for errors

Git 操作

commit my changes with a descriptive message

create a PR with a summary of all changes

show me what changed since yesterday

4. 基本命令与快捷键

斜杠命令

在会话中输入 / 开头的命令:

命令 作用
/init 生成项目的 CLAUDE.md 配置文件
/clear 清空当前对话上下文
/compact 压缩对话历史,释放上下文空间
/context 查看当前上下文使用情况
/memory 管理 Claude 的记忆系统
/help 查看帮助���息

快捷键

快捷键 作用
Enter 发送消息
Shift+Tab 切换 Plan Mode(只分析不修改)
Escape 停止当前操作
Escape × 2 回退到上一个检查点

非交互模式

适合脚本和 CI/CD 场景:

# 一次性查询
claude -p "Explain what this project does"

# 执行任务后退出
claude -p "List all API endpoints" --output-format json

# 管道输入
cat error.log | claude -p "Analyze this error log and suggest fixes"

5. 权限管理

Claude Code 默认需要你确认才会执行写操作(编辑文件、运行命令),这保证了安全性。

权限模式

模式 说明
Ask(默认) 每次修改需确认
Auto Accept Edits 自动接受文件编辑
Plan Mode 只分析不修改(Shift+Tab 切换)

配置权限白名单

.claude/settings.json 中预先允许安全命令:

{
  "permissions": {
    "allow": [
      "Bash(npm run *)",
      "Bash(git commit *)",
      "Bash(npx jest *)"
    ],
    "deny": [
      "Bash(rm -rf *)",
      "Read(./.env)"
    ]
  }
}

6. 项目初始化:/init

进入项目后,运行 /init 让 Claude 自动分析项目结构并生成 CLAUDE.md

> /init

Claude 会:

  1. 扫描项目结构
  2. 识别技术栈和框架
  3. 生成包含项目上下文的 CLAUDE.md 文件

这个文件会在每次会话开始时自动加载,让 Claude 始终了解你的项目。


小结

  • Claude Code 是运行在终端的 AI 编程助手,支持自然语言交互
  • 安装后进入项目目录输入 claude 即可开始
  • /init 初始化项目,用 /clear 清理上下文
  • 默认权限模式安全可控,可通过配置文件自定义

延伸阅读

内容来源:Anthropic 官方文档