第6集:iTerm2 — 15 年积累的终端之王
💡 进群学习加 wx: agentupdate
(申请发送: agentupdate)
(申请发送: agentupdate)
macOS 终端事实标准。tmux -CC 独家支持,AppleScript/Python API 最深可脚本化。
历史地位
iTerm2 自 2008 年发布,是 macOS 上最流行的终端模拟器。15 年的功能积累造就了最深的可脚本化能力。
安装方式
macOS(唯一平台):
# 方式 1:Homebrew(推荐)
brew install --cask iterm2
# 方式 2:官网下载
# https://iterm2.com/downloads.html
# 方式 3:手动下载 .zip
# https://iterm2.com/downloads/stable/latest
操作系统支持
| 系统 | 支持情况 | 备注 |
|---|---|---|
| macOS 10.14+ | 完整 | 原生 Cocoa 应用 |
| Linux | 不支持 | macOS 独占 |
| Windows | 不支持 | macOS 独占 |
杀手功能
1. tmux -CC 模式(独家)
iTerm2 独有的 tmux 集成。其他终端做不到。
# 普通 tmux:所有内容在一个窗口内,用 Ctrl+b 切换
tmux
# tmux -CC 模式:tmux 窗口变成 iTerm2 原生标签页
tmux -CC
效果对比:
普通 tmux:
┌─────────────────────────┐
│ [0:bash] [1:agent] [2:test] │ ← tmux 状态栏
│ ┌───────────────────────┐ │
│ │ 当前 pane 内容 │ │ ← 用 Ctrl+b 切换
│ └───────────────────────┘ │
└─────────────────────────┘
tmux -CC 模式(iTerm2):
┌─────────────────────────┐
│ bash │ agent │ test │ ← iTerm2 原生标签栏
│ ┌───────────────────────┐ │
│ │ 当前标签内容 │ │ ← Cmd+数字 切换
│ └───────────────────────┘ │
└─────────────────────────┘
每个 tmux window 变成 iTerm2 原生标签页,每个 pane 变成原生分屏。体验完全原生。
2. AppleScript 自动化
通过 AppleScript 完全控制 iTerm2:
-- 创建新标签并运行 Claude Code
tell application "iTerm2"
tell current window
set newTab to (create tab with default profile)
tell current session of newTab
write text "claude"
end tell
end tell
end tell
3. Python API
iTerm2 提供完整的 Python API(需安装 iTerm2 Python Runtime):
import iterm2
async def main(connection):
app = await iterm2.async_get_app(connection)
# 创建新标签
window = app.current_window
tab = await window.async_create_tab()
# 在新标签中运行命令
session = tab.current_session
await session.async_send_text("claude\n")
# 监听输出变化
@iterm2.RPC
async def on_output(session_id, text):
if "ERROR" in text:
# 自动通知
print(f"Agent 报错: {text}")
iterm2.run_forever(main)
4. Hotkey Window
全局快捷键呼出浮窗终端。Agent 工作时一键查看状态,用完一键隐藏。
5. Shell Integration
- Marks:标记命令位置,快速跳转
- 命令历史回放:回看之前所有命令和输出
- 目录跳转:
cmd+shift+h查看最近访问的目录
6. 无限滚动缓冲
Agent 输出再多也能翻回去。iTerm2 支持无限 scrollback(默认 1M 行可配)。
Agent 交互机制
iTerm2 拥有所有终端中最丰富的可编程接口:
flowchart TB
subgraph 自动化接口
A[AppleScript
完整终端控制]
B[Python API
profile/触发器/命令]
C[Shell Integration
命令标记/历史]
D[tmux -CC
原生标签体验]
end
subgraph Agent管理
E[Agent 1
Tab 1]
F[Agent 2
Tab 2]
G[Agent N
Tab N]
end
A -->|创建标签/分屏| E
A -->|创建标签/分屏| F
B -->|自动化脚本| E
B -->|自动化脚本| G
C -->|命令追踪| E
D -->|tmux原生体验| H[远程Agent]
style A fill:#2ecc71,color:#fff
style B fill:#4a9eff,color:#fff
style D fill:#9b59b6,color:#fffiTerm2 + tmux -CC 多 Agent 架构
sequenceDiagram
participant User as 开发者
participant IT as iTerm2
participant Tmux as tmux -CC
participant S1 as Session: agent-1
participant S2 as Session: agent-2
User->>IT: tmux -CC
IT->>Tmux: 启动 tmux -CC 模式
Tmux->>IT: 注册为原生标签页
User->>Tmux: tmux new -t agent-1
Tmux->>IT: 新增原生标签 "agent-1"
IT->>S1: 启动 Claude Code
User->>Tmux: tmux new -t agent-2
Tmux->>IT: 新增原生标签 "agent-2"
IT->>S2: 启动 Claude Code
Note over IT: Cmd+1 切换 agent-1
Cmd+2 切换 agent-2
体验完全原生
User->>IT: 合上 MacBook
Note over Tmux: tmux 会话继续运行
User->>IT: 打开 MacBook → tmux -CC
IT->>Tmux: 重新连接
Tmux->>IT: 恢复所有原生标签优缺点
| 优点 | 缺点 |
|---|---|
| tmux -CC 独家(最大卖点) | 仅 macOS |
| AppleScript + Python API 最深 | CPU 渲染(~3x 慢于 Ghostty) |
| 15 年功能积累 | 大量输出时卡顿 |
| 无限滚动缓冲 | 资源占用较高 |
| Shell Integration 成熟 | 无内置 AI 功能 |
| GPL 开源 | 无 Agent 通知机制 |
| 社区庞大 | 无垂直标签栏 |
适用场景
- macOS + tmux 重度用户
- 需要深度自动化(AppleScript/Python)
- 远程 Agent + tmux -CC 原生体验
- 不追求极致速度,追求生态深度