Upvotes
0
Upvotes: 0tmux 的介紹與用法
tmux 是一個終端機多工器(terminal multiplexer)。
它讓你在一個 terminal 裡同時開很多工作區,而且就算你把視窗關掉、SSH 斷線,工作也可以繼續留著。
你可以把它想成:
最常見的用途有這些:
例如你可以:
nvimpython train.pyhtoptail -f logs/app.log一個完整的 tmux 工作空間。
例如你可以有:
dev:開發用server:遠端主機維護train:模型訓練session 裡面的分頁,類似瀏覽器 tab。
window 裡面再切成多塊。
brew install tmux
sudo apt install tmux
sudo dnf install tmux
sudo pacman -S tmux
直接輸入:
tmux
這會開一個新的 session。
也可以指定名稱:
tmux new -s dev
這樣 session 名字叫 dev。
tmux 預設有一個前綴鍵(prefix key),預設是:
Ctrl-b
意思是很多快捷鍵都要先按 Ctrl-b,再按下一個鍵。
例如:
Ctrl-b c:新增 windowCtrl-b %:垂直分割Ctrl-b ":水平分割tmux new -s dev
tmux ls
tmux attach -t dev
按:
Ctrl-b d
這叫 detach。
tmux kill-session -t dev
tmux kill-server
Ctrl-b c
Ctrl-b n
Ctrl-b p
Ctrl-b 0
Ctrl-b 1
Ctrl-b 2
Ctrl-b ,
直接在那個 shell 裡輸入:
exit
Ctrl-b %
Ctrl-b "
Ctrl-b 方向鍵
exit
先按 prefix,再按:
Ctrl-b Ctrl-方向鍵
有些環境也常用:
Ctrl-b :
resize-pane -L 5
resize-pane -R 5
resize-pane -U 5
resize-pane -D 5
Ctrl-b z
這個很好用。
這是 tmux 最核心的功能。
你離開 tmux,但裡面程式照跑:
Ctrl-b d
tmux attach
如果有很多 session:
tmux attach -t dev
這對 SSH 很重要。 例如你在遠端跑訓練:
tmux new -s train
python train.py
中途網路斷掉沒關係,之後再登入:
tmux attach -t train
就能接回去。
tmux 可以像 pager 一樣往上捲、複製內容。
進入 copy mode:
Ctrl-b [
進去後你可以用方向鍵、PageUp/PageDown、Vi 風格移動。
離開通常按:
q
如果你有開 vi 風格設定,會更順手。
tmux new -s project
分成三塊:
nvimgit status / lazygitpython main.pytmux new -s server
開幾個 window:
htoptail -f /var/log/...tmux new -s exp1
python train.py > train.log 2>&1
然後 detach,之後再回來看。
tmux new -s dev # 新建 session
tmux ls # 列出 sessions
tmux attach -t dev # 接回 session
tmux kill-session -t dev # 刪除 session
tmux kill-server # 關掉所有 tmux
快捷鍵(預設 prefix = Ctrl-b):
Ctrl-b c 新增 window
Ctrl-b n 下一個 window
Ctrl-b p 上一個 window
Ctrl-b % 垂直分割 pane
Ctrl-b " 水平分割 pane
Ctrl-b 方向鍵 切換 pane
Ctrl-b d detach
Ctrl-b z 放大/還原 pane
Ctrl-b [ copy mode
Ctrl-b , 重新命名 window
因為它有這幾個很強的點:
不一定。
如果你是 detach,tmux 會留著。
如果你直接把整個 session 裡最後一個 shell exit 掉,tmux session 就會結束。
terminal 分頁通常是本機 GUI 功能;tmux 是 shell 層級。 所以 tmux 在 SSH、遠端主機、無圖形環境特別有價值。
因為要先按 prefix,也就是預設的 Ctrl-b。
你可以先只學這 6 個:
tmux new -s dev
Ctrl-b %
Ctrl-b "
Ctrl-b 方向鍵
Ctrl-b d
tmux attach -t dev
只會這幾個,就已經很夠用了。
tmux new -s test
進去後:
Ctrl-b % 切左右Ctrl-b " 切上下Ctrl-b + 方向鍵切換 panebash
watch -n 1 date
5. 按 Ctrl-b d 離開
6. 再輸入:
bash
tmux attach -t test
你就會理解 tmux 的核心價值。
如果你要,我下一則可以直接幫你整理成一份 「tmux 新手速查表」,或是幫你寫一份適合你工作流的 .tmux.conf。
Comments
Login to add a comment.
No comments yet.