tmux

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

tmux

tmux

Terminal multiplexer for background processes, output capture, and session management.
用于后台进程管理、输出捕获和会话管理的终端复用工具。

Quick Reference

快速参考

CommandDescription
tmux new -d -s name 'cmd'
Run command in background session
tmux capture-pane -t name -p
Capture output from session
tmux send-keys -t name 'text' Enter
Send input to session
tmux kill-session -t name
Terminate session
tmux ls
List all sessions
tmux has -t name
Check if session exists
命令说明
tmux new -d -s name 'cmd'
在后台会话中运行命令
tmux capture-pane -t name -p
捕获会话中的输出
tmux send-keys -t name 'text' Enter
向会话发送输入内容
tmux kill-session -t name
终止会话
tmux ls
列出所有会话
tmux has -t name
检查会话是否存在

Running Background Processes

运行后台进程

bash
undefined
bash
undefined

Run command in new detached session

在新的分离会话中运行命令

tmux new-session -d -s myserver 'python -m http.server 8080'
tmux new-session -d -s myserver 'python -m http.server 8080'

With specific working directory

指定工作目录

tmux new-session -d -s build -c /path/to/project 'make build'
tmux new-session -d -s build -c /path/to/project 'make build'

Keep session alive after command completes

命令执行完成后保持会话活跃

tmux new-session -d -s task 'command; exec bash'
tmux new-session -d -s task 'command; exec bash'

Run only if session doesn't exist

仅在会话不存在时运行

tmux has -t myserver || tmux new-session -d -s myserver 'command'
undefined
tmux has -t myserver || tmux new-session -d -s myserver 'command'
undefined

Capturing Output

捕获输出

bash
undefined
bash
undefined

Capture visible output

捕获可见输出

tmux capture-pane -t mysession -p
tmux capture-pane -t mysession -p

Capture entire scrollback history

捕获整个回滚历史

tmux capture-pane -t mysession -p -S -
tmux capture-pane -t mysession -p -S -

Capture last N lines

捕获最后N行内容

tmux capture-pane -t mysession -p -S -100
tmux capture-pane -t mysession -p -S -100

Save to file

保存到文件

tmux capture-pane -t mysession -p > output.txt
tmux capture-pane -t mysession -p > output.txt

Capture with escape sequences (colors)

捕获包含转义序列(颜色)的输出

tmux capture-pane -t mysession -p -e
undefined
tmux capture-pane -t mysession -p -e
undefined

Sending Input

发送输入

bash
undefined
bash
undefined

Send text and Enter

发送文本并执行(按下回车)

tmux send-keys -t mysession 'echo hello' Enter
tmux send-keys -t mysession 'echo hello' Enter

Send without Enter

发送文本但不执行

tmux send-keys -t mysession 'some-text'
tmux send-keys -t mysession 'some-text'

Send Ctrl+C

发送Ctrl+C

tmux send-keys -t mysession C-c
undefined
tmux send-keys -t mysession C-c
undefined

Session Management

会话管理

bash
undefined
bash
undefined

List sessions

列出会话

tmux list-sessions tmux ls
tmux list-sessions tmux ls

Kill specific session

终止指定会话

tmux kill-session -t myserver
tmux kill-session -t myserver

Kill all sessions

终止所有会话

tmux kill-server
tmux kill-server

Check if session exists

检查会话是否存在

tmux has -t mysession
undefined
tmux has -t mysession
undefined

Wait for Completion

等待任务完成

bash
undefined
bash
undefined

Signal completion from command

从命令发送完成信号

tmux new-session -d -s job 'command; tmux wait-for -S job-done'
tmux new-session -d -s job 'command; tmux wait-for -S job-done'

Wait for signal

等待完成信号

tmux wait-for job-done
undefined
tmux wait-for job-done
undefined

Common Patterns

常见使用模式

Development Servers

开发服务器

bash
tmux new-session -d -s backend 'bun run backend'
tmux new-session -d -s frontend 'bun run frontend'
tmux new-session -d -s tests 'vitest --watch'
bash
tmux new-session -d -s backend 'bun run backend'
tmux new-session -d -s frontend 'bun run frontend'
tmux new-session -d -s tests 'vitest --watch'

Run and Capture Output

运行并捕获输出

bash
tmux new-session -d -s job 'command'
sleep 0.5
output=$(tmux capture-pane -t job -p)
echo "$output"
bash
tmux new-session -d -s job 'command'
sleep 0.5
output=$(tmux capture-pane -t job -p)
echo "$output"

Conditional Session

条件式创建会话

bash
tmux has -t myserver || tmux new-session -d -s myserver 'command'
bash
tmux has -t myserver || tmux new-session -d -s myserver 'command'

Cleanup

清理会话

bash
tmux kill-session -t backend
tmux kill-session -t frontend
tmux kill-server  # Kill all
bash
tmux kill-session -t backend
tmux kill-session -t frontend
tmux kill-server  # 终止所有会话

Tips

小贴士

  • Use
    tmux new-session -d
    for background processes
  • Use
    tmux capture-pane -p -S -
    for full scrollback
  • Use
    tmux has -t name
    to check session existence
  • Use
    tmux kill-server
    to clean up all sessions
  • Use
    -c /path
    to set working directory
  • Use
    exec bash
    to keep session alive after command
  • 使用
    tmux new-session -d
    来运行后台进程
  • 使用
    tmux capture-pane -p -S -
    来捕获完整的回滚历史
  • 使用
    tmux has -t name
    来检查会话是否存在
  • 使用
    tmux kill-server
    来清理所有会话
  • 使用
    -c /path
    来设置工作目录
  • 使用
    exec bash
    在命令执行完成后保持会话活跃