using-tmux-for-interactive-commands by obra/superpowers-lab
npx skills add https://github.com/obra/superpowers-lab --skill using-tmux-for-interactive-commands交互式 CLI 工具(vim、交互式 git rebase、REPL 等)无法通过标准 bash 进行控制,因为它们需要一个真实的终端。tmux 提供了可以通过 send-keys 和 capture-pane 以编程方式控制的分离会话。
在以下情况下使用 tmux:
git rebase -i、git add -p)不适用于:
| 任务 | 命令 |
|---|---|
| 启动会话 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
tmux new-session -d -s <name> <command> |
| 发送输入 | tmux send-keys -t <name> 'text' Enter |
| 捕获输出 | tmux capture-pane -t <name> -p |
| 停止会话 | tmux kill-session -t <name> |
| 列出会话 | tmux list-sessions |
# 这会挂起,因为 vim 期望交互式终端
bash -c "vim file.txt"
# 创建分离的 tmux 会话
tmux new-session -d -s edit_session vim file.txt
# 发送命令(Enter、Escape 是 tmux 键名)
tmux send-keys -t edit_session 'i' 'Hello World' Escape ':wq' Enter
# 捕获屏幕内容
tmux capture-pane -t edit_session -p
# 清理
tmux kill-session -t edit_session
send-keys(可以发送特殊键如 Enter、Escape)capture-pane -p 查看当前屏幕状态常见的 tmux 键名:
Enter - 回车/换行Escape - ESC 键C-c - Ctrl+CC-x - Ctrl+XUp, Down, Left, Right - 方向键Space - 空格键BSpace - 退格键创建会话时指定工作目录:
tmux new-session -d -s git_session -c /path/to/repo git rebase -i HEAD~3
为便于使用,请参阅 /home/jesse/git/interactive-command/tmux-wrapper.sh:
# 启动会话
/path/to/tmux-wrapper.sh start <session-name> <command> [args...]
# 发送输入
/path/to/tmux-wrapper.sh send <session-name> 'text' Enter
# 捕获当前状态
/path/to/tmux-wrapper.sh capture <session-name>
# 停止
/path/to/tmux-wrapper.sh stop <session-name>
tmux new-session -d -s python python3 -i
tmux send-keys -t python 'import math' Enter
tmux send-keys -t python 'print(math.pi)' Enter
tmux capture-pane -t python -p # 查看输出
tmux kill-session -t python
tmux new-session -d -s vim vim /tmp/file.txt
sleep 0.3 # 等待 vim 启动
tmux send-keys -t vim 'i' 'New content' Escape ':wq' Enter
# 文件现已保存
tmux new-session -d -s rebase -c /repo/path git rebase -i HEAD~3
sleep 0.5
tmux capture-pane -t rebase -p # 查看 rebase 编辑器
# 发送命令修改 rebase 指令
tmux send-keys -t rebase 'Down' 'Home' 'squash' Escape
tmux send-keys -t rebase ':wq' Enter
问题: 在 new-session 后立即捕获会显示空白屏幕
修复: 在首次捕获前添加短暂休眠(100-500ms)
tmux new-session -d -s sess command
sleep 0.3 # 让命令初始化
tmux capture-pane -t sess -p
问题: 命令已输入但未执行
修复: 显式发送 Enter
tmux send-keys -t sess 'print("hello")' Enter # 注意:Enter 是单独的参数
问题: tmux send-keys -t sess '\n' 无效
修复: 使用 tmux 键名:Enter,而不是 \n
tmux send-keys -t sess 'text' Enter # ✓
tmux send-keys -t sess 'text\n' # ✗
问题: 孤立的 tmux 会话会累积
修复: 完成后始终终止会话
tmux kill-session -t session_name
# 或检查是否存在:tmux has-session -t name 2>/dev/null
每周安装数
152
仓库
GitHub 星标数
238
首次出现
2026年1月21日
安全审计
安装于
opencode130
codex127
gemini-cli123
github-copilot117
claude-code109
cursor106
Interactive CLI tools (vim, interactive git rebase, REPLs, etc.) cannot be controlled through standard bash because they require a real terminal. tmux provides detached sessions that can be controlled programmatically via send-keys and capture-pane.
Use tmux when:
git rebase -i, git add -p)Don't use for:
| Task | Command |
|---|---|
| Start session | tmux new-session -d -s <name> <command> |
| Send input | tmux send-keys -t <name> 'text' Enter |
| Capture output | tmux capture-pane -t <name> -p |
| Stop session | tmux kill-session -t <name> |
| List sessions | tmux list-sessions |
# This hangs because vim expects interactive terminal
bash -c "vim file.txt"
# Create detached tmux session
tmux new-session -d -s edit_session vim file.txt
# Send commands (Enter, Escape are tmux key names)
tmux send-keys -t edit_session 'i' 'Hello World' Escape ':wq' Enter
# Capture what's on screen
tmux capture-pane -t edit_session -p
# Clean up
tmux kill-session -t edit_session
send-keys (can send special keys like Enter, Escape)capture-pane -p to see current screen stateCommon tmux key names:
Enter - Return/newlineEscape - ESC keyC-c - Ctrl+CC-x - Ctrl+XUp, Down, Left, Right - Arrow keysSpace - Space barBSpace - BackspaceSpecify working directory when creating session:
tmux new-session -d -s git_session -c /path/to/repo git rebase -i HEAD~3
For easier use, see /home/jesse/git/interactive-command/tmux-wrapper.sh:
# Start session
/path/to/tmux-wrapper.sh start <session-name> <command> [args...]
# Send input
/path/to/tmux-wrapper.sh send <session-name> 'text' Enter
# Capture current state
/path/to/tmux-wrapper.sh capture <session-name>
# Stop
/path/to/tmux-wrapper.sh stop <session-name>
tmux new-session -d -s python python3 -i
tmux send-keys -t python 'import math' Enter
tmux send-keys -t python 'print(math.pi)' Enter
tmux capture-pane -t python -p # See output
tmux kill-session -t python
tmux new-session -d -s vim vim /tmp/file.txt
sleep 0.3 # Wait for vim to start
tmux send-keys -t vim 'i' 'New content' Escape ':wq' Enter
# File is now saved
tmux new-session -d -s rebase -c /repo/path git rebase -i HEAD~3
sleep 0.5
tmux capture-pane -t rebase -p # See rebase editor
# Send commands to modify rebase instructions
tmux send-keys -t rebase 'Down' 'Home' 'squash' Escape
tmux send-keys -t rebase ':wq' Enter
Problem: Capturing immediately after new-session shows blank screen
Fix: Add brief sleep (100-500ms) before first capture
tmux new-session -d -s sess command
sleep 0.3 # Let command initialize
tmux capture-pane -t sess -p
Problem: Commands typed but not executed
Fix: Explicitly send Enter
tmux send-keys -t sess 'print("hello")' Enter # Note: Enter is separate argument
Problem: tmux send-keys -t sess '\n' doesn't work
Fix: Use tmux key names: Enter, not \n
tmux send-keys -t sess 'text' Enter # ✓
tmux send-keys -t sess 'text\n' # ✗
Problem: Orphaned tmux sessions accumulate
Fix: Always kill sessions when done
tmux kill-session -t session_name
# Or check for existing: tmux has-session -t name 2>/dev/null
Weekly Installs
152
Repository
GitHub Stars
238
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
opencode130
codex127
gemini-cli123
github-copilot117
claude-code109
cursor106