基本概念
另见终端概述
介绍 Session, 一组窗口的集合(windows集合,左下角黄色),通常用来概括同一个任务, 自己的名字便于任务之间的切换。
Window, 单个可见窗口。Windows有自己的编号,也可以认为和ITerm2中的 Tab类似(1,2,3,4,窗口)
Pane, 窗格或者面板(windows上的多个pane),被划分成小块的窗口,类似于Vim中 C-w +v 后的效果。
会话 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 tmux tmux new-session -s test tmux new -s test tmux new-session -s test -d <tmux-prefix> + d tmux ls tmux list-sessions <tmux-prefix> + d tmux attach -t test tmux a -t test tmux kill-session -t test tmux kill-server has-session
窗口 在一个tmux会话中运行多个命令或者同时执行多个程序的情况非常普遍,一般利用“window”来管理。window类似“现代图形终端模拟器”或者web浏览器的标签页。 一个tmux窗口创建时,tmux会初始化,在detaching和attaching会话过程中,所有的“window”都会一直存在.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 tmux -t windows -n windows-name1 <tmux-prefix> + c <tmux-prefix> + , 之后在状态栏写入名称即可 rename-window new_name set-option -g allow-rename off setw -g allow-rename off setw -g automatic-rename off <tmux-prefix> + (0, 1, 2, 3, ...) <tmux-prefix> + w(windows list) <tmux-prefix> + f(find,和命令session结合在一起,非常棒,只是不支持模糊匹配) select-window -t session-name:window-no
面板 在一个windows分隔多个面板,监狱里面每一层都理所当然有很多房间嘛,除非碰到大BOSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 <tmux-prefix> + h/j/k/w bind h select-pane -L bind j/k/l select-pane -D/-U/-R tiled 大小相等 even-horizontal 水平排列,从左到右 even-vertical 垂直排列 main-horizontal 顶部大面板,其余小面板水平的放置在下方 main-vertical 左侧大面板,其余小面板垂直的放置在右方 <tmux-prefix> + space select-layout -t [session-name] main-horizontal <tmux-prefix> + % <tmux-prefix> + | <tmux-prefix> + " <tmux-prefix> + - bind | split-window -h bind - split-window -v split-window -v -t [session-name] split-window -v -p 10 -t [session-name] set -g base-index 1 setw -g pane-base-index 1 <tmux-prefix> + q <tmux-prefix> + X exit <tmux-prefix> H/J/K/L bind H resize-pane -L 5 bind J resize-pane -J 5 bind K resize-pane -K 5 bind L resize-pane -L 5 bind -r H resize-pane -L 5
鼠标 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ctrl - a + : set-option -g mouse on tmux setw -g mode-mouse on set -g mouse-select-pane on set -g mouse-resize-pane on set -g mouse-select-window on set -g mouse on/off
分屏工具的分类:
自带分屏功能的终端模拟器:例如terminator 终端分屏软件:例如tmux,screen
优势:
终端模拟器的可配置性不好,但是本身的性能又不好(如果你能做到chrome,那我无话可说),不容易使用; tmux是screen的BSD升级版,包含了了screen的大部分功能:
多窗口工作模式;
单一窗口中多个pane模式;
快速在各个区域进行复制黏贴操作;
配置各种快捷键,加快编程效率;
利用脚本来控制Tmux,更加高效化;
结对编程;
命令前缀:告诉tmux执行命令,而并非让tmux中的应用程序去执行命令,这就是的作用。
1 2 3 4 5 6 7 8 9 10 11 unbind C-b set -g prefix C-aset -g prefix2 C-abind-key -n C-r source-file ~/.tmux.conf bind C-a send-prefix
重载配置: 前面的所有tmux快捷键都是“预先定义好的一些组合键”。存在两种方式来执行tmux命令:
从终端本身执行tmux命令;
在tmux状态栏的“命令区域”执行命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <tmux-prefix> + : shell上执行:tmux cmd 命令模式下:cmd new-window -n console new-window -n process "top" source-file ~/.tmux.conf bind r source-file ~/.tmux.conf \; display "Reloaded file."
命令语法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 bind key1unbind key1 unbind-key key1 source-file file1 set optionssetw options set -adisplay "str1" command 1 \; command 2 \; command 3send-keys -t [session]:[window].[pane] cmd tmux是完全基于“纯键盘操作”,但是如果想用鼠标进行滚屏、选择窗口、选择面板操作。但是,还是不建议使用鼠标操作 mode-mouse on mouse-select-pane on mouse-resize-pane on mouse-select-window on mode-keys vi new-session -s [session-name] new-window attach -t [session-name] tmux split-window -h -t [session-name]
Install Ubuntu 1 2 3 4 5 6 7 8 9 10 sudo aptitude update sudo aptitude install libevent-dev sudo aptitude install -y libncurses5-dev sudo aptitude install -y python-software-properties software-properties-common sudo add-apt-repository -y ppa:pi-rho/dev sudo aptitude install -y tmux=2.0-1~ppa1~t export PATH=$PATH :/usr/local/tmux/bin/
source code 1 2 3 4 5 cd /tmp && wget https://github.com/tmux/tmux/releases/download/2.0/tmux-2.0.tar.gztar zxf tmux-2.0.tar.gz cd tmux-2.0sudo mkdir /usr/local/tmux && ./configure --prefix=/usr/local/tmux make && sudo make install
mac 1 2 brew install tmux tmux -vvv
Copy 1 2 3 4 5 6 7 8 9 10 11 brew install xclip brew install reattach-to-user-namespace Select text and Yank it ---> copy into register "* ---> C-v # tmux内部剪贴设置(配置见.tmux.conf) Prefix + [或者Prefix + Enter ---> 选择文本 ---> Y复制 ---> Prefix + ] # Copy plugin 使用tmux-yank插件替代上面的copy设置。 不过相应的快捷键无法用在vim中。
Logging 在任何一个目录下面,执行tmux -vvv,则会生成client/server日志,之后可以根据日志中的调试信息查看tmux启动过程中的各种问题。
Define 本地配置tmux.conf.local在grocery-shop版本控制中,安装完tmux之后,替换该文件即可。
ubuntu 1 2 3 4 5 6 7 8 9 10 11 12 13 . ~/.local/lib/python2.7/site-packages/powerline/bindings/zsh/powerline.zsh TERM="xterm-256color" alias tmux="tmux -2" source /usr/local/lib/python2.7/dist-packages/powerline/bindings/tmux/powerline.confset-option -g default-terminal "xterm"
Plugins tpm 使用tpm来实现自动化安装插件配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm 下面的代码需要放在.tmux.conf中,在.tmux.conf.local中不生效 set -g @plugin 'tmux-plugins/tpm' set -g @plugin 'tmux-plugins/tmux-sensible' run '~/.tmux/plugins/tpm/tpm' <tmux-prefix> + I <tmux-prefix> + U rm -rf /path-to/plugins/plugins-directory
介绍 手动安装:
1 2 3 4 git clone https://github.com/tmux-plugins/tmux-yank ~/.tmux/plugins/ run-shell ~/clone/path/yank.tmux
tmux-yank: 通过y见来复制信息到cliboard中,其实可以通过配置完成操作。 tmux-safekill: 安全的退出tmuxinator创建的session, + C退出session, + Q退出windows
状态栏配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 左窗口:显示tmux会话 右窗口:显示时间以及标志信息 窗口列表:windows列表 状态栏中可能用到的变量: set -g status-left "#S" set -g status-left "#[fg=green]#S" set -g status-interval 60set -g status-justify centresetw -g monitor-activity on set -g visual-activity on
定制化tmux Introduction 使用一个指定的脚本,一键开启所有的窗口,自动运行一大堆的工具和程序集,开启应用开发。
使用tmux的client-server mode创建一个定制的脚本来自动的构建开发环境、分隔窗口并运行程序。
创建一个简单的全新脚本 Step 1 – create script file: mkdir ~/devproject
Step 2 – create development
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 tmux new -session -s development1 -n editor -d tmux send-keys development1 'cd ~/devproject' C-m tmux send-keys development1 'vim' C-m tmux split -window -v -t developmen1 tmux select-layout -t development1 main-horizontal tmux send-keys -t development1:1.2 'cd ~/devproject' C-m tmux new -window -n console -t development tmux send-keys -t development1:2 'cd ~/devproject' C-m tmux select-window -t development1:1 tmux attach -t development1
Step 3 – Run script file
1 2 chmod +x development./development
.tmux.conf 1 2 3 4 5 tmux -f app.conf attach 默认配置文件,可以在自定义
Manager Tmuxinator 1 2 3 4 5 6 7 8 9 10 curl -sSL https://get.rvm.io | bash source /Users/bamboo/.rvm/scripts/rvmrvm install ruby gem install tmuxinator
tmuxinator配置文件中,每一个session配置被当成一个project,根据需要配置好路径,之后window和pane就可以工作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 export EDITOR=vimtmuxinator open development tmuxinator development tmuxinator open [name] tmuxinator [name] tmuxinator list tmuxinator copy [src] [dst] tmuxinator delete [name] tmuxinator implode tmuxinator doctor
PS:将.tmuxinator/中的配置放到grocery-shop版本控制下面。
文本与缓冲区 进入vim模式 按键 + [进入复制模式,此时可以使用光标键移动屏幕,加入vi模式,可以按照vim模式进行操作。
setw -g mode-keys vim
命令 1 2 3 4 5 show-buffer capture-pane list-buffers choose-buffer save-buffer [filename]
默认缓冲区 tmux的复制模式可以让我们将文本拷贝到“粘贴缓冲区”,之后可以将该缓冲区中的内容在tmux内部粘贴或者转存到系统剪贴板中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Press SPACE at content begin; Move cursor by vim mode; Complete copy after press ENTER; tmux show-buffer tmux save-buffer buffer.txt <tmux-prefix> + ] tmux list-buffers tmux choose-buffer unbind [ bind Escape copy-mode unbind p bind p paste-bufferbind -t vi-copy 'v' begin-selectionbind -t vi-copy 'y' copy-selection
系统剪贴板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 sudo aptitude install xclip bind C-c run "tmux save-buffer - | xclip -i -sel clipboard" bind C-v run "tmux set-buffer \"$(xclip -o -sel clipboard) \"; tmux paste-buffer" brew install reattach-to-user-namespace set -g default-command "reattach-to-user-namespace -l /bin/bash" tmux show-buffer | pbcopy tmux set-buffer pbpaste; tmux paste-buffer bind C-c run "tmux save-buffer - | reattach-to-user-namespace pbcopy" bind C-v run "tmux set-buffer $(reattach-to-user-namespace pbpaste) ; tmux paste-buffer"
结对编程 Intro 多个开发者在不同的地方使用“屏幕共享”进行共同开发操作。
Method 方式
创建一个共享用户账户,在该账户下配置“tmux和开发环境”,然后将账户作为一个共享的工作空间;
使用tmux的”socket连接”,这样可以让第二个用户连接到你的tmux session而无需共享你的账户;
改进
上述的两种方式有一个固有的安全缺陷:暴露你的屏幕信息和账户给他人。
使用中间服务器来进行“结对编程”,使用便宜的VPS或者VirtualBox创建的虚拟机,快速的创建一个用于结对编程的开发环境。
共享账户
创建共享账户
设置登录SSH公钥
开发人员1创建session
开发人员2 attach已有的session
共享账户和组会话 独立的窗口交互,使用组会话,确保存在私有的session(不同的人使用不同的编辑器)
A用户创建组会话:tmux new-session -s groupedsession
B用户创建一个新的会话并加入组会话中:tmux new-session -t groupedsession -s mysession
B用户可以使用Kill-session杀死的他自己的所有会话。
独立账户和socket结对编程 使用tmux提供的socket支持,创建多个用户都可以连接的tmux session。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 sudo adduser user1 sudo adduser user2 sudo groupadd tmux sudo mkdir /var/tmux sudo chgrp tmux /var/tmux sudo chmod g+ws /var/tmux sudo usermod -aG tmux user1 sudo usermod -aG tmux user2 tmux -S /var/tmux/pairing tmux -S /var/tmux/pairing attach
Workflow 面板和窗口 在将面板分隔为不同的“工作空间”时,也有将面板“弹出”为一个独立的window需求,当然也有将window“缩小”为面板的需求。
pane到window: 将面板弹出“pop out”,之后会根据当前pane创建一个新的window
window到pane: 为了合并工作空间,将window变为pane,都整合到一个window中,需要使用命令”join-pane”命令,此时可能会有不同会话之间的合并操作。
1 2 3 4 5 6 7 8 9 10 11 12 tmux new-session -s panes -n first -d tmux new-session -t panes -n second tmux attach -t panes :join-pane -s 1 :join-pane -s 1.0
pane的最大化和恢复, 临时的最大化某一个pane,之后恢复该pane,此时需要使用命令”break-pane, join-pane”,具体流程如下:
1 2 3 4 5 6 7 8 9 unbind Up bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmpunbind Down bind Down last-window \; swap-pane -s tmp.1 \; kill-window -t tmp
pane中执行命令: 在新的pane中自动执行命令。
1 2 3 4 tmux new-session -s servers -d "ssh deploy@bums" tmux split-window -v "ssh dba@smithers" tmux attach -t servers
管理会话 管理session,如会话的移动、会话的连接、窗口的移动
会话间移动: 单机上的所有session都是通过本机的tmux服务器来管理,所以可以在一台机器上实现会话之间的移动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 tmux new -s editor -d vim tmux new -s process -d top tmux attach -t editor <tmux-prefix> + ( <tmux-prefix> + ) <tmux-prefix> + s bind -r ( switch-client -pbind -r ) switch-client -n
连接会话: 判断一个会话是否存在,如果不存在,创建,存在,连接
1 2 3 4 5 6 if ! tmux has-session -t remote; then exec tmux new-session -s development -d fi exec tmux attach -t development
会话之间移动窗口: 将一个window移动到另外一个会话中,一般用于合并“工作空间”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 tmux new -s editor -d vim tmux new -s process -d top tmux attach -t process <tmux-prefix> + . :editor tmux move-window -s process:1 -t editor
操作系统 设置默认shell, 在.tmux.conf中设置默认的shell:
1 2 set -g default-command /bin/zshset -g default-shell /bin/zsh
默认启动tmux: 设置终端启动的时候默认开启tmux,通过使用screen/default-terminal来判断当前终端是否处于tmux中,脚本如下:
1 2 3 4 5 if [[ "${TERM} " != "screen-256color" ]];then tmux attach-session -t "${USER} " || tmux new-session -s "${USER} " exit fi
程序输出到日志: 将一个终端会话的输出记录到一个指定的日志中,前面有capture-pane和save-buffer来进行信息的保存, 另外tmux通过“pipe-pane”来把pane的活动都记录到一个文本中,类似shell中的“script”命令,命令如下:
1 2 bind P pipe-pane -o "cat >>~/#W.log" \; display "Toggled logging to ~/#W.log"
显示电池电量: 在终端显示电池电量(其实没必要)