基本概念

另见终端概述

介绍

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
# 可以一台计算机上创建多个会话,并通过为会话指定唯一名称来管理它们
# 1 创建会话
tmux # 创建一个默认会话,默认会话名从0开始
tmux new-session -s test # 创建一个名字叫test的会话
tmux new -s test

# 2 分离会话
# tmux的一个“最大优势”,在启动tmux环境并执行各个程序和进程时,可以通过从会话“detaching”让tmux在后台进程,此时
# “会话终端”的关闭不会影响后台运行的tmux进程。 任何使用可以通过“attaching”来连接在后台运行的会话。(Cool)
tmux new-session -s test -d # 在后台创建test会话
<tmux-prefix> + d # 将当前会话放入后台运行

# 3 列出当前的所有会话
tmux ls
tmux list-sessions

# 4 连接会话
# 首先从当前会话分离
<tmux-prefix> + d
# attach后台会话
tmux attach -t test
tmux a -t test

# 5 杀死会话
tmux kill-session -t test # 关闭test会话
tmux kill-server # 关闭tmux服务,所有的会话将被关闭

# 6 判断会话存在
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 window
tmux -t windows -n windows-name1
<tmux-prefix> + c

# 重命名
# (非常有用,经常看错windows名称)
# 如果没有设定windows名称,则会根据当前运行的程序自定义名称
<tmux-prefix> + , 之后在状态栏写入名称即可
rename-window new_name

# 必须设定该选项,保证-n操作顺利执行
set-option -g allow-rename off
setw -g allow-rename off
setw -g automatic-rename off

# 切换window
<tmux-prefix> + (0, 1, 2, 3, ...)
<tmux-prefix> + w(windows list)

# 搜索window
<tmux-prefix> + f(find,和命令session结合在一起,非常棒,只是不支持模糊匹配)

# 选择window或者定位窗口
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
# Chapter 1 -- 基本操作
# 面板间的移动,使用下面的原理
<tmux-prefix> + h/j/k/w
# 面板间的选择
bind h select-pane -L
bind j/k/l select-pane -D/-U/-R

# Chapter 2 -- 面板布局
# 所有默认布局
tiled 大小相等
even-horizontal 水平排列,从左到右
even-vertical 垂直排列
main-horizontal 顶部大面板,其余小面板水平的放置在下方
main-vertical 左侧大面板,其余小面板垂直的放置在右方

# 自动变换面板的布局(非常棒的功能,可以从上下两屏变为左右或者其他格式),默认布局见官网说明
<tmux-prefix> + space
# 默认面板布局
select-layout -t [session-name] main-horizontal

# 垂直切割和水平切割,对于tmux,垂直分隔表示在当前面板下创建一个新的pane,此时两个面板会上下重叠在一起,从而达到“水平分隔”的效果
# 即实际上-h(horizontal-水平)效果是相反的。
<tmux-prefix> + % <tmux-prefix> + |
<tmux-prefix> + " <tmux-prefix> + -
bind | split-window -h
bind - split-window -v
# 在指定session中水平分隔面板,-t表示目标window/pane
split-window -v -t [session-name]
# 分隔时指定窗口所占的百分比
split-window -v -p 10 -t [session-name]


# Chapter 3 -- 设置面板初始序号
# 考虑到0在键盘的位置,所以将window和pane的初始序号从0->1
set -g base-index 1
setw -g pane-base-index 1
# 短暂的显示所有面板编号
<tmux-prefix> + q


# Chapter 4 -- 关闭面板
<tmux-prefix> + X
exit

# Chapter 5 -- 选择和大小
# 这是非常常用的操作
<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
# 重复按键,不需要每次都按住<tmux-prefix>,时间间隔为500ms
bind -r H resize-pane -L 5

鼠标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Chapter 1 -- open mouse mode
# 命令行模式:鼠标滚动(最新版)
ctrl - a + :
set-option -g mouse on
# 配置模式
tmux setw -g mode-mouse on

# Chapter 2 -- set mouse mode on of pane
# 可以鼠标调整pane的大小
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on

# 禁用和开启
set -g mouse on/off

configure

分屏工具的分类:

自带分屏功能的终端模拟器:例如terminator
终端分屏软件:例如tmux,screen

