本页目录15
- 用 @ 在提示词中直接注入文件或目录内容,省去手动复制的步骤
- `claude --continue` 或 `claude --resume` 可跨会话无缝恢复任务,无需重新解释背景
- `claude --worktree <name>` 开启隔离并行分支,多个终端同时开发互不干扰
- `claude --permission-mode plan` 让 Claude 先出方案再动文件,`Shift+Tab` 可在会话中随时切换
- 委托子代理探索大型代码库,主会话只收摘要,避免上下文膨胀
- `claude -p` 启用非交互管道模式,可无缝接入 CI 流水线和 pre-commit hook
本文是对 Claude Code 官方文档「Common workflows」的中文要点摘要,完整内容以原文为准:https://code.claude.com/docs/en/common-workflows
常用提示词模式
理解新代码库
进入项目根目录启动 claude,递进提问即可快速建立认知:
give me an overview of this codebase— 高层架构概览explain the main architecture patterns used here— 了解设计模式trace the login process from front-end to database— 追踪执行流程
建议先问宽泛问题,再聚焦到具体模块;使用项目自身的领域术语效果更佳。可安装对应语言的 code intelligence 插件,赋予 Claude「跳转到定义」和「查找引用」能力。
高效修复 Bug
将报错信息、复现命令和堆栈跟踪直接给 Claude:
I'm seeing an error when I run npm test
suggest a few ways to fix the @ts-ignore in user.ts
update user.ts to add the null check you suggested
提供可复现步骤并说明问题是偶发还是必现,有助于 Claude 给出更准确的修复方案。
重构代码
find deprecated API usage in our codebase
suggest how to refactor utils.js to use modern JavaScript features
refactor utils.js to use ES2024 features while maintaining the same behavior
run tests for the refactored code
建议分小步迭代,每步跑测试验证行为不变;需要保持向后兼容时,明确告知 Claude。
编写测试
find functions in NotificationsService.swift that are not covered by tests
add tests for the notification service
add test cases for edge conditions in the notification service
Claude 会参考现有测试文件的框架和断言风格生成用例;想覆盖边界值、错误路径等容易遗漏的场景,需主动让 Claude 分析代码路径给出补充用例。
创建 Pull Request
最简方式:直接输入 create a pr for my changes。或分步操作:先 summarize the changes I've made to the authentication module 概述改动,再用 create a pr 创建 PR,最后让 Claude 在 PR 描述中补充更多上下文(如 enhance the PR description with more context about the security improvements)。用 gh pr create 创建的 PR 会自动绑定当前会话,下次可用 claude --from-pr <number> 恢复,或把该 PR 的 URL 粘贴进 /resume 选择器的搜索框来恢复。
处理文档
find functions without proper JSDoc comments in the auth module
add JSDoc comments to the undocumented functions in auth.js
check if the documentation follows our project standards
可指定文档风格(JSDoc、docstrings 等),并要求附带使用示例。
图片与截图
三种输入方式:拖入窗口、Ctrl+V 粘贴、或在提示词中提供路径。Claude 可分析截图、设计稿、架构图,并生成对应代码(如 Generate CSS to match this design mockup)。当 Claude 引用图片标记(如 [Image #1])时,Mac 上 Cmd+Click、Windows/Linux 上 Ctrl+Click 可直接打开原图。
@ 引用文件与目录
在提示词中用 @ 快速注入内容,无需等待 Claude 主动读取:
| 写法 | 效果 |
|---|---|
@src/utils/auth.js | 包含文件全文 |
@src/components | 显示目录结构列表 |
@github:repos/owner/repo/issues | 读取 MCP 资源 |
路径支持相对路径和绝对路径,一条消息中可引用多个文件。
恢复上次会话
Claude Code 会在本地保存所有会话,跨多次工作时无需重新解释上下文:
claude --continue # 恢复当前目录最近一次会话
claude --resume # 弹出列表供选择
会话内也可随时使用 /resume 切换历史会话。
并行会话与 Worktrees
在不同终端同时处理多个独立任务,互不干扰:
claude --worktree feature-auth
每个 worktree 是独立分支的独立检出;可用 background agents 在单一屏幕上监控所有并行会话,无需来回切换终端。
Plan 模式:先规划再修改
不想让 Claude 直接动文件时,启用 plan 模式:
claude --permission-mode plan
也可在会话中按 Shift+Tab 随时切换。Claude 会读取相关文件并生成变更方案,等你批准后再执行;方案支持在外部文本编辑器中修改。
子代理委托调研
探索大型代码库会消耗主会话大量上下文,可将读文件工作委托给子代理:
use a subagent to investigate how our auth system handles token refresh
子代理在独立的上下文窗口中完成探索,只把摘要结果返回主会话,主上下文保持干净。
管道模式(非交互)
在 CI、pre-commit hook 或批处理场景中调用 Claude:
git log --oneline -20 | claude -p 'summarize these recent commits'
-p 启用非交互模式,标准输入/输出与普通 Unix 工具一致,支持自定义 permission flag 和扇出模式。
定时任务
官方提供四种定时运行方式:
| 方式 | 运行位置 | 适用场景 |
|---|---|---|
| Routines | Anthropic 云端 | 关机仍可运行,支持 API/GitHub 事件触发 |
| Desktop scheduled tasks | 本机桌面应用 | 需访问本地文件或未提交改动 |
| GitHub Actions | CI 流水线 | 与仓库事件绑定的 cron |
/loop | 当前 CLI 会话 | 会话内快速轮询,开启新对话即停止,--resume 或 --continue 可恢复尚未过期的任务 |
为定时任务编写提示词时,需明确说明成功标准和结果处理方式,因为任务运行中无法询问澄清问题。