tmux
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesetmux Skill
tmux 技巧
Use tmux to control interactive terminal applications by sending keystrokes and capturing output.
使用tmux发送按键、捕获输出,以此来控制交互式终端应用。
When to Use
适用场景
- Running interactive REPLs (python, node, psql)
- Debugging with gdb/lldb
- Any CLI that requires TTY interaction
- Remote execution where you need to observe output
- 运行交互式REPL(python、node、psql)
- 使用gdb/lldb调试
- 任何需要TTY交互的CLI
- 需要观察输出的远程执行
Core Pattern
核心使用模式
bash
undefinedbash
undefinedCreate session
Create session
tmux new-session -d -s "$SESSION" -x 120 -y 40
tmux new-session -d -s "$SESSION" -x 120 -y 40
Send commands
Send commands
tmux send-keys -t "$SESSION" "python3" Enter
tmux send-keys -t "$SESSION" "python3" Enter
Capture output
Capture output
tmux capture-pane -t "$SESSION" -p
tmux capture-pane -t "$SESSION" -p
Wait for prompt (poll)
Wait for prompt (poll)
for i in {1..30}; do
output=$(tmux capture-pane -t "$SESSION" -p)
if echo "$output" | grep -q ">>>"; then break; fi
sleep 0.5
done
for i in {1..30}; do
output=$(tmux capture-pane -t "$SESSION" -p)
if echo "$output" | grep -q ">>>"; then break; fi
sleep 0.5
done
Cleanup
Cleanup
tmux kill-session -t "$SESSION"
undefinedtmux kill-session -t "$SESSION"
undefinedRemote Execution (Codespaces/SSH)
远程执行(Codespaces/SSH)
For mise-installed tools, wrap in zsh:
bash
undefined对于通过mise安装的工具,使用zsh包裹命令:
bash
undefinedNon-interactive (won't hang)
Non-interactive (won't hang)
ssh host 'zsh -c "source ~/.zshrc; tmux new-session -d -s mysession; tmux send-keys -t mysession python Enter"'
ssh host 'zsh -c "source ~/.zshrc; tmux new-session -d -s mysession; tmux send-keys -t mysession python Enter"'
Interactive (for tmux attach) - needs TTY
Interactive (for tmux attach) - needs TTY
ssh host -t 'zsh -ilc "tmux attach -t mysession"'
**Critical**: Use `zsh -c "source ~/.zshrc; ..."` not `zsh -lc` to avoid hangs.ssh host -t 'zsh -ilc "tmux attach -t mysession"'
**注意**:请使用`zsh -c "source ~/.zshrc; ..."`而非`zsh -lc`,避免命令挂起。User Notification
用户提示
After starting a session, ALWAYS print:
To monitor: tmux attach -t $SESSION
To capture: tmux capture-pane -t $SESSION -p启动会话后,请务必输出以下内容:
To monitor: tmux attach -t $SESSION
To capture: tmux capture-pane -t $SESSION -pTips
实用提示
- Use for consistent pane size
-x 120 -y 40 - Poll with rather than
capture-pane -pwait-for - Send literal text with flag to avoid shell expansion
-l - Control keys: (interrupt),
C-c(EOF),C-dEscape - For Python REPL: set to avoid fancy console interference
PYTHON_BASIC_REPL=1
- 使用设置统一的窗格大小
-x 120 -y 40 - 优先使用轮询,而非
capture-pane -pwait-for - 搭配参数发送字面文本,避免shell展开
-l - 控制按键:(中断)、
C-c(EOF)、C-dEscape - 针对Python REPL:设置避免花哨控制台的干扰
PYTHON_BASIC_REPL=1
Helper Scripts
辅助脚本
wait-for-text.sh
wait-for-text.sh
Poll tmux pane for a text pattern with timeout:
bash
scripts/wait-for-text.sh -t session:0.0 -p '^>>>' -T 15带超时轮询tmux窗格,匹配文本模式:
bash
scripts/wait-for-text.sh -t session:0.0 -p '^>>>' -T 15find-sessions.sh
find-sessions.sh
List tmux sessions, optionally filtered:
bash
scripts/find-sessions.sh -q claude # filter by name
scripts/find-sessions.sh --all # all sessions列出tmux会话,支持可选过滤:
bash
scripts/find-sessions.sh -q claude # filter by name
scripts/find-sessions.sh --all # all sessions