使用GNU screen了很多年,猛然發現了一個比screen更好用的終端復用神器 -- tmux。本文將簡明扼要地介紹這一神器。
01 - 為什么要使用tmux?
和screen一樣,tmux允許一個任務持續運行,因此可以根據需要斷開連接(比如下班回家),而不中斷正在運行的任務。
02 - 基本用法
02.1 安裝tmux
- Fedora/CentOS
sudo dnf install tmux # Fedora sudo yum install tmux # CentOS
- Ubuntu
sudo apt install tmux
02.2 啟動與退出
如果在終端上鍵入命令tmux,就會開啟一個新的會話。如果退出這個會話,就可以回到正常的shell中。
$ tmux
或者
$ tmux new -s <session-name>
按下Ctrl+d或者顯式輸入exit命令,就可以退出tmux。
02.3 前綴鍵
和GNU screen一樣,tmux支持很多快捷鍵,而所有的快捷鍵都要通過前綴鍵喚起。GNU screen默認的前綴鍵是Ctrl+a, 而tmux默認的前綴鍵是Ctrl+b,即先按下Ctrl+b,快捷鍵才會生效。
03 - 基本概念
一個tmux會話(session)支持多個窗口(window), 一個tmux窗口支持多個窗格(pane)。
Basically, tmux sessions have windows, and windows have panes.
- session/window/pane from TMUX(1)
03.1 Session(會話)
會話(session)的本質就是一個連接,對應一個socket文件。打個比方,你拿起手機給別人打電話,電話一撥通,你們之間就建立了一個會話,只要手機不掛斷,會話就不停。
huanli@DELL380:~$ rm -rf /tmp/tmux-* && tmux new -s foo
[detached (from session foo)] huanli@DELL380:~$ file /tmp/tmux-1000/default /tmp/tmux-1000/default: socket
03.2 Window(窗口)
開啟一個會話(session)后,默認就會產生一個窗口(window),也可以增加新的窗口,一個窗口占據了整個屏幕。
在下圖中,開啟了三個窗口,活動窗口是"1: window1*"
03.3 Pane(窗格)
tmux的窗口(window)可以根據需要開啟多個窗格(pane),目前screen並不支持窗格,所以tmux更強大。
在下圖中,窗口1中開啟了三個窗格。
04 - 最簡操作流程
- 新建一個會話: tmux new -s foo
- 在tmux窗口運行所需的程序
- 按快捷鍵Ctrl+b d將會話分離,回到shell
- 再次使用時,重新連接到會話:tmux attach-session -t foo
05 - 會話(session)管理
- 新建會話:tmux new -s <session name>
- 分離會話:tmux detach
- 查看會話:tmux ls
- 接入會話:tmux attach -t <session-name OR session-id>
- 切換會話:tmux switch -t <session-name OR session-id>
- 殺掉會話:tmux kill-session -t <session-name OR session-id>
- 重命名會話:tmux rename-session -t <old-session-name> <new-session-name>
06 - 窗口(window)管理
- 新建窗口:tmux new-window OR tmux new-window -n <window-name>
- 切換窗口:tmux select-window -t <window-id OR window-name>
- 重命名窗口:tmux rename-window <new-window-name>
07 - 窗格(pane)管理
- 划分窗格:tmux split-window
# 上下划分 :tmux split-window # 左右划分 :tmux split-window -h
- 移動光標:tmux select-pane
# 光標切換到上方窗格: tmux select-pane -U # 光標切換到下方窗格: tmux select-pane -D # 光標切換到左邊窗格: tmux select-pane -L # 光標切換到右邊窗格: tmux select-pane -R
- 交換窗格:tmux swap-pane
# 當前窗格上移: tmux swap-pane -U
# 當前窗格下移: tmux swap-pane -D
08 - 快捷鍵 (來源戳這里)
These all play off of the Ctrl-b shortcut. Basics ? : get help Session management s : list sessions $ : rename the current session d : detach from the current session Windows c : create a new window , : rename the current window w : list windows % : split horizontally " : split vertically n : change to the next window p : change to the previous window 0 to 9 : select windows 0 through 9 Panes % : create a horizontal pane " : create a vertical pane h : move to the left pane. * j : move to the pane below * l : move to the right pane * k : move to the pane above * q : show pane numbers o : toggle between panes } : swap with next pane { : swap with previous pane ! : break the pane out of the window x : kill the current pane
09 - 定制$HOME/.tmux.conf
由於本人長期使用screen,習慣了快捷鍵前綴Ctrl+a,那么,可以將tmux的快捷鍵前綴Ctrl+b設置為Ctrl+a以實現使用習慣的平滑遷移。例如:
# Remap prefix from 'C-b' to 'C-a' unbind C-b set-option -g prefix C-a bind-key C-a send-prefix
將上述幾行保存到你的~/.tmux.conf中,就可以將快捷鍵習慣從screen平滑遷移到tmux上了。更多定制,請參見這里。
參考資料: