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

什麼是 Tmux

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

此外即使 Terminal 意外關閉也沒關係,因爲 Session 完全保存在 Tmux Server 中。 再次打開 Terminal 時只需$ tmux attach便可回到你的工作區,就像從未退出過一樣。

同時 Tmux 還支持“遠程協助”,原本在服務器中,大家ssh上去之後都是互不干擾的操作,如果進入了相同的 Tmux Session,所有人看到的畫面都是一樣的,你可以看到別人的操作,別人也可以看到你的操作,你們的操作也會互相影響,一般在服務器出現難題需要人協助的時候,可以通過這個實時“直播”大神的操作。

https://harttle.land/assets/i...
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就可以了

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)

小結

平時總是太忙了,也沒什麼時間寫,今天總算是有時間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章