Tmux配置文件大全(tmux-resurrect session自动保存)

一、是否需要Tmux

对于所有的程序猿来说shell terminal是每天都必须要接触的东西,但是经常需要在多个窗口之间切来切去真的很费手指,如果能有一款软件能在一个屏幕同时展示多个窗口是不是很方便呢?
如果你经常有以下需求,可以不妨尝试使用下Tmux:

  • 经常是不是需要搬着电脑去会议室或者其他地方的时候必不可免的会断网,连着的所有terminal都断了而必须重新登录;
  • 本来想执行一条命令就准备关机回家,然而命令执行时间太长而不敢关机,无奈只能等命令执行结束才能下班;
  • 在只有一个显示屏的情况下有时需要同时查看不同terminal的输出;
  • 需要在多个窗口同时输入同一命令;

二、Tmux安装

你可以获取到最新的tmux代码包:https://github.com/tmux/tmux

  • 安装gcc,libevent-devel,ncurses-devel
yum install gcc
yum install  libevent-devel
yum install ncurses-devel
  • 安装tmux
tar -zxvf tmux-1.6.tar.gz
cd tmux-1.6
./configure
make && make install
  • 如果报multiple definition
    把Makefile中的 am__append_2 = -std=c99这一行注释掉

三、tmux配置

在home目录新建.tmux.conf文件

set -g base-index 1
#允许鼠标操作
set -g mouse on  
set -g status-right "#{?pane_synchronized,[S],}[#(date)]" 
set -g status-interval 1
setw -g monitor-activity on
set -g history-limit 10000
set -g default-terminal "screen-256color"
#去除默认的前置键 ctrl + b
unbind C-b
#绑定前置键为 Alt + b
set -g prefix M-b
bind M-b send-prefix
#set -g set-remain-on-exit on
bind z confirm-before -p"kill-session #S? (y/n)" kill-session
#m键最大化窗口
bind m resize-pane -Z
# use twice to clear all buffer
#bind ` send C-l \; clearhist
bind -T root WheelUpPane copy-mode -e \; send-keys -M
#bind -t emacs-copy MouseDown3Pane cancel
unbind -n MouseDown3Pane
bind -T root MouseDown3Pane if-shell -F -t = "#{pane_in_mode}" "send-keys -M" "paste-buffer"
#+ 键将当前窗口分成4个窗口
bind + splitw -vp 50 \; splitw -hdp 50 \; selectp -U \; splitw -hdp 50
# | 垂直分割
bind | split-window -h
# - 水平分割
bind - split-window -v
# u 删除所有窗口
bind u killp -a -t 0
#bind r respawnp -k
#reload tmux config file
bind r source-file ~/.tmux.conf
bind F11 splitw -hp 67 \; splitw -hp 50
bind F10 splitw -vdp 50 \; splitw -hp 67 \; splitw -hp 50 \; selectp -U \; splitw -hp 67 \; splitw -hp 50
bind -T root C-F1 send 'tcpdump -i eth2 port 80 or 8080 or 443 -Sn' Enter
bind -T root F12  if -F "#{pane_synchronized}" "setw synchronize-panes off" "setw synchronize-panes on"
bind -T root C-6  send 'systemctl -l status nodesupervisor' Enter
setw -g mode-keys vi
# Vim style pane selection

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Use Alt-vim keys without prefix key to switch panes
# Alt + h 光标左移到左边窗口,无需提前按Alt + b
bind -n M-h select-pane -L
bind -n M-j select-pane -D
bind -n M-k select-pane -U
bind -n M-l select-pane -R
# Use Alt-arrow keys without prefix key to switch panes
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D

bind P paste-buffer
#bind-key -t vi-copy 'v' begin-selection
#bind-key -t vi-copy 'y' copy-selection
#bind-key -t vi-copy 'r' rectangle-toggle

# Shift arrow to switch windows
bind -n M-q  previous-window
bind -n M-w next-window

#alt +b, alt+K 键上移窗口
bind -r K resizep -U 10 # upward (prefix Ctrl+k)
bind -r J resizep -D 10 # downward (prefix Ctrl+j)
bind -r H resizep -L 10 # to the left (prefix Ctrl+h)
bind -r L resizep -R 10 # to the right (prefix Ctrl+l)

set -wg allow-rename off
set -wg automatic-rename off

以上的快捷键大多都需要提前按下前置键alt +b(默认为ctrl +b,上面配置有更改默认键),然后在2-3s时间内上面的设置的键才会生效;如果太慢,依然为普通的操作,不是tmux操作。

四、演示

示例图
四分窗口图

五、采坑记录

  1. 分割线乱码
    Putty:主要是因为putty的编码格式设置不对
    解决方法:
    • 选择Window-Translation配置页面,Remote character set 要设置成和服务器上一样的字符集(一般是utf-8),可以通过locale命令查看。
    • 勾上 Enable VT100 line drawing even in UTF-8 mode。

六、tmux-resurrect

由于有些时候tmux进程所在的server因为各种原因重启,这样就会导致我们使用了很久的tmux session全部丢失,在server起来后又不得不重新开一个tmux窗口并重新布置窗口,如果有一款工具能帮我们保存session样式并自动恢复,那将会节省很多气力,而tmux-resurrect就是为了这个目的而生的。

  1. 使用tmux-resurrect前置条件
    tmux-resurrect要求Tmux 版本 > v1.9; 可使用tmux -V查看当前tmux版本
    tmux必须在bash里新建session,否则restore会失败,save没问题
  2. 下载地址
    https://github.com/tmux-plugins/tmux-resurrect
  3. 解压tar包至指定目录
  4. .tmux.conf中添加tmux-resurrect
    比如tmux-resurrect在/home/eric/software/tmux-resurrect目录。
    run-shell /home/eric/software/tmux-resurrect/resurrect.tmux
    刷新tmux session以应用更新后的tmux config
  5. 保存session
    tmux prefix + ctrl s
  6. 恢复session
    若server 重启导致tmux丢失,则tmux new -s new新建一个tmux,之后用 tmux prefix + ctrl r 来恢复之前保存过的tmux session
posted @ 2020-09-04 13:30  无知是恶  阅读(952)  评论(0编辑  收藏  举报