项目作者: huahuayu

项目描述 :
tmux烹饪书,cookbook,tmux by examples
高级语言:
项目地址: git://github.com/huahuayu/tmux-cookbook.git
创建时间: 2019-03-22T10:18:32Z
项目社区:https://github.com/huahuayu/tmux-cookbook

开源协议:

下载


tmux 烹饪书

tmux安装

ubuntu安装

apt install tmux

centos安装

yum install tmux安装的是1.8版,很老的版本,可以使用以下脚本安装2.8稳定版
install-tmux2.8.sh (https://gist.github.com/pokev25/4b9516d32f4021d945a140df09bf1fde)

  1. # Install tmux 2.8 on Centos
  2. # install deps
  3. yum install gcc kernel-devel make ncurses-devel
  4. # DOWNLOAD SOURCES FOR LIBEVENT AND MAKE AND INSTALL
  5. curl -LOk https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
  6. tar -xf libevent-2.1.8-stable.tar.gz
  7. cd libevent-2.1.8-stable
  8. ./configure --prefix=/usr/local
  9. make
  10. make install
  11. # DOWNLOAD SOURCES FOR TMUX AND MAKE AND INSTALL
  12. curl -LOk https://github.com/tmux/tmux/releases/download/2.8/tmux-2.8.tar.gz
  13. tar -xf tmux-2.8.tar.gz
  14. cd tmux-2.8
  15. LDFLAGS="-L/usr/local/lib -Wl,-rpath=/usr/local/lib" ./configure --prefix=/usr/local
  16. make
  17. make install
  18. # pkill tmux
  19. # close your terminal window (flushes cached tmux executable)
  20. # open new shell and check tmux version
  21. tmux -V

prefix = ctrl + b

tmux概念

一个ssh连接可有多个tmux session
session里面有windows
windows里面有panel
ssh掉线重登录tmux中的现场还在
tmux之所以能保存现场,因为其进程一直在后台运行

自动使用tmux

将以下配置加入服务器的~/.bash_profile~/.zsh_profile文件中, 只要一登录服务器就会自动使用tmux(Mac也适用)

  1. if [ -z "$TMUX" ]; then
  2. tmux attach -t 0 || tmux new -s 0
  3. fi

个人配置

config file

  1. # .tmux.conf
  2. # setw -g mode-keys vi
  3. # 使用`代替ctrl+b,按两下`输出`字符
  4. unbind ^b
  5. bind-key ` send-prefix
  6. set -g prefix `
  7. # `+r 应用tmux.conf文件
  8. bind r source-file ~/.tmux.conf \; display-message "Config reloaded"
  9. # 切分窗口,-横切,|竖切
  10. # unbind '"'
  11. bind - splitw -v -c '#{pane_current_path}'
  12. # unbind %
  13. bind | splitw -h -c '#{pane_current_path}'
  14. set-option -g mouse on
  15. # 绑定hjkl键为窗格切换的上下左右键
  16. bind -r k select-pane -U # 绑定k为↑
  17. bind -r j select-pane -D # 绑定j为↓
  18. bind -r h select-pane -L # 绑定h为←
  19. bind -r l select-pane -R # 绑定l为→
  20. # 绑定大写KJHL键为窗格上下左右调整边缘的快捷指令
  21. bind -r K resizep -U 10 # 绑定K为往↑调整面板边缘10个单元格
  22. bind -r J resizep -D 10 # 绑定J为往↓调整面板边缘10个单元格
  23. bind -r H resizep -L 10 # 绑定H为往←调整面板边缘10个单元格
  24. bind -r L resizep -R 10 # 绑定L往→调整面板边缘10个单元格
  25. # 设置tmux的延迟,文档说当有干扰的时候可以设置这个参数,比如影响vim编辑的时候
  26. set -s escape-time 1

热加载配置

prefix + : 进入命令模式然后输入
source-file ~/.tmux.conf

按我个人配置,后续只要prefix + r即可

系统操作

command usage
prefix + ? 列出所有快捷键;按q返回
prefix + d 脱离当前会话,返回Shell界面
prefix + D 选择要脱离的会话;在同时开启了多个会话时使用
prefix + Ctrl+z 挂起当前会话
prefix + : 进入命令行模式,此时可以输入tmux支持的命令
prefix + [ 进入复制模式;此时的操作与vi相同,按q/Esc退出

Session管理

新建session并命名

  1. tmux new -s myname

打开已存在的session

  1. tmux a -t session_name

断开session

  1. # 方式一
  2. exit # 如果只有单个tmux窗口,这样退出后tmux session就不存在了
  3. # 方式二
  4. prefix + d # d = detach, session保留

查看session列表

  1. tmux ls
  2. 0: 1 windows (created Wed Dec 5 16:14:47 2018) [232x56]
  3. 2: 2 windows (created Wed Dec 5 19:50:16 2018) [265x58]

删除session

  1. $ tmux ls
  2. 0: 2 windows (created Fri Jan 10 07:39:41 2020) [137x24]
  3. 1: 4 windows (created Fri Jan 10 07:40:09 2020) [137x24]
  4. 2: 1 windows (created Fri Jan 10 07:40:38 2020) [137x24]
  5. $ tmux kill-session -t 1
  6. $ tmux kill-session -t 2
  7. $ tmux ls
  8. 0: 2 windows (created Fri Jan 10 07:39:41 2020) [137x24]

窗口(如同浏览器tab)

新建窗口

  1. prefix + c

切换窗口

  1. prefix + [0~9]

重命名窗口

  1. prefix + ,

删除窗口

  1. prefix + &

窗格(一个大窗分成几个格)

水平分隔窗格

  1. prefix + -

垂直分隔窗格

  1. prefix + |

移动窗格

  1. prefix + hjkl
  2. prefix + ↑↓←→

缩放窗格

  1. prefix + z (z for zoom) # 有多个窗格的情况下,聚焦正在工作的窗格

置换窗格

  1. prefix + { # 向前置换
  2. prefix + } # 向后置换

调整窗格边界

  1. prefix + KJHL # 上下左右调整面板边界

嵌套tmux

不建议嵌套使用tmux,但是有时候会有从本地tmux ssh到远程server,然后又在远程server也使用tmux的情况,这样就会产生嵌套了。
如果恰好两个tmux的prefix键都是一样的,那么使用prefix键时将只对外层的tmux有效,没办法使用里层的tmux。
要解决这个问题,按两次prefix键即可(第二层按2次,第三层按3次,以此类推)。