優雅地使用命令行:Tmux 終端複用

什麼是 Tmux

你是否曾經開過一大堆的 Terminal?有沒有把它們都保存下來的衝動?Tmux 的Session就是作這件事情的! 你能夠隨時退出或者進入任何一個 Session。每一個 Session 有若干個 Window,每一個 Window 又能夠分紅多個窗格(Pane)。 極大地知足 Terminal 用戶的需求。shell

此外即便 Terminal 意外關閉也不要緊,由於 Session 徹底保存在 Tmux Server 中。 再次打開 Terminal 時只需$ tmux attach即可回到你的工做區,就像從未退出過同樣。後端

同時 Tmux 還支持「遠程協助」,本來在服務器中,你們ssh上去以後都是互不干擾的操做,若是進入了相同的 Tmux Session,全部人看到的畫面都是同樣的,你能夠看到別人的操做,別人也能夠看到你的操做,大家的操做也會互相影響,通常在服務器出現難題須要人協助的時候,能夠經過這個實時「直播」大神的操做。bash

clipboard.png

分享個人 Tmux 配置

由於是小公司,沒有運維,做爲後端,也不可避免的負責了一部分運維的工做,還好本身Linux的操做還算熟悉,就寫了一個本身用的 Tmux 配置,不必定適合全部人,可是我我的用的確實挺順手。配置的快捷鍵是基於 Emacs 快捷鍵操做的( Emacs 絕對是最好的編輯器,哈哈哈 )服務器

######################
### DESIGN CHANGES ###
######################
set -g bell-action any
set -g terminal-overrides 'rxvt-unicode*:sitm@,ritm@'
set -g default-terminal "screen-256color"

unbind C-b
set -g default-shell /bin/bash
set -g prefix C-x
set -g status-keys emacs
set -gw mode-keys emacs

set-option -g default-command "which reattach-to-user-namespace > /dev/null && reattach-to-user-namespace -l $SHELL || $SHELL"
# bind-key -t emacs-copy M-w copy-pipe 'reattach-to-user-namespace pbcopy'
bind-key -n C-y paste-buffer

bind-key C-x send-prefix
set-option -g history-limit 30000
bind r source-file ~/.tmux.conf \; display-message "Reloading tmux config ..."


bind-key -nr C-M-P resize-pane -U 5
bind-key -nr C-M-N resize-pane -D 5
bind-key -nr C-M-F resize-pane -R 5
bind-key -nr C-M-B resize-pane -L 5

bind-key -n M-p select-pane -U
bind-key -n M-n select-pane -D

bind-key -nr M-o display-panes

bind-key 2 split-window
bind-key 3 split-window -h

bind-key -nr M-, previous-window
bind-key -nr M-. next-window


# start window indexing at one instead of zero
set-option -g base-index 1
set-window-option -g pane-base-index 1

# enable wm window titles
set -g set-titles on
# wm window title string (uses statusbar variables)
set -g set-titles-string "tmux:#I #W"
# session initialization

set -g @tpm_plugins "          \
  tmux-plugins/tpm             \
  tmux-plugins/tmux-sensible   \
"

set -g @tpm_plugins "          \
  tmux-plugins/tpm             \
  tmux-plugins/tmux-copycat    \
"

# set -g @plugin 'jimeh/tmux-themepack'
# set -g @themepack 'powerline/default/gray'
# set -g @plugin 'seebi/tmux-colors-solarized'
# set -g @colors-solarized 'dark'

# enable wm window titles
set -g set-titles on
# wm window title string (uses statusbar variables)
set -g set-titles-string "tmux:#I #W"

# Default termtype. If the rcfile sets $TERM, that overrides this value.
set -g default-terminal screen-256color

set -g status-left-length 52
set -g status-right-length 451
set -g status-fg white
set -g status-bg colour234
set -g pane-border-fg colour245
set -g pane-active-border-fg colour39
set -g message-fg colour16
set -g message-bg colour221
set -g message-attr bold

set -g status-left '#[fg=colour235,bg=colour252,bold] ❐ #S #[fg=colour252,bg=colour238,nobold]#[fg=colour245,bg=colour238,bold] #(whoami) #[fg=colour238,bg=colour234,nobold]'
set -g window-status-current-format "#[fg=colour234,bg=colour39]#[fg=black,bg=colour39,noreverse,bold] #I: #W #[fg=colour39,bg=colour234,nobold]"

set -sg escape-time 0

把這份配置寫入~/.tmux.conf文件,而後$ pkill tmux && tmux就能夠了session

Tmux 快捷鍵

我主要是把 Tmux 的 Ctrl+b 換成了 Emacs 的 Ctrl+x,其餘的快捷鍵沒怎麼修改,新增了一些 Emacs 上經常使用的快捷鍵,下面說說幾個經常使用的快捷鍵,其他的你們能夠看配置文件
ps:ctrl+x是先按ctrl,再按x,並非按住,ctrl-alt纔是按住不放運維

// pane 操做
ctrl+x 2   //上下分屏
ctrl+x 3   //左右分屏
ctrl+x x   //刪除當前pane
ctrl+x o   //pane切換
alt+n      //下一個pane
alt+p      //上一個pane
ctrl-alt p //當前pane向上移動
ctrl-alt n //當前pane向下移動
ctrl-alt f //當前pane向右移動
ctrl-alt b //當前pane向左移動
ctrl+x 空格 //切換pane佈局

// window 操做
ctrl+x c   //新建window
ctrl+x n   //下一個window
ctrl+x p   //上一個window
ctrl+x w   //顯示window列表

// session 操做
ctrl+x s   //顯示session列表
ctrl+x d   //優雅退出tmux(保留session)

小結

tmux的應用還有不少,好比在運行服務的時候不再須要$ nohup /root/test_srv &......
平時老是太忙了,也沒什麼時間寫,今天總算是有時間寫點什麼,這個只是開頭,之後有時間,我會把我這些年開發用到的一些東西分享給你們。
我始終以爲,後端就應該擁抱Linux,能純鍵盤+命令行操做效率真的是過高了。ssh

相關文章
相關標籤/搜索