Appearance
Cursor CLI 让你在终端里直接运行 Cursor AI Agent,无需打开 IDE。支持交互式会话、一行命令批处理、云端 Agent 推送三种用法,内置 Plan/Ask/Agent 三种模式,会话可以暂停恢复。本文是官方 CLI 文档的中文改写,介绍安装、启动、模式切换、会话管理,以及 CI/自动化场景下的非交互式用法。
Cursor CLI:在终端里运行 AI Agent
不想开 IDE 也能用 Cursor AI?CLI 版本让你在任何终端环境运行 Agent——本地开发、SSH 远程服务器、CI 流水线都可以。
安装
macOS / Linux / WSL:
bash
curl https://cursor.com/install -fsS | bashWindows PowerShell:
powershell
irm 'https://cursor.com/install?win32=true' | iex启动交互式会话
直接运行 agent 进入交互模式:
bash
agent也可以带上初始任务,直接开始执行:
bash
agent 'refactor the auth module to use JWT tokens'三种工作模式
| 模式 | 功能 | 激活方式 |
|---|---|---|
| Agent(默认) | 完整访问权限,可读写文件、执行命令 | 默认 |
| Plan | 先规划方案、澄清需求,不直接改代码 | Shift+Tab、/plan、--plan、--mode=plan |
| Ask | 只回答问题,不做任何代码改动 | /ask、--mode=ask |
非交互式执行(CI / 自动化)
适合脚本调用,执行完单个任务后退出:
bash
# 指定任务和模型
agent -p 'find and fix performance issues' --model 'claude-sonnet-4-6'
# Plan 模式生成方案文档
agent --mode=plan -p '设计新的缓存层架构'适合在 CI 流水线里做代码检查、自动修复等任务。
Cloud Agent:推送任务到云端
在任务前加 &,把任务推送到云端 Agent 异步执行(不占用本地资源):
bash
& refactor the auth module and add comprehensive tests
& run all tests and create a summary report适合长时间任务,本地可以继续干别的事。
会话管理
bash
# 列出所有历史会话
agent ls
# 恢复上次对话
agent resume
# 继续上次对话(另一种写法)
agent --continue
# 恢复特定会话(用 session ID)
agent --resume='chat-id-here'Sandbox 沙箱控制
控制 Agent 的代码执行环境:
bash
# 启用沙箱
agent --sandbox enabled
# 禁用沙箱
agent --sandbox disabled
# 会话中切换
/sandboxMax Mode
对高难度任务启用更强的模型推理:
bash
/max-mode on
/max-mode offsudo 支持
当 Agent 需要 sudo 权限时,CLI 会安全地提示输入密码,密码通过加密 IPC 通道直接传给 sudo,AI 模型不会看到密码内容。
与 Claude Code CLI 的对比
| Cursor CLI | Claude Code CLI | |
|---|---|---|
| 安装 | curl .../install | bash | npm install -g @anthropic-ai/claude-code |
| 命令 | agent | claude |
| 交互模式 | 支持 | 支持 |
| Plan Mode | --plan 或 /plan | Shift+Tab |
| 会话恢复 | agent resume | --resume |
| Cloud Agent | 支持(& 前缀) | 不支持 |
常见问题
Q: Cursor CLI 需要单独购买吗?
不需要,CLI 包含在 Cursor 订阅里,用同一账号登录即可。国内用户注意 CLI 安装时可能需要科学上网。
Q: 非交互式模式适合在 GitHub Actions 里用吗?
适合,但需要配置认证(环境变量传入 Cursor API Key)。适合做代码审查、自动修复小问题等任务,不建议让 CI 里的 Agent 直接推送代码。
Q: Cloud Agent 的任务在哪里看结果?
推送后会返回一个会话 ID,用 agent ls 列出或 agent --resume='session-id' 继续查看。