tmux的使用方法和個性化配置

介紹

tmux是一個優秀的終端複用軟件,即便非正常掉線,也能保證當前的任務運行,這一點對於遠程SSH訪問特別有用,網絡很差的狀況下仍然能保證工做現場不丟失!此外,tmux徹底使用鍵盤控制窗口,實現窗口的切換功能。html

簡單地說,tmux對於我主要有兩個功能(這應該也是tmux的主要功能):linux

  1. split窗口。能夠在一個terminal下打開多個終端,也能夠對當前屏幕進行各類split,便可以 同時打開多個顯示範圍更小的終端。
  2. 在 使用SSH的環境下,避免網絡不穩定,致使工做現場的丟失。想象如下場景, 你在執行一條命令的過程當中,因爲網絡不穩定,SSH鏈接斷開了。這個時候,你就不知道以前 的那條命令是否執行成功。若是此時你打開了不少文件,進入了較深層次的目錄,因爲網絡 不穩定,SSH鏈接斷開。從新鏈接之後,你又不得不從新打開那些文件,進入那個深層次的 目錄。若是使用了tmux,從新鏈接之後,就能夠直接回到原來的工做環境,不但提升了工做 效率,還下降了風險,增長了安全性。

 

我本身的tmux配置文件(~/.tmux.conf):git

# 修改配置文件以後,能夠在任何終端(包括tmux的終端)執行 tmux source-file ~/.tmux.conf 來當即使其生效,不用關閉session
# 可是要加載顏色等的更改,可能須要使用 tmux 或 tmux kill-session 或使用 () 來殺掉相應的session #設置前綴爲Ctrl
+ k set -g prefix C-k #解除Ctrl+b 與前綴的對應關係 unbind C-b # set -g default-terminal "screen-256color" set -g default-terminal "xterm-256color" # prefix l 是向右切換面板,Ctrl+l清屏。 #up bind-key k select-pane -U #down bind-key j select-pane -D #left bind-key h select-pane -L #right bind-key l select-pane -R # 鼠標啓用以後,終端不能再用鼠標選擇文件進行拷貝,所以禁用鼠標 # Make mouse useful in copy mode # setw -g mode-mouse on # Allow mouse to select which pane to use # set -g mouse-select-pane on # Allow mouse dragging to resize panes # set -g mouse-resize-pane on # Allow mouse to select windows # set -g mouse-select-window on # Scroll History set -g history-limit 90000 # 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 #能夠設置爲vi或emacs set-window-option -g mode-keys vi # bind -t vi-copy 'v' begin-selection bind-key -T copy-mode-vi v send-keys -X begin-selection # bind -t vi-copy 'y' copy-selection bind-key -T copy-mode-vi y send-keys -X copy-selection #開啓窗口的UTF-8支持 # It is no longer necessary for setting utf8 # set-window-option -g utf8 on # 安裝網址: https://github.com/tmux-plugins/tmux-resurrect run-shell ~/.tmux-resurrect/resurrect.tmux # set pane-active-border-bg "#ff0000" # quotes for rgb, no quotes for name # don't rename windows automatically set-option -g allow-rename off # Info on left (I don't have a session display for now) set -g status-left '' # loud or quiet? set-option -g visual-activity off set-option -g visual-bell off set-option -g visual-silence off set-window-option -g monitor-activity off set-option -g bell-action none # The modes { setw -g clock-mode-colour colour135 setw -g mode-attr bold setw -g mode-fg colour196 setw -g mode-bg colour238 # } # The panes { set -g pane-border-bg colour16 set -g pane-border-fg colour236 set -g pane-active-border-bg colour16 set -g pane-active-border-fg colour3 # } # The statusbar { set -g status-justify left set -g status-interval 2 set -g status-position bottom set -g status-bg colour234 set -g status-fg colour149 set -g status-attr dim set -g status-left '' # set -g status-right '#[fg=colour233,bg=colour19,bold] %d/%m #[fg=colour233,bg=colour8,bold] %H:%M:%S ' set -g status-right-length 50 set -g status-left-length 20 setw -g window-status-current-fg colour1 setw -g window-status-current-bg colour11 setw -g window-status-current-attr bold setw -g window-status-current-format ' #I#[fg=colour1]:#[fg=colour21]#W#[fg=colour21]#F ' setw -g window-status-fg colour243 setw -g window-status-bg colour234 setw -g window-status-attr none setw -g window-status-format ' #I#[fg=colour243]:#[fg=colour243]#W#[fg=colour243]#F ' setw -g window-status-bell-attr bold setw -g window-status-bell-fg colour255 setw -g window-status-bell-bg colour1 # } # The messages { set -g message-attr bold set -g message-fg colour11 set -g message-bg colour16 set -g message-command-fg blue set -g message-command-bg black # }tmux ls 來查看全部session,
# 而後kill-session -atmux kill-session -t targetSessiontargetSession 是整數

 

 

