|
| 1 | +# Cursor CLI 安装与使用指南 |
| 2 | + |
| 3 | +> 在终端中直接使用 Cursor AI Agent,支持交互式对话与脚本自动化 |
| 4 | +
|
| 5 | +## 📋 目录 |
| 6 | + |
| 7 | +1. [简介](#简介) |
| 8 | +2. [安装](#安装) |
| 9 | +3. [验证安装](#验证安装) |
| 10 | +4. [交互式模式](#交互式模式) |
| 11 | +5. [非交互式模式(脚本/无头)](#非交互式模式脚本无头) |
| 12 | +6. [工作模式](#工作模式) |
| 13 | +7. [会话管理](#会话管理) |
| 14 | +8. [Cloud Agent 交接](#cloud-agent-交接) |
| 15 | +9. [常用场景示例](#常用场景示例) |
| 16 | +10. [故障排除](#故障排除) |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 简介 |
| 21 | + |
| 22 | +**Cursor CLI** 让你在终端里直接与 AI Agent 交互,用于编写、审查和修改代码。无论你喜欢在终端里对话,还是在脚本或 CI 里用“打印模式”自动化,CLI 都能在命令行里提供同样的能力。 |
| 23 | + |
| 24 | +- **交互式**:像和助手聊天一样描述目标、审查修改、批准命令 |
| 25 | +- **无头/脚本**:用 `-p` / `--print` 做非交互式任务(代码审查、安全扫描、批量处理等) |
| 26 | +- **多模型**:支持 Anthropic、OpenAI、Gemini 等前沿模型 |
| 27 | +- **环境无关**:可在任意 IDE 或终端使用(VS Code、JetBrains、Neovim 等) |
| 28 | + |
| 29 | +> **注意**:使用 Cursor CLI 需要有效的 Cursor 订阅;首次使用可能需登录 Cursor 账户完成认证。 |
| 30 | +
|
| 31 | +--- |
| 32 | + |
| 33 | +## 安装 |
| 34 | + |
| 35 | +### Windows(PowerShell) |
| 36 | + |
| 37 | +在 **PowerShell** 中执行(建议以管理员身份运行,以便写入 PATH): |
| 38 | + |
| 39 | +```powershell |
| 40 | +irm 'https://cursor.com/install?win32=true' | iex |
| 41 | +``` |
| 42 | + |
| 43 | +安装完成后,**重新打开终端**(或新开一个 PowerShell 窗口),再使用 `agent` 命令。 |
| 44 | + |
| 45 | +### macOS / Linux / WSL |
| 46 | + |
| 47 | +```bash |
| 48 | +curl https://cursor.com/install -fsS | bash |
| 49 | +``` |
| 50 | + |
| 51 | +安装后若找不到 `agent`,可重启终端或执行 `source ~/.bashrc` / `source ~/.zshrc`。 |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## 验证安装 |
| 56 | + |
| 57 | +在终端中运行: |
| 58 | + |
| 59 | +```bash |
| 60 | +agent --version |
| 61 | +``` |
| 62 | + |
| 63 | +若输出版本号(例如 `2026.01.28-fd13201`),说明安装成功。也可用 `agent -h` 或 `agent --help` 查看帮助。 |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## 交互式模式 |
| 68 | + |
| 69 | +在终端里和 Agent 对话,描述目标、查看建议、决定是否执行。 |
| 70 | + |
| 71 | +### 启动交互会话 |
| 72 | + |
| 73 | +```bash |
| 74 | +# 直接启动,进入对话 |
| 75 | +agent |
| 76 | + |
| 77 | +# 带初始提示启动 |
| 78 | +agent "把 auth 模块重构为使用 JWT" |
| 79 | +agent "给这个项目加单元测试" |
| 80 | +``` |
| 81 | + |
| 82 | +进入后可以连续输入多轮指令,Agent 会读文件、改代码、运行命令(经你确认),和 Cursor 编辑器里的 Agent 行为一致。 |
| 83 | + |
| 84 | +### 交互中的常用操作 |
| 85 | + |
| 86 | +- **发送任务**:输入自然语言描述,回车 |
| 87 | +- **审查修改**:Agent 会列出将要改动的文件与内容,按提示接受/拒绝 |
| 88 | +- **切换模式**:可用 `/plan`、`/ask` 等(见下方 [工作模式](#工作模式)) |
| 89 | +- **退出**:输入 `exit` 或按 `Ctrl+C` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## 非交互式模式(脚本/无头) |
| 94 | + |
| 95 | +适合脚本、CI、自动化流水线:只输出结果,不等待人工确认。 |
| 96 | + |
| 97 | +### 基本用法 |
| 98 | + |
| 99 | +使用 **打印模式**:`-p` 或 `--print`。 |
| 100 | + |
| 101 | +```bash |
| 102 | +# 只分析/回答,不改文件 |
| 103 | +agent -p "这个项目是做什么的?" |
| 104 | +agent -p "找出并说明性能问题" |
| 105 | + |
| 106 | +# 指定模型(若你的账户支持) |
| 107 | +agent -p "做一次安全审查" --model "gpt-5.2" |
| 108 | +``` |
| 109 | + |
| 110 | +### 在脚本中允许改文件 |
| 111 | + |
| 112 | +在打印模式下,**默认不会写文件**。若要在脚本里真正改代码,需加上 `--force`: |
| 113 | + |
| 114 | +```bash |
| 115 | +# 允许 Agent 直接修改文件(无确认) |
| 116 | +agent -p --force "把这段代码改成现代 ES6+ 语法" |
| 117 | + |
| 118 | +# 不写 --force 时,只会给出建议,不落盘 |
| 119 | +agent -p "给这个文件加 JSDoc 注释" |
| 120 | +``` |
| 121 | + |
| 122 | +### 输出格式 |
| 123 | + |
| 124 | +便于和脚本配合: |
| 125 | + |
| 126 | +```bash |
| 127 | +# 纯文本(默认),只要最终回答 |
| 128 | +agent -p "总结最近改动" --output-format text |
| 129 | + |
| 130 | +# 结构化 JSON |
| 131 | +agent -p "做代码审查并输出结构化结果" --output-format json |
| 132 | + |
| 133 | +# 流式 JSON,适合实时进度 |
| 134 | +agent -p --output-format stream-json --stream-partial-output "分析项目并生成报告" |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## 工作模式 |
| 140 | + |
| 141 | +CLI 与编辑器中的 Agent 模式一致,可切换: |
| 142 | + |
| 143 | +| 模式 | 说明 | 快捷方式 / 命令 | |
| 144 | +|--------|--------------------------|-------------------------| |
| 145 | +| Agent | 完整能力,可读改代码、跑命令 | 默认 | |
| 146 | +| Plan | 先规划再动手,多轮澄清 | `Shift+Tab`、`/plan`、`--mode=plan` | |
| 147 | +| Ask | 只读不写,仅探索与回答 | `/ask`、`--mode=ask` | |
| 148 | + |
| 149 | +示例: |
| 150 | + |
| 151 | +```bash |
| 152 | +# 以“只读”模式询问 |
| 153 | +agent --mode=ask "这段代码有没有内存泄漏风险?" |
| 154 | + |
| 155 | +# 先规划再执行 |
| 156 | +agent --mode=plan "重构用户认证模块" |
| 157 | +``` |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## 会话管理 |
| 162 | + |
| 163 | +可以恢复之前的对话,保持上下文。 |
| 164 | + |
| 165 | +```bash |
| 166 | +# 列出历史会话 |
| 167 | +agent ls |
| 168 | + |
| 169 | +# 恢复最近一次会话 |
| 170 | +agent resume |
| 171 | + |
| 172 | +# 恢复指定会话(用 chat-id) |
| 173 | +agent --resume="chat-id-here" |
| 174 | +``` |
| 175 | + |
| 176 | +--- |
| 177 | + |
| 178 | +## Cloud Agent 交接 |
| 179 | + |
| 180 | +把当前对话“交给” [Cloud Agent](https://cursor.com/docs/cloud-agent),在后台继续跑,你可以在 [cursor.com/agents](https://cursor.com/agents) 用网页或手机查看。 |
| 181 | + |
| 182 | +在**任意一条消息前加 `&`** 即可: |
| 183 | + |
| 184 | +```text |
| 185 | +& 把 auth 模块重构好并加上完整测试 |
| 186 | +``` |
| 187 | + |
| 188 | +--- |
| 189 | + |
| 190 | +## 常用场景示例 |
| 191 | + |
| 192 | +### 1. 快速问答(不改文件) |
| 193 | + |
| 194 | +```bash |
| 195 | +agent -p "这个代码库的主要功能是什么?" |
| 196 | +agent -p "解释 src/auth.js 里的登录逻辑" |
| 197 | +``` |
| 198 | + |
| 199 | +### 2. 代码审查(可写文件) |
| 200 | + |
| 201 | +```bash |
| 202 | +# 只输出审查意见 |
| 203 | +agent -p "对最近改动做代码审查,写进 review.txt" --output-format text |
| 204 | + |
| 205 | +# 允许直接改代码 |
| 206 | +agent -p --force "按审查意见修复 src/utils.js" |
| 207 | +``` |
| 208 | + |
| 209 | +### 3. 批量处理(脚本 + --force) |
| 210 | + |
| 211 | +```bash |
| 212 | +# 示例:给一批 JS 文件加 JSDoc(需根据实际路径调整) |
| 213 | +Get-ChildItem -Path src -Filter "*.js" -Recurse | ForEach-Object { |
| 214 | + agent -p --force "为 $($_.FullName) 添加完整的 JSDoc 注释" |
| 215 | +} |
| 216 | +``` |
| 217 | + |
| 218 | +### 4. 图片/媒体分析 |
| 219 | + |
| 220 | +在提示里写上文件路径,Agent 会通过工具读取并分析: |
| 221 | + |
| 222 | +```bash |
| 223 | +agent -p "分析这张图并描述内容:./screenshot.png" |
| 224 | +agent -p "对比这两张图:./before.png ./after.png" |
| 225 | +``` |
| 226 | + |
| 227 | +### 5. 与 Git 结合 |
| 228 | + |
| 229 | +```bash |
| 230 | +# 审查未提交的改动 |
| 231 | +agent -p "审查当前 git 改动,关注安全和可读性" |
| 232 | +``` |
| 233 | + |
| 234 | +--- |
| 235 | + |
| 236 | +## 故障排除 |
| 237 | + |
| 238 | +### 找不到 `agent` 命令 |
| 239 | + |
| 240 | +- **Windows**:安装后请**新开一个 PowerShell 窗口**,或检查 PATH 是否包含 Cursor CLI 所在目录 |
| 241 | +- **macOS/Linux**:执行 `source ~/.bashrc` 或 `source ~/.zshrc`,或重新打开终端 |
| 242 | + |
| 243 | +### 认证失败 / 未登录 |
| 244 | + |
| 245 | +- 确保已安装并登录过 **Cursor 桌面应用** |
| 246 | +- CLI 会复用 Cursor 的登录状态;若在无图形界面的环境(如 CI),需按官方文档配置 `CURSOR_API_KEY` 等(见 [Cursor 文档 - 认证](https://cursor.com/docs/cli/reference/authentication)) |
| 247 | + |
| 248 | +### 脚本里加了 `-p --force` 但没改文件 |
| 249 | + |
| 250 | +- 确认提示语里明确说了要“修改/写入/重写”等 |
| 251 | +- 确认路径和文件权限正确,Agent 有权限写目标文件 |
| 252 | + |
| 253 | +### 更多帮助 |
| 254 | + |
| 255 | +- 命令帮助:`agent -h` / `agent --help` |
| 256 | +- 官方文档:[Cursor CLI 概述](https://cursor.com/docs/cli/overview)、[无头模式](https://cursor.com/docs/cli/headless) |
| 257 | + |
| 258 | +--- |
| 259 | + |
| 260 | +## 总结 |
| 261 | + |
| 262 | +| 需求 | 推荐用法 | |
| 263 | +|----------------|----------| |
| 264 | +| 终端里对话、改代码 | `agent` 或 `agent "任务描述"` | |
| 265 | +| 脚本里只读/分析 | `agent -p "问题"` | |
| 266 | +| 脚本里自动改文件 | `agent -p --force "任务"` | |
| 267 | +| 指定模型/输出格式 | `--model "模型名"`、`--output-format text/json/stream-json` | |
| 268 | +| 恢复历史会话 | `agent resume` 或 `agent --resume=id` | |
| 269 | +| 交给云端继续跑 | 在消息前加 `&` | |
| 270 | + |
| 271 | +安装好 Cursor CLI 后,在项目目录下直接运行 `agent` 即可开始使用。 |
| 272 | + |
| 273 | +--- |
| 274 | + |
| 275 | +<div style="display: flex; justify-content: space-between; align-items: center; margin: 40px 0; padding: 20px; background: #f5f5f5; border-radius: 8px; flex-wrap: wrap;"> |
| 276 | + <div style="flex: 1; min-width: 150px;"> |
| 277 | + <p style="margin: 0; color: #666; font-size: 14px;">上一篇</p> |
| 278 | + <p style="margin: 5px 0 0 0;"> |
| 279 | + <a href="quick-reference.md" style="color: #0366d6; text-decoration: none; font-weight: 500;">← Cursor 快速参考手册</a> |
| 280 | + </p> |
| 281 | + </div> |
| 282 | + <div style="flex: 1; text-align: center; min-width: 150px;"> |
| 283 | + <p style="margin: 0;"> |
| 284 | + <a href="../README.md" style="color: #0366d6; text-decoration: none; font-weight: 500;">🏠 返回教程索引</a> |
| 285 | + </p> |
| 286 | + </div> |
| 287 | + <div style="flex: 1; text-align: right; min-width: 150px;"> |
| 288 | + <p style="margin: 0; color: #666; font-size: 14px;">下一篇</p> |
| 289 | + <p style="margin: 5px 0 0 0;"> |
| 290 | + <a href="advanced-features.md" style="color: #0366d6; text-decoration: none; font-weight: 500;">Cursor 高级功能指南 →</a> |
| 291 | + </p> |
| 292 | + </div> |
| 293 | +</div> |
0 commit comments