tmux
終端多工器,讓你在單一視窗管理多個終端會話
安裝
# macOS
brew install tmux
# Ubuntu/Debian
sudo apt install tmux
# Arch Linux
sudo pacman -S tmux基本概念
- Session : 工作區,可包含多個 window
- Window : 單一畫面,可包含多個 pane
- Pane : 分割的子視窗
會話管理
# 建立新會話
tmux
tmux new -s session-name
# 列出會話
tmux ls
# 附加到會話
tmux attach -t session-name
tmux a -t session-name
# 分離會話
# 在 tmux 中按 Ctrl+b d
# 終止會話
tmux kill-session -t session-name常用快捷鍵
所有快捷鍵前綴為 Ctrl+b
會話
| 按鍵 | 功能 |
|---|---|
d | 分離會話 |
s | 列出會話 |
$ | 重命名會話 |
視窗
| 按鍵 | 功能 |
|---|---|
c | 建立新視窗 |
n | 下一個視窗 |
p | 上一個視窗 |
0-9 | 切換到特定視窗 |
, | 重命名視窗 |
& | 關閉視窗 |
w | 視窗列表 |
窗格 (Pane)
| 按鍵 | 功能 |
|---|---|
% | 垂直分割 |
" | 水平分割 |
方向鍵 | 切換窗格 |
x | 關閉窗格 |
z | 最大化/還原窗格 |
{ / } | 交換窗格位置 |
空白鍵 | 切換佈局 |
複製模式
| 按鍵 | 功能 |
|---|---|
[ | 進入複製模式 |
q | 退出複製模式 |
Space | 開始選擇 |
Enter | 複製選擇 |
] | 貼上 |
配置
配置檔位置:~/.tmux.conf
# 設定前綴鍵為 Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# 啟用滑鼠
set -g mouse on
# 視窗索引從 1 開始
set -g base-index 1
setw -g pane-base-index 1
# 256 色支援
set -g default-terminal "screen-256color"
# 分割視窗快捷鍵
bind | split-window -h
bind - split-window -v
# vim 風格窗格切換
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# 快速重載配置
bind r source-file ~/.tmux.conf \; display "Reloaded!"實用技巧
共享會話
# 多人連接同一會話
tmux new -s shared
# 另一終端
tmux attach -t shared保存和恢復會話
使用 tmux-resurrect 插件:
# 保存: Ctrl+b Ctrl+s
# 恢復: Ctrl+b Ctrl+r與 SSH 搭配
ssh user@host -t "tmux attach || tmux new"