tmux

准備
tmux既可以進行遠程session的管理,即使ssh斷開,程序還會繼續運行,以前跑實驗要跑半個月的時候都是使用screen,可以防止網絡斷了程序跑一半的情況,現在發現tmux更強大。
和許多軟件一樣,需要在快捷鍵前加上前綴,習慣把默認的Ctrl+b 改為 Ctrl+a。
session
新建session,如果沒有-s,會以數字0, 1, 2...命名:
$ tmux new -s test
列出當前可用的session:
$ tmux ls
test: 1 windows (created Fri May 10 10:05:32 2019) [158x32]
zsh: 1 windows (created Fri May 10 09:50:32 2019) [158x32]
detach, 使用命令detach-client(可以簡寫為detach或det),直接關掉gnome-terminial也會自動detach:
prefix + d
tmux detach # detach當前tmux session
tmux detach -s test
連接session, 命令是attach-session(可以簡寫為attach或a):
tmux a -t test
如果session在其它地方被使用,可以使用-d在連接的同時,detach掉其它地方的連接,如果不detach掉的話,這邊的修改同時也會同步到其它地方:
tmux a -d -t test
列表顯示所有session:
prefix + a
重命令session:
tmux rename-session -t 4 nnjob
window
新建window
prefix + c
切換到下一個window:
prefix + n
切換到上一個window:
prefix + p
切換到指定window:
prefix + window的編號(如第0個窗口就是0)
關閉當前window
prefix + x
列表顯示當前window:
prefix + w
修改當前窗口名稱,以方便記住:
prefix + ,
panel
panel是tmux最具特色的功能,它可以在一個界面中顯示多個shell操作界面。
左右分屏:
prefix + |
上下分屏:
prefix + -
切換到下一個分屏:
prefix + o
切換:
prefix + ijlk
最大化顯示當前panel,很好用,再操作一次返回以前狀態:
prefix + z
問題
tmux有個痛點,也是我一度拋棄tmux的原因。那就是鼠標滾屏與鼠標選中中鍵復制功能不能兼得。
-
如果不開啟鼠標滾屏,可以使用選中復制。但是無法滾屏查看上面的信息,只能通過
prefix + PageUp
這樣的操作去查看。 -
如果開啟鼠標滾屏,那么就無法使用選中復制的功能,需要使用tmux提供的復制功能,一套新的復制系統,操作起來很別扭,即使改成vi的快捷鍵,實際上感覺並不好用。現在發現可以在選中和復制時分別按住Shift鍵來進行,感覺這樣的妥協還是可以接受的。
set -g mouse on
配置文件
tmux配置文件位置~/.tmux.conf:
set -g prefix ^a
unbind ^b
bind a send-prefix
setw -g mode-keys vi
set -g base-index 1
#水平或垂直分割窗口
unbind '"'
bind - splitw -v -c "#{pane_current_path}" # 分割成上下兩個窗口
unbind %
bind | splitw -h -c "#{pane_current_path}" # 分割成左右兩個窗口'"'
bind c new-window -c "#{pane_current_path}"
# 開啟鼠標模式
set-option -g mouse on
# 允許鼠標選擇窗格
setw -g mouse on
#set -g mouse-select-pane on
# 如果喜歡給窗口自定義命名,那么需要關閉窗口的自動命名
#set-option -g allow-rename off
# Scroll History
set -g history-limit 30000
# Set ability to capture on start and restore on exit window data when running an application
setw -g alternate-screen on
# Lower escape timing from 500ms to 50ms for quicker response to scroll-buffer access.
set -s escape-time 50
# 如果對 vim 比較熟悉,可以將 copy mode 的快捷鍵換成 vi 模式
set-window-option -g mode-keys vi
bind Escape copy-mode
bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection
unbind [
#unbind p
#bind p paste-buffer
#選擇分割的窗格
bind k selectp -U # 選擇上窗格
bind j selectp -D # 選擇下窗格
bind h selectp -L # 選擇左窗格
bind l selectp -R # 選擇右窗格
#重新調整窗格的大小
bind ^k resizep -U 10 # 跟選擇窗格的設置相同,只是多加 Ctrl(Ctrl-k)
bind ^j resizep -D 10 # 同上
bind ^h resizep -L 10 # ...
bind ^l resizep -R 10 # ...
#交換兩個窗格
bind ^u swapp -U # 與上窗格交換 Ctrl-u
bind ^d swapp -D # 與下窗格交換 Ctrl-d
# 狀態欄設置
# status bar with load and time
set -g status-bg blue
set -g status-fg '#bbbbbb'
set -g status-left-fg green
set -g status-left-bg blue
set -g status-right-fg green
set -g status-right-bg blue
set -g status-left-length 90
set -g status-right-length 90
set -g status-left '[#(whoami)]'
set -g status-right '[#(date +" %m-%d %H:%M ")]'
#set -g status-justify "centre"
set -g window-status-format '#I #W'
set -g window-status-current-format ' #I #W '
setw -g window-status-current-bg blue
setw -g window-status-current-fg green
set -g default-terminal "screen-256color"