# CLI 与执行模式

> 安装与使用 Amp CLI、账号管理、IDE 连接、在本机 / orb / runner 上运行线程，以及 -x 执行模式在脚本和 CI 中的用法。

- 网址：https://funcoding.ai/agents/amp/usage/cli/
- 核实日期：2026-10-18（命令、配置和价格以官方文档为准）
- 官方来源：[Amp 文档：CLI Getting Started](https://ampcode.com/docs/cli)、[Amp 文档：Execute Mode](https://ampcode.com/docs/cli/execute-mode)

---
Amp CLI 在终端里运行，支持 macOS、Linux 和通过 WSL 的 Windows（Windows 上建议用 WezTerm 或 Alacritty 以获得完整的剪贴板支持）。安装命令以官方安装页为准（工作区可能有自己的安装说明）。

```bash
amp                      # 交互模式
echo "commit all my changes" | amp   # 管道输入作为第一条消息
amp --help
```

- **账号**：`amp login` 添加账号并设为当前，`amp account list` / `amp account switch <邮箱或用户ID>` 切换，`amp logout` 退出；每个进程保持启动时的账号。设置了 `AMP_API_KEY` 时它优先于已保存账号。
- **补全**：`source <(amp completions bash|zsh)`，Fish 和 PowerShell 同理。
- **图片**：`Ctrl+V` 粘贴剪贴板图片（macOS 上也是 Ctrl+V，不是 Cmd+V），也可以拖拽图片文件到终端。
- **在哪里运行**：`amp --executor local|orb|runner:<id>`；runner 用 `amp --no-tui --runner-id <id>` 启动。
- **更新**：自动在后台检查安装，也可 `amp update`、`amp version`。
- **连接编辑器**：VS Code、Cursor、Windsurf、Zed、Neovim（Neovim 另需 amp.nvim 插件）。打开编辑器后运行 `amp`，`Ctrl+O` 打开命令面板选 `ide connect`；Amp 能看到当前文件和选区，并通过 IDE 编辑文件（支持撤销）。

## 执行模式

`-x` / `--execute` 把消息发给智能体，等它结束这一轮、打印最终消息并退出：

```bash
amp -x "what files in this folder are markdown files? Print only the filenames."
cat ~/.vimrc | amp -x "which colorscheme is used?"
amp -ox "Investigate why the latest CI run on main failed"   # 在 orb 里跑，立即返回线程 URL
amp -x "Run the tests and fix any failures" --executor runner:mac-mini
```

重定向 stdout 时执行模式会自动开启。`--fast` 为单次调用启用 Fast 模式；`--mcp-config '<JSON>'` 临时指定 MCP 服务器而不改设置；`--attach <path>` 为 runner 线程上传文件；`--plugin-ready-timeout [秒]` 让执行模式等插件就绪（依赖 `agent.start`/`agent.end` 事件的插件需要）；`--timings` 在退出时把各阶段耗时打印到 stderr，回复仍在 stdout，可以和 `--stream-json`、重定向一起用。**CI 等非交互环境**：设置环境变量 `AMP_API_KEY` 为设置页里生成的访问令牌（以 `sgamp_` 开头），不接受 `amp login` 存的短期会话令牌。另有 TypeScript 和 Python 的 SDK 以及 `--stream-json` 流式输出，见官方 SDK 文档。
