Skip to content

交互模式

什么是交互模式?

交互模式是 Claude Code 的核心功能之一,它提供了一个持续的对话环境,让你可以与 Claude 进行多轮交互,而不需要每次都重新启动命令。

主要优势:

  • 🔄 上下文保持:Claude 记住之前的对话内容
  • 快速迭代:无需重复输入环境信息
  • 🎯 精确控制:可以逐步引导 Claude 完成任务
  • 💬 自然对话:像与同事聊天一样编程

启动交互模式

bash
# 方式 1:直接启动
$ claude

# 方式 2:在指定目录启动
$ cd /path/to/project
$ claude

# 方式 3:使用特定配置启动
$ claude --profile dev

交互模式界面:

╭─────────────────────────────────────────╮
│   Claude Code - Interactive Mode       │
│   Model: Claude Sonnet 4.5             │
│   Project: /home/user/my-project       │
╰─────────────────────────────────────────╯

You: _

基本操作

发送消息

直接输入你的请求并按回车:

You: 创建一个 calculator.js 文件

Claude: I'll create a calculator.js file with basic arithmetic operations.

✓ File created: calculator.js

You: 为它添加单元测试

Claude: I'll create tests for the calculator...

✓ File created: calculator.test.js

多行输入

使用 \ 在行末继续输入:

You: 创建一个 API 服务器,\
需要包含以下端点:\
- GET /users\
- POST /users\
- DELETE /users/:id

Claude: I'll create an API server with those endpoints...

或者使用三重引号:

You: """
创建一个 React 组件:
- 名称:UserCard
- Props:name, email, avatar
- 显示用户信息卡片
"""

Claude: I'll create the UserCard component...

查看历史

You: /history

Recent conversation:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1. You: 创建 calculator.js
   Claude: Created calculator.js
2. You: 添加单元测试
   Claude: Created calculator.test.js
3. You: 运行测试
   Claude: Tests passed ✓
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

会话管理

保存会话

You: /save my-feature-session

✓ Session saved as: my-feature-session
  Location: ~/.claude-code/sessions/my-feature-session.json

加载会话

bash
# 启动时加载
$ claude --session my-feature-session

# 或在交互模式中
You: /load my-feature-session

 Session loaded: my-feature-session
  Messages: 15
  Last activity: 2026-01-20 15:30

列出所有会话

You: /sessions

Saved sessions:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
📁 my-feature-session
   15 messages | 2026-01-20 15:30

📁 bugfix-auth
   8 messages | 2026-01-19 10:00

📁 refactor-api
   23 messages | 2026-01-18 14:15
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

删除会话

You: /delete-session bugfix-auth

⚠  Are you sure you want to delete 'bugfix-auth'? (yes/no)
You: yes

✓ Session deleted: bugfix-auth

上下文管理

查看当前上下文

You: /context

Current context:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Working directory: /home/user/my-project
Files in context: 3
  - src/app.js (120 lines)
  - src/utils.js (45 lines)
  - package.json (25 lines)

Recent operations: 5
Tokens used: 2,340 / 200,000
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

添加文件到上下文

You: /add src/config.js src/database.js

✓ Added to context:
  - src/config.js
  - src/database.js

Now tracking 5 files in context

移除文件

You: /remove src/database.js

✓ Removed from context: src/database.js

清空上下文

You: /clear

⚠  This will clear all context. Continue? (yes/no)
You: yes

✓ Context cleared
  Starting fresh conversation

自动上下文管理

Claude Code 会智能地管理上下文:

You: 修改 src/app.js 中的路由

Claude: [自动将 src/app.js 添加到上下文]
I'll modify the routes in src/app.js...

You: 确保与 config.js 中的配置一致

Claude: [自动将 src/config.js 添加到上下文]
I'll check the configuration and ensure consistency...

快捷键

交互模式支持多种快捷键提高效率:

快捷键功能说明
Ctrl + C中断当前操作停止 Claude 正在执行的任务
Ctrl + D退出交互模式安全退出(会提示保存)
Ctrl + L清屏清除终端显示
Ctrl + R搜索历史搜索之前的命令
/ 浏览历史查看之前的输入
Tab自动完成补全命令和文件名
Ctrl + U清除输入行删除当前输入
Ctrl + W删除单词删除光标前的单词
Ctrl + A行首移动到行首
Ctrl + E行尾移动到行尾

实用示例

# 中断执行
You: 分析整个代码库的性能问题
Claude: [开始分析,但时间过长]
[按 Ctrl + C]
⚠ Operation interrupted by user

# 搜索历史
[按 Ctrl + R]
(reverse-i-search)`test': 运行所有单元测试

实战场景

场景 1:迭代开发功能

You: 创建一个用户注册功能

Claude: I'll create a user registration feature with:
- Registration form
- Validation
- Database storage
✓ Created files: register.js, register.html

You: 添加邮箱验证

Claude: I'll add email verification...
✓ Updated register.js
✓ Created email-service.js

You: 添加密码强度检查

Claude: I'll add password strength validation...
✓ Updated register.js with password requirements

You: 写测试用例

Claude: I'll create comprehensive tests...
✓ Created register.test.js

You: 运行测试

Claude: Running tests...
✓ All 15 tests passed

场景 2:调试问题

You: 用户登录失败,返回 401 错误

Claude: Let me investigate the authentication issue.
Reading auth.js... Found potential issue in JWT validation.

You: 具体是什么问题?

Claude: The JWT secret key doesn't match between:
- auth.js: uses process.env.JWT_SECRET
- config.js: defines jwtSecret (different name)

You: 修复这个问题

Claude: I'll align the JWT secret configuration...
✓ Updated config.js
✓ Updated auth.js
Issue should be resolved now.

You: 测试一下

Claude: Testing authentication...
✓ Login successful
✓ Token validation working

场景 3:代码重构

You: src/api.js 代码太长了,需要重构

Claude: Let me analyze src/api.js...
File has 850 lines. I suggest splitting into:
- routes/users.js
- routes/products.js
- middleware/auth.js
- middleware/validation.js

You: 好的,开始重构

Claude: Refactoring src/api.js...
✓ Created routes/users.js (180 lines)
✓ Created routes/products.js (220 lines)
✓ Created middleware/auth.js (95 lines)
✓ Created middleware/validation.js (120 lines)
✓ Updated src/api.js (now 125 lines)

You: 确保测试仍然通过

Claude: Running tests...
✓ All 42 tests passed
Refactoring complete!

小结

在本章中,我们学习了:

  • ✅ 交互模式的概念和优势
  • ✅ 基本操作(发送消息、多行输入、查看历史)
  • ✅ 会话管理(保存、加载、删除会话)
  • ✅ 上下文管理(添加、移除、查看上下文)
  • ✅ 快捷键操作提高效率
  • ✅ 实战场景演示

关键要点:

  • 交互模式保持上下文,适合复杂任务
  • 善用会话保存功能
  • 注意管理上下文以控制成本
  • 熟练使用快捷键提高效率