tmux快捷鍵:github

Ctrl+b 激活控制檯;此時如下按鍵生效
系統操做 ? 列出全部快捷鍵;按q返回
d 脫離當前會話;這樣能夠暫時返回Shell界面,輸入tmux attach可以從新進入以前的會話
D 選擇要脫離的會話;在同時開啓了多個會話時使用
Ctrl+z 掛起當前會話
r 強制重繪未脫離的會話
s 選擇並切換會話;在同時開啓了多個會話時使用
: 進入命令行模式;此時能夠輸入支持的命令,例如kill-server能夠關閉服務器
[ 進入複製模式;此時的操做與vi/emacs相同,按q/Esc退出
~ 列出提示信息緩存;其中包含了以前tmux返回的各類提示信息
窗口操做 c 建立新窗口
& 關閉當前窗口
數字鍵 切換至指定窗口
p 切換至上一窗口
n 切換至下一窗口
l 在先後兩個窗口間互相切換
w 經過窗口列表切換窗口
, 重命名當前窗口;這樣便於識別
. 修改當前窗口編號;至關於窗口從新排序
f 在全部窗口中查找指定文本
面板操做 將當前面板平分爲上下兩塊
% 將當前面板平分爲左右兩塊
x 關閉當前面板
! 將當前面板置於新窗口;即新建一個窗口,其中僅包含當前面板
Ctrl+方向鍵 以1個單元格爲單位移動邊緣以調整當前面板大小
Alt+方向鍵 以5個單元格爲單位移動邊緣以調整當前面板大小
Space 在預置的面板佈局中循環切換;依次包括even-horizontal、even-vertical、main-horizontal、main-vertical、tiled
q 顯示面板編號
o 在當前窗口中選擇下一面板
方向鍵 移動光標以選擇面板
{ 向前置換當前面板
} 向後置換當前面板
Alt+o 逆時針旋轉當前窗口的面板
Ctrl+o 順時針旋轉當前窗口的面板

 臨時將當前pannel全屏: CTRL+B Z,   再按一次恢復原來大小。shell

臨時執行命令:   prefix + :   ,這樣能夠臨時執行配置命令。   如執行    set status-style "bg=black"ubuntu

 

 

tmux 如何手動 save session:                https://superuser.com/questions/440015/restore-tmux-session-after-rebootvim

I wrote a simple bash script that persists open tmux sessions, windows and current working directories in each.windows

Call it like so manually or periodically from cron (because you might forget):緩存

tmux-session save

It will write to ~/.tmux-session. Restore them after reboot like so:安全

tmux-session restore

I find this much better than a several hundred line long Perl script.

save session 幫助用到的網址:

https://github.com/mislav/dotfiles/blob/d2af5900fce38238d1202aa43e7332b20add6205/bin/tmux-session

 

tmux 在pannel中滾動(後來備註:強烈推薦使用複製模式來實如今pannel中滾動):                          https://superuser.com/questions/209437/how-do-i-scroll-in-tmux

Ctrl-b then [ then you can use your normal navigation keys to scroll around (eg. Up Arrowor PgDn). Press q to quit scroll mode.

Alternatively you can press Ctrl-b PgUp to go directly into copy mode and scroll one page up (which is what it sounds like you will want most of the time)

In vi mode (see below), you can also scroll the page up/down line by line using Shift-k and Shift-j (if you're already in scroll mode). Unshifted, the cursor moves instead of the page.

Excerpts from the man page:

tmux may be controlled from an attached client by using a key combination of a prefix key, ‘C-b’ (Ctrl-b) by default, followed by a command key.

The default command key bindings are:

[           Enter copy mode to copy text or view the history.

Function                     vi              emacs
--------                     --              -----
Half page down               C-d             M-Down
Half page up                 C-u             M-Up
Next page                    C-f             Page down
Previous page                C-b             Page up
Scroll down                  C-Down or C-e   C-Down
Scroll up                    C-Up or C-y     C-Up
Search again                 n               n
Search again in reverse      N               N
Search backward              ?               C-r
Search forward               /               C-s

Plus a bunch more. Note that you have to press C-b twice if you use that for page up since C-bis bound as the command key. See the man page for information on prefacing a copy mode command with a repeat count.

You can set the key binding mode using Ctrl-b, then

:set-window-option mode-keys vi

or emacs.

 

 

tmux的複製模式(含滾動):

設置複製模式中的默認鍵盤佈局;能夠設置爲vi或emacs,我是把這個設置寫入了配置文件。
set-window-option -g mode-keys vi          #能夠設置爲vi或emacs
set-window-option -g utf8 on                    #開啓窗口的UTF-8支持
tmux複製模式的命令表:

       Function                        vi             emacs
       Back to indentation     ^              M-m
       Clear selection            Escape         C-g
       Copy selection            Enter          M-w
       Cursor down                j              Down
       Cursor left                     h              Left
       Cursor right                  l              Right
       Cursor to bottom line  L
       Cursor to middle line  M              M-r
       Cursor to top line         H              M-R
       Cursor up                      k              Up
       Delete entire line         d              C-u
       Delete to end of line   D              C-k
       End of line                    $              C-e
       Goto line                       :              g
       Half page down          C-d            M-Down
       Half page up               C-u            M-Up
       Next page                    C-f            Page down
       Next word                     w              M-f
       Paste buffer                  p              C-y
       Previous page             C-b            Page up
       Previous word              b              M-b
       Quit mode                     q              Escape
       Scroll down                   C-Down or J    C-Down
       Scroll up                        C-Up or K      C-Up
       Search again                n              n
       Search backward         ?              C-r
       Search forward             /               C-s
       Start of line                    0              C-a
       Start selection              Space          C-Space
       Transpose chars                           C-t

複製模式步驟:
個人控制鍵爲:C-k
一、 C-k [ 進入複製模式
二、 參考上表移動鼠標到要複製的區域,移動鼠標時可用vim的搜索功能"/","?"
三、 安空格鍵開始選擇複製區域
四、 選擇完成後安enter鍵複製並退出
五、 C-k ] 粘貼

 

 

tmux session的保存和恢復:

tmux-resurrect       https://github.com/tmux-plugins/tmux-resurrect             好用啊

tmuxinator       https://github.com/tmuxinator/tmuxinator                     基於 ruby,好像只能手動配置session,之後啓動自動加載這個session,但不能save工做中的session

 

 

tmux 設置界面的顏色:

首先必須參考 這篇文章 將ubuntu的fish shell terminal 設置爲256色。

https://github.com/seebi/dircolors-solarized                        裏面有顏色列表

https://github.com/seebi/tmux-colors-solarized

https://github.com/erikw/tmux-powerline 

可使用以下命令在bash shell裏打印各個顏色值:

for i in {0..255}; do
    printf "\x1b[38;5;${i}mcolour${i}\x1b[0m\n"
done
相關文章
相關標籤/搜索