优势:

终端模拟器的可配置性不好,但是本身的性能又不好(如果你能做到chrome,那我无话可说),不容易使用;
tmux是screen的BSD升级版,包含了了screen的大部分功能:

  • 多窗口工作模式;
  • 单一窗口中多个pane模式;
  • 快速在各个区域进行复制黏贴操作;
  • 配置各种快捷键,加快编程效率;
  • 利用脚本来控制Tmux,更加高效化;
  • 结对编程;

命令前缀:告诉tmux执行命令,而并非让tmux中的应用程序去执行命令,这就是的作用。

1
2
3
4
5
6
7
8
9
10
11
# 更改命令前缀和大写键,在.tmux.conf中
unbind C-b
set -g prefix C-a

set -g prefix2 C-a

# 不需要前缀的快捷键,使用-n参数来设置,但是该设置会让tmux会话的任何程序或者命令都禁用组合键
bind-key -n C-r source-file ~/.tmux.conf

# 发送前缀键到其他程序,将组合键发送到tmux内的应用中,此时只需要按C-a + C-a既可以将C-a发送应用中。
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命令和在命令模式执行tmux命令
shell上执行:tmux cmd
命令模式下:cmd

# 在命令模式中创建一个窗口
new-window -n console
new-window -n process "top" # 创建初始化进程后,如果关闭top,则整个window也会关闭

# 重新加载tmux配置
source-file ~/.tmux.conf

# 绑定+r为重新加载配置,并使用display输出
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
# Chapter 1 -- bind and unbind  
# 绑定
bind key1

# 取消绑定
unbind key1
unbind-key key1

# Chapter 2 -- setting
# 重新加载配置
source-file file1

# 基本配置(set-option)
set options
# 窗口交互配置(set-window-option)
setw options
# 添加新的配置而并非替换选项的值
set -a

# 回显或者输出
display "str1"

# 多条命令同时执行
command 1 \; command 2 \; command 3

# 向指定的window, pane发送命令,其中目标的格式如下
send-keys -t [session]:[window].[pane] cmd


# Chapter 3 -- mouse
tmux是完全基于“纯键盘操作”,但是如果想用鼠标进行滚屏、选择窗口、选择面板操作。但是,还是不建议使用鼠标操作
# 鼠标模式
mode-mouse on
# 鼠标调整面板大小
mouse-select-pane on
mouse-resize-pane on
mouse-select-window on

# 按键模式
mode-keys vi


# Chapter 4 -- attach
# 新会话
new-session -s [session-name]
# 新窗口
new-window
# attaching,target
attach -t [session-name]
# 对已存在的会话发送命令
tmux split-window -h -t [session-name]

Install

Ubuntu

1
2
3
4
5
6
7
8
9
10
# requirement
sudo aptitude update
sudo aptitude install libevent-dev
sudo aptitude install -y libncurses5-dev
sudo aptitude install -y python-software-properties software-properties-common

# install tmux from apitude
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.gz
tar zxf tmux-2.0.tar.gz
cd tmux-2.0
sudo 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
# tmux到系统剪贴设置(macvim)
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启动过程中的各种问题。

Configure

Define

本地配置tmux.conf.local在grocery-shop版本控制中,安装完tmux之后,替换该文件即可。

ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
# 保证powerline.zsh的成功执行
. ~/.local/lib/python2.7/site-packages/powerline/bindings/zsh/powerline.zsh
TERM="xterm-256color"
alias tmux="tmux -2"
# 保证powerline.conf在tmux中的配置
# 保证默认终端为xterm

# 保证TERM-color的设定
# ubuntu14.04中的颜色修正

source /usr/local/lib/python2.7/dist-packages/powerline/bindings/tmux/powerline.conf
set-option -g default-terminal "xterm"
# 保证terminal的字体选择

Plugins

tpm

使用tpm来实现自动化安装插件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Step 1 -- 安装
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# Step 2 -- 添加配置
下面的代码需要放在.tmux.conf中,在.tmux.conf.local中不生效
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
# 放在最后一行
run '~/.tmux/plugins/tpm/tpm'

