Back to Unix Utilities

See Also screenvim

tmux

tmux 是一个优秀的终端复用器类自由软件,功能类似 GNU Screen,但使用 BSD 许可发布。用户可以通过 tmux 在一个终端内管理多个分离的会话,窗口及面板,对于同时使用多个命令行,或多个任务时非常方便。

1. 概念模型

tmux 采用 client/server 模型,主要由以下模块组成:

模块

简介

server

服务。tmux 运行的基础服务,以下模块均依赖此服务。

session

会话。一个服务可以包含多个会话。

window

窗口。一个会话可以包含多个窗口。

pane

面板。一个窗口可以包含多个面板。

执行 tmux 命令时就开启了一个服务并创建了一个会话,窗口和面板。

2. tmux快捷键及常用操作

tmux快捷键

2.1. Window相关

Cmd Info Hint
send-prefix + c 建立新窗口
send-prefix + d 临时断开会话
send-prefix + l 與先前選擇的窗口間切換
send-prefix + n 移動到下個窗口
send-prefix + p 移動到上個窗口
send-prefix + & 刪除目前的窗口
send-prefix + , 重新命名目前的窗口
send-prefix + q 显示pane的号码 快速输入可以切换到对应号码的pane
send-prefix + o 切換到下个pane
send-prefix + ? 列出所有快捷键的说明
send-prefix + w 列出目前 clinet 的窗口,可以用數字鍵切換
send-prefix + : 进入命令行模式

2.2. Window Split to Panes(窗口切割)

Cmd Info Hint
Ctrl-a + % split-window-将当前面板平分为左右两块
Ctrl-a + " split-window-将当前面板平分为上下两块
Ctrl-a + x 关闭当前面板
Ctrl-a + ! 将当前面板置于新窗口;即新建一个窗口,其中仅包含当前面板
Ctrl-a + 方向键 移动光标以选择面板
Ctrl-a + o 在当前窗口中选择下一面板
Ctrl-a + q 显示面板编号
Ctrl-a + { 置换当前面板到前面
Ctrl-a + } 置换当前面板到后面
Ctrl-a + z 最大化当前面板及还原 tmux >= 1.8(tmux -V)

4_panes.png

2.3. 多屏操作

默认情况下,一个window上只有一个pane被激活,接收键盘交互。但是某些场景下需要在多个pane中执行相同的操作,比如同时修改两台或更多台远程机器的nginx配置。

这时候可以在分屏后按 Prefix+: 进入命令模式,输入 set synchronize-panes ,即可进入批量操作模式,要退出批量操作模式,再次输入相同命令即可。

3. 调教

3.1. 用Ctl+a进行激活

vim ~/.tmux.conf

set -g prefix C-a
unbind C-b
bind C-a send-prefix

By default, each command begins with a control-a (abbreviated C-a from now on), and is followed by one other keystroke.

3.2. 开启鼠标模式

vim ~/.tmux.conf

   1 setw -g mouse-resize-pane on
   2 setw -g mouse-select-pane on
   3 setw -g mouse-select-window on
   4 setw -g mode-mouse on
  1. 开启用鼠标拖动调节pane的大小(拖动位置是pane之间的分隔线)
  2. 开启用鼠标点击pane来激活该pane
  3. 开启用鼠标点击来切换活动window(点击位置是状态栏的窗口名称)
  4. 开启window/pane里面的鼠标支持(也即可以用鼠标滚轮回滚显示窗口内容,此时还可以用鼠标选取文本)

3.3. tmuxifier

https://github.com/jimeh/tmuxifier

安装与配置

➜  ~ git clone https://github.com/jimeh/tmuxifier.git ~/.tmuxifier
Cloning into '/Users/liyan/.tmuxifier'...
remote: Counting objects: 1136, done.
remote: Total 1136 (delta 0), reused 0 (delta 0), pack-reused 1136
Receiving objects: 100% (1136/1136), 230.66 KiB | 26.00 KiB/s, done.
Resolving deltas: 100% (630/630), done.
Checking connectivity... done.
➜  ~ vi ~/.zshrc
# add for tmuxifier
export PATH="$HOME/.tmuxifier/bin:$PATH"
➜  ~ tmuxifier help
usage: tmuxifier <command> [<args>]

