Execute commands in a user-visible tmux pane to enable collaborative terminal operations.
Determine the target pane
The target pane is specified by the user or obtained through discovery:
tmux list-sessions -F "#S"
# PANE Title
tmux list-panes -a -F "#S:#I.#P #T"
# PANE Title CMD PWD
tmux list-panes -t main -F "#S:#I.#P #T #{pane_current_command} @ #{pane_current_path}"
The examples below use $PANE to represent the target pane (e.g., main:1.1)。
Key rules
| Rule | Description |
|---|---|
| Do not run interactive TUIs | Do not run vim, nano, top and other programs that require continuous interaction |
| Quote escaping | send-keys Pay attention to quote escaping in to prevent command format errors |
| Separate reading and writing | After sending a command,mustread the output to confirm the result; do not assume success |
Core operations
Execute command
# 可选:先清屏
tmux send-keys -t $PANE "clear" C-m
# 发送命令
tmux send-keys -t $PANE "你的命令" C-m
# 必须:读取输出确认
tmux capture-pane -pt $PANE -S -100
Read output
tmux capture-pane -pt $PANE -S -100 # 根据预期输出调整行数
Terminate process
tmux send-keys -t $PANE C-c
Common key presses
| Key | Effect |
|---|---|
C-c |
Interrupt process |
C-d |
EOF/exit |
C-z |
Suspend process |
C-l |
Clear screen |
Tip
- For long-running tasks, just confirm they have started; no need to wait for completion
- When the output is too long, increase the number of
-Slines (e.g.,-S -200) - Determine command completion: capture multiple times to check whether the shell prompt appears