# Step 3 -- 安装插件以及更新插件
<tmux-prefix> + I # install plugin
<tmux-prefix> + U # update and reload(tmux-sensible插件作用)

# Step 4 -- 删除
rm -rf /path-to/plugins/plugins-directory

介绍

手动安装:

1
2
3
4
# tmux-yank
git clone https://github.com/tmux-plugins/tmux-yank ~/.tmux/plugins/
# add at .tmux.conf.local
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
# Chapter 1 -- 结构
左窗口:显示tmux会话
右窗口:显示时间以及标志信息
窗口列表:windows列表


# Chapter 2 -- 变量
状态栏中可能用到的变量:
#H 本地主机的主机名
#h 没有domain的本地主机名
#F 当前窗口的标签
#I 当前窗口的索引
#P 当前面板的索引
#S 当前会话的名称
#T 当前窗口的标题
#W 当前窗口的名称
## #符号
#(shell-command) shell命令的第一行输出
#[attributes] 欲改变的属性和颜色


# Chapter 3 -- 配置
# 左侧显示会话名称
set -g status-left "#S"
# 前景色更加明亮
set -g status-left "#[fg=green]#S"
# 状态栏实时更新,默认15秒
set -g status-interval 60
# 窗口列表居中显示
set -g status-justify centre
# 窗口活动通知,加粗变色处理
setw -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
" 发送shell命令,同时在tmux中换行(Control-M)
tmux send-keys development1 'cd ~/devproject' C-m
" 打开vim编辑器
tmux send-keys development1 'vim' C-m
" 分隔主编辑窗口并选择分隔比例
tmux split-window -v -t developmen1
tmux select-layout -t development1 main-horizontal

" 在分隔后的第二个Pane中发送命令改变路径信息
tmux send-keys -t development1:1.2 'cd ~/devproject' C-m
" 打开第二个window并命名为console
tmux new-window -n console -t development
tmux send-keys -t development1:2 'cd ~/devproject' C-m

" 创建第二个window后,重新定位到第一个window
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总是调用new-session,所以需要attach保证仅仅存在一个session
tmux -f app.conf attach

# .tmux.conf
默认配置文件,可以在自定义

Manager

Tmuxinator

1
2
3
4
5
6
7
8
9
10
# Step 1 -- install rvm
curl -sSL https://get.rvm.io | bash
source /Users/bamboo/.rvm/scripts/rvm

# Step 2 -- install ruby(mac自带需要重新安装)
rvm install ruby

# Step 3 -- install tmuxinator by gem
# gem为ruby包管理
gem install tmuxinator

Configure

tmuxinator配置文件中,每一个session配置被当成一个project,根据需要配置好路径,之后window和pane就可以工作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Chapter 1 -- envionment
export EDITOR=vim

# Chapter 2 -- create new project
# 此时会在~/.tmuxinator/下生成development.yml文件,该文件同6.1章节无任何关系,使用YAML语法
tmuxinator open development

# Chapter 3 -- Open exists project
tmuxinator development

# Chapter 4 -- command
tmuxinator open [name] # create
tmuxinator [name] # open
tmuxinator list # list all project
tmuxinator copy [src] [dst] # copy a project
tmuxinator delete [name] # delete name
tmuxinator implode # delete all
tmuxinator doctor # 使用tmuxinator和系统配置查找问题

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
# Chapter 1 -- copy
# 没必要使用鼠标进行复制操作。
Press SPACE at content begin;
Move cursor by vim mode;
Complete copy after press ENTER;

# Chapter 2 -- show buffer
tmux show-buffer

# Chapter 3 -- save buffer
# 建议设置为一个快键键
tmux save-buffer buffer.txt

# Chapter 4 -- paste buffer
# 默认总是粘贴“0”号缓冲区中的内容
<tmux-prefix> + ]

# Chapter 5 -- buffer stack
# tmux自身的缓冲区是一个“栈空间”,多次复制的内容都按照顺序在整个栈空间中存储
# 列出所有缓存
tmux list-buffers
# 选择缓冲区
tmux choose-buffer

