用途
Cursor CLI Agent 技能用于指导开发者使用 Cursor 命令行工具(CLI)完成各类软件工程任务。它既可作为交互式编码助手使用,也可在 CI/CD 等非交互场景中自动化执行代码分析、测试与重构等操作。该技能内容已针对 2026 年 1 月的功能更新进行整理,并附带 tmux 自动化指南。
安装
标准安装(macOS、Linux、Windows WSL):
curl https://cursor.com/install -fsS | bash
Homebrew(仅 macOS):
brew install --cask cursor-cli
安装后设置:
macOS:在 ~/.zshrc(zsh)或 ~/.bashrc(bash)中添加 PATH:
export PATH="$HOME/.local/bin:$PATH"
重启终端或运行 source ~/.zshrc(或 ~/.bashrc)。要求 macOS 10.15 或更高版本,Intel 与 Apple Silicon Mac 均可使用。
Linux/Ubuntu:重启终端或重新加载 shell 配置,然后用 agent --version 验证。
两个平台通用:主命令为 agent,cursor-agent 保持向后兼容。验证安装:agent --version 或 cursor-agent --version。
认证
通过浏览器认证:
agent login
或使用 API Key:
export CURSOR_API_KEY=your_api_key_here
更新
保持 CLI 为最新版本:
agent update
# 或
agent upgrade
主要能力与命令
交互模式
启动交互式会话:
agent
带初始提示启动:
agent "Add error handling to this API"
向后兼容:cursor-agent 仍可用,但 agent 现为主命令。
模型切换
列出所有可用模型:
agent models
# 或
agent --list-models
使用指定模型:
agent --model gpt-5
会话中切换模型:
/models
会话管理
列出会话:agent ls
恢复最近会话:agent resume
恢复指定会话:agent --resume="[chat-id]"
上下文选择
在对话中包含指定文件或文件夹:
@filename.ts
@src/components/
斜杠命令
交互式会话中可用:
- /models - 交互式切换 AI 模型
- /compress - 总结对话并释放上下文窗口
- /rules - 直接在 CLI 中创建和编辑规则
- /commands - 创建和修改自定义命令
- /mcp enable [server-name] - 启用 MCP 服务器
- /mcp disable [server-name] - 禁用 MCP 服务器
键盘快捷键
- Shift+Enter - 多行提示换行
- Ctrl+D - 退出 CLI(需双击以确保安全)
- Ctrl+R - 审查更改(按 i 查看说明,用方向键导航)
- ArrowUp - 循环浏览历史消息
非交互 / CI 模式
在非交互模式下运行,适用于 CI/CD 流水线:
agent -p 'Run tests and report coverage'
# 或
agent --print 'Refactor this file to use async/await'
输出格式:
# 纯文本(默认)
agent -p 'Analyze code' --output-format text
# 结构化 JSON
agent -p 'Find bugs' --output-format json
# 实时流式 JSON
agent -p 'Run tests' --output-format stream-json --stream-partial-output
强制模式(自动应用更改,无需确认):
agent -p 'Fix all linting errors' --force
媒体支持:
agent -p 'Analyze this screenshot: screenshot.png'
与 AI 智能体 / 自动化配合使用(需要 tmux)
关键提示:在自动化环境(AI 智能体、脚本、子进程调用)中运行 Cursor CLI 时,CLI 需要真实的 TTY。直接执行会无限挂起。
解决方案:使用 tmux
# 1. 如未安装 tmux 则先安装
sudo apt install tmux # Ubuntu/Debian
brew install tmux # macOS
# 2. 创建 tmux 会话
tmux kill-session -t cursor 2>/dev/null || true
tmux new-session -d -s cursor
# 3. 进入项目目录
tmux send-keys -t cursor "cd /path/to/project" Enter
sleep 1
# 4. 运行 Cursor agent
tmux send-keys -t cursor "agent 'Your task here'" Enter
# 5. 处理工作区信任提示(首次运行)
sleep 3
tmux send-keys -t cursor "a" # 信任工作区
# 6. 等待完成
sleep 60 # 根据任务复杂度调整
# 7. 捕获输出
tmux capture-pane -t cursor -p -S -100
# 8. 验证结果
ls -la /path/to/project/
为何有效:tmux 提供持久伪终端(PTY);Cursor 的 TUI 需要交互式终端能力;从子进程/exec 直接调用 agent 在没有 TTY 时会挂起。
无效做法:
# ❌ 以下方式会无限挂起:
agent "task" # 无 TTY
agent -p "task" # 无 TTY
subprocess.run(["agent", ...]) # 无 TTY
script -c "agent ..." /dev/null # 可能导致 Cursor 崩溃
规则与配置
agent 会自动加载以下位置的规则:
- .cursor/rules
- AGENTS.md
- CLAUDE.md
使用 /rules 命令可直接在 CLI 中创建和编辑规则。
MCP 集成
MCP 服务器会从 mcp.json 配置自动加载。可即时启用/禁用:
/mcp enable server-name
/mcp disable server-name
注意:完全支持带空格的服务器名称。
工作流
代码审查
对当前更改或指定分支执行代码审查:
agent -p 'Review the changes in the current branch against main. Focus on security and performance.'
重构
为提升可读性或性能重构代码:
agent -p 'Refactor src/utils.ts to reduce complexity and improve type safety.'
调试
分析日志或错误信息以定位根因:
agent -p 'Analyze the following error log and suggest a fix: [paste log here]'
Git 集成
结合上下文自动化 git 操作:
agent -p 'Generate a commit message for the staged changes adhering to conventional commits.'
批处理(CI/CD)
在 CI 流水线中运行自动化检查:
# 在 CI 环境中设置 API Key
export CURSOR_API_KEY=$CURSOR_API_KEY
# 以 JSON 输出运行安全审计
agent -p 'Audit this codebase for security vulnerabilities' --output-format json --force
# 生成测试覆盖率报告
agent -p 'Run tests and generate coverage report' --output-format text
多文件分析
使用上下文选择分析多个文件:
agent
# 然后在交互模式中:
@src/api/
@src/models/
Review the API implementation for consistency with our data models
适用场景
- 交互式编码辅助:在终端中与 agent 对话完成代码修改、解释与重构。
- 自动化代码审查与安全审计:在 CI 中以非交互模式运行并输出 JSON 或文本报告。
- 测试与覆盖率报告生成。
- Git 提交信息生成等版本控制辅助。
- 多文件一致性分析。
- 在 AI 智能体或脚本中集成 Cursor CLI(需配合 tmux)。
限制
- 在自动化环境中直接调用 agent 会因缺少 TTY 而无限挂起,必须使用 tmux 提供伪终端。
- script -c 方式可能导致 Cursor 崩溃,不推荐使用。
- macOS 安装要求 10.15 或更高版本。
- 关于具体模型可用性、收费与价格、平台支持范围等细节,资料中暂未提供。