优雅地使用命令行: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

相关文章
相关标签/搜索