# Chapter 6 -- keys map
# 自定义快捷键设置
unbind [
bind Escape copy-mode # 进入复制模式
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -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
# Chapter 1 -- Linux
# 安装xclip
sudo aptitude install xclip
# 通过xclip来使用tmux中的save-buffer和set-buffer命令
bind C-c run "tmux save-buffer - | xclip -i -sel clipboard"
# 系统剪贴板到tmux中
bind C-v run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"

# Chapter 2 -- Mac
# mac上的剪贴板工具(pbcopy、pbpaste)用来实现上述的操作,但是因为权限问题,需要结合第三方应用reattach-to-user-namespace来操作
brew install reattach-to-user-namespace
# 设置通过包装脚本加载bash
set -g default-command "reattach-to-user-namespace -l /bin/bash"
# 使用pbcopy/pbpaste命令
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创建的虚拟机,快速的创建一个用于结对编程的开发环境。

共享账户

  1. 创建共享账户
  2. 设置登录SSH公钥
  3. 开发人员1创建session
  4. 开发人员2 attach已有的session

共享账户和组会话

独立的窗口交互,使用组会话,确保存在私有的session(不同的人使用不同的编辑器)

  1. A用户创建组会话:tmux new-session -s groupedsession
  2. B用户创建一个新的会话并加入组会话中:tmux new-session -t groupedsession -s mysession
  3. 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
# Step 1 -- Create user
sudo adduser user1
sudo adduser user2

# Step 2 -- Create group and save session
sudo groupadd tmux
sudo mkdir /var/tmux
sudo chgrp tmux /var/tmux
sudo chmod g+ws /var/tmux

# Step 3 -- Add user to group
sudo usermod -aG tmux user1
sudo usermod -aG tmux user2

# Step 4 -- Create share session
# user1登录:不创建命名会话,不适用默认的socket(UNIX socket),使用-S指定位置
tmux -S /var/tmux/pairing
# user2登录:连接会话,此时用户user2自己的tmux配置是不生效的
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
# Step 1 -- create test session
# 创建脱离session
tmux new-session -s panes -n first -d
tmux new-session -t panes -n second
tmux attach -t panes

# Step 2 -- move first window into second
:join-pane -s 1

# Step 3 -- handle multiple pane
# 将1号窗口的0号面板添加到当前widow中,目标窗口格式:-t [session_name]:[window].[pane]
:join-pane -s 1.0

pane的最大化和恢复, 临时的最大化某一个pane,之后恢复该pane,此时需要使用命令”break-pane, join-pane”,具体流程如下:

1
2
3
4
5
6
7
8
9
# Step 1 -- unbind up keys
# 这个如果绑定之后要小心了
unbind Up
# 创建tmp临时窗口;使用swap-pane将已经选择的面板和临时窗口的已有面板交换;打开交换后的窗口
bind Up new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp

# Step 2 -- unbind down keys
unbind 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
# Step 1 -- create test session
tmux new -s editor -d vim
tmux new -s process -d top

# Step 2 -- attach
tmux attach -t editor

# Step 3 -- moving
<tmux-prefix> + (
<tmux-prefix> + )
# 显示会话列表
<tmux-prefix> + s

# Step 4 -- binding
bind -r ( switch-client -p
bind -r ) switch-client -n

连接会话: 判断一个会话是否存在,如果不存在,创建,存在,连接

1
2
3
4
5
6
# script
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
# Step 1 -- create test session
tmux new -s editor -d vim
tmux new -s process -d top

# Step 2 -- attach
tmux attach -t process

# Step 3 -- move window
<tmux-prefix> + .
:editor

# Step 4 -- shell handle
# 将process的第一个窗口移动到editor中
tmux move-window -s process:1 -t editor

操作系统

设置默认shell, 在.tmux.conf中设置默认的shell:

1
2
set -g default-command /bin/zsh
set -g default-shell /bin/zsh

默认启动tmux: 设置终端启动的时候默认开启tmux,通过使用screen/default-terminal来判断当前终端是否处于tmux中,脚本如下:

1
2
3
4
5
# 注意,在任意窗口使用exit命令都会关闭所有连接到会话终端窗口
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
# 绑定<tmux-prefix> + p来开启日志记录功能
bind P pipe-pane -o "cat >>~/#W.log" \; display "Toggled logging to ~/#W.log"

显示电池电量: 在终端显示电池电量(其实没必要)