tmux

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

tmux 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
undefined
bash
undefined

Create 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"
undefined
tmux kill-session -t "$SESSION"
undefined

Remote Execution (Codespaces/SSH)

远程执行(Codespaces/SSH)

For mise-installed tools, wrap in zsh:
bash
undefined
对于通过mise安装的工具,使用zsh包裹命令:
bash
undefined

Non-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 -p

Tips

实用提示

  • Use
    -x 120 -y 40
    for consistent pane size
  • Poll with
    capture-pane -p
    rather than
    wait-for
  • Send literal text with
    -l
    flag to avoid shell expansion
  • Control keys:
    C-c
    (interrupt),
    C-d
    (EOF),
    Escape
  • For Python REPL: set
    PYTHON_BASIC_REPL=1
    to avoid fancy console interference
  • 使用
    -x 120 -y 40
    设置统一的窗格大小
  • 优先使用
    capture-pane -p
    轮询,而非
    wait-for
  • 搭配
    -l
    参数发送字面文本,避免shell展开
  • 控制按键:
    C-c
    (中断)、
    C-d
    (EOF)、
    Escape
  • 针对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 15

find-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