Some useful tmuxifier commands are:
   <command>      <alias>
   load-session   s        Load the specified session layout.
   load-window    w        Load the specified window layout into current session.
   list           l        List all session and window layouts.
   list-sessions  ls       List session layouts.
   list-windows   lw       List window layouts.
   new-session    ns       Create new session layout and open it with $EDITOR.
   new-window     nw       Create new window layout and open it with $EDITOR.
   edit-session   es       Edit specified session layout with $EDITOR.
   edit-window    ew       Edit specified window layout with $EDITOR.
   commands                List all tmuxifier commands (including internal).
   version                 Print Tmuxifier version.
   help                    Show this message.

See 'tmuxifier help <command>' for information on a specific command.

3.3.1. session(define windows)

➜  ~ tmuxifier ns wiki
➜  ~ st /Users/liyan/.tmuxifier/layouts/wiki.session.sh
# Set a custom session root path. Default is `$HOME`.
# Must be called before `initialize_session`.
session_root "/srv/www/moin/wiki"

# Create session with specified name if it does not already exist. If no
# argument is given, session name will be based on layout file name.
if initialize_session "wiki"; then

  # Create a new window inline within session layout definition.
  new_window "main"
  run_cmd 'cd /srv/www/moin/wiki/datas/main/pages'
  new_window "book"
  run_cmd 'cd /srv/www/moin/wiki/datas/book/pages'

  # Load a defined window layout.
  #load_window "example"

  # Select the default active window on session creation.
  select_window 1

fi

# Finalize session creation and switch/attach to it.
finalize_and_go_to_session

➜  ~ tmuxifier s wiki

3.3.2. my wrk window(define 4 panes)

#4panes for wrk test

➜  wrk tmuxifier nw wrk
➜  wrk st ~/.tmuxifier/layouts/wrk.window.sh
# Set window root path. Default is `$session_root`.
window_root "/opt/app/test/wrk"

new_window "wrk"

# Split window into panes.
split_v 50
split_h 50
select_pane 0
split_h 50

# Run commands.
run_cmd "sshpass -pNiubilityPassword()_() ssh li3huo@10.22.33.215 -p2188 #0.115" 0
run_cmd "sshpass -pNiubilityPassword()_() ssh li3huo@10.22.33.99 -p2188 #0.116" 1
run_cmd "sshpass -pNiubilityPassword()_() ssh li3huo@10.22.33.234 -p2188 #0.117" 2
run_cmd "sshpass -pNiubilityPassword()_() ssh li3huo@10.22.33.47 -p2188 #0.119" 3

# Paste text
#send_keys "top"    # paste into active pane
#send_keys "date" 1 # paste into pane 1
send_keys "/data/project/fusdk-platform/run.sh status" 0
send_keys "/data/project/fusdk-platform/run.sh status" 1
send_keys "/data/project/fusdk-platform/run.sh status" 2
send_keys "/data/project/fusdk-platform/run.sh status" 3

# Set active pane.
#select_pane 0

tmux command-prompt -I 'setw synchronize-panes'

➜  wrk tmux
➜  wrk tmuxifier w wrk

3.4. Samples

https://github.com/aaronhalford/tmux-scripts: A set of native tmux scripts to autoarrange and start terminal commands in tmux windows and panes.

create a session named join with a psyshell in it

➜  ~  tmux #
[detached (from session 0)]
➜  ~  tmux ls
0: 1 windows (created Thu Dec 31 17:54:14 2015) [133x24]
➜  ~  tmux a #Ctrl-b + d
[detached (from session 0)]

start new with session name

tmux new -s byname
tmux a -t byname
tmux kill-session -t byname

Kill all the tmux sessions

tmux ls | grep : | cut -d. -f1 | awk '{print substr($1, 0, length($1)-1)}' | xargs kill

4. Reference


CategoryTool

MainWiki: tmux (last edited 2016-06-13 16:44:14 by twotwo)