使用 tmuxp 可以很好的帮助我们来管理 tmux 的会话(session),解决了平时在使用 tmux 工具时候的痛点。
1. 工具介绍
事实上,tmuxp 也是一个构建于 tmux 之上的对象关系映射的 ORM 的 API 工具,就是利用 tmux 工具定义的窗格(pane)、窗口(windows)和会话(session),以创建关联视图的 Server, Session, Window, Pane 对象。我们在使用的时候,可以使用 YAML, JSON 以及 dict 字配置项来启动我们配置好的窗口和面板。使用时候需要注意的是,只支持 tmux>=1.8 的版本。
# 只有这一种安装方式
$ dnf install tmux
$ pip install tmuxp
# 配置zsh补全(.zshrc)
$ eval "$(_TMUXP_COMPLETE=source_zsh tmuxp)"
# 配置bash补全(.bashrc)
$ eval "$(_TMUXP_COMPLETE=source tmuxp)"
# 如果运行命令提示报错则执行如下命令即可
$ tmux kill-server
常用命令
# 输入tmux detach命令,就会将当前会话与窗口分离。之后可以 attach 进来
$ tmux detach
# 上面命令执行后,就会退出当前 Tmux 窗口,但是会话和里面的进程仍然在后台运行。
# tmux ls命令可以查看当前所有的 Tmux 会话。
$ tmux ls
$ tmux list-session
# tmux attach命令用于重新接入某个已存在的会话。
# 使用会话编号
$ tmux attach -t 0
# 使用会话名称
$ tmux attach -t <session-name>
# tmux kill-session命令用于杀死某个会话。
# 使用会话编号
$ tmux kill-session -t 0
# 使用会话名称
$ tmux kill-session -t <session-name>
# tmux switch命令用于切换会话。
# 使用会话编号
$ tmux switch -t 0
# 使用会话名称
$ tmux switch -t <session-name>
# tmux rename-session命令用于重命名会话。
tmux rename-session -t 0 <new-name>
# 面命令将0号会话重命名。
# 光标切换到上方窗格
$ tmux select-pane -U
# 光标切换到下方窗格
$ tmux select-pane -D
# 光标切换到左边窗格
$ tmux select-pane -L
# 光标切换到右边窗格
$ tmux select-pane -R
# 列出当前所有 Tmux 会话的信息
$ tmux info
2. 使用方式
只需要记住 load 这个一个命令其实就够了
tmuxp 工具将配置文件保存在 ~/.tmuxp 中或在项目目录下作为 ~/.tmuxp.{yaml,json} 独立出现。当然我们也可以使用其提供的命令,进行会话的相关操作和使用。下来就让我们一起去看看,如何使用吧!
[1] 加载会话(Load session)
# 使用方式
# session_name: 会话(Session)名称
# -h: tmux服务器的地址
# -L: tmux服务器的socket名,与tmux相同
# -S: tmux服务器的socket路径,与tmux相同
# -2: 强迫tmux的终端支持256色
# -8: 与-2类似,但是只支持88色
# --list=False: 列出可用的配置文件
$ tmuxp load [-h] [-L socket-name] [-S socket-path] [-2 | -8] [--list] [config]
# 通过配置文件路径加载
$ tmuxp load .
$ tmuxp load ./mysession.yaml
$ tmuxp load ~/workspaces/myproject.yaml
# 通过自定义会话名称加载
$ tmuxp load mysession
# 一次加载多个会话
$ tmuxp load mysession ./another/project/
[2] 启用鼠标支持
在Terminal输入vi ~/.tmux.conf创建一个配置文件, 并在文件中输入set -g mouse on并保存.
cat ~/.tmux.conf
set -g mouse on
随后在Terminal中输入tmux进入tmux模式, 进入后按Ctrl+b+:, 此时tmux底部会变颜色. 在此输入source ~/.tmux.conf并回车即可.
[3] 开启鼠标支持后复制粘贴
然后就可以复制了, 步骤是这样的, 按住 shift 选择要复制的内容,在看你终端的复制快捷键是哪个,比如xshell这里是ctrl + insert,
1.选择完要复制的内容;
2.按下ctrl + insert 就复制成功了;
3.然后就可以shift+insert粘贴了;
3. 面板窗口配置
[1] 两个窗格(2 split panes)
session_name: 2-pane-vertical
windows:
- window_name: my test window
panes:
- echo hello
- echo hello
session_name: 2-pane-vertical-long
windows:
- window_name: test
panes:
- shell_command:
- cd ~
- pwd
- top
- shell_command:
- cd /var/www
- pwd
- window_name: second window
shell_command_before: cd /var/www
panes:
- shell_command: pwd
- shell_command:
- pwd
[2] 三个窗格(3 panes)
session_name: 3-panes
windows:
- window_name: dev window
layout: main-vertical
shell_command_before:
- cd ~/
panes:
- shell_command:
- cd /var/log
- ls -al | grep \.log
- echo hello
- echo hellotmu
[3] 四个窗格(4 panes)
session_name: 4-pane-split
windows:
- window_name: dev window
layout: tiled
shell_command_before:
- cd ~/
panes:
- shell_command:
- cd /var/log
- ls -al | grep \.log
- echo hello
- echo hello
- echo hello
[4] 空白窗格(Blank panes)
直接可以使用 ‘null, ‘blank’, ‘pane’ 中任何一个,即可创建空白窗格
session_name: Blank pane test
windows:
# 如果之前没有shell命令将打开一个空白窗格
- window_name: Blank pane test
panes:
-
- pane
- blank
- window_name: More blank panes
panes:
- null
- shell_command:
- shell_command:
-
# 空字符串将被视为回车
- window_name: Empty string (return)
panes:
- ""
- shell_command: ""
- shell_command:
- ""
# 窗格可以有其他选项但仍然是空的
- window_name: Blank with options
panes:
- focus: true
- start_directory: /tmp
[5] 启动目录(Start Directory)
设置起始目录
等价于tmux new-window -c
session_name: start directory
start_directory: /var/
windows:
- window_name: should be /var/
panes:
- shell_command:
- echo "\033c
- it trickles down from session-level"
- echo hello
- window_name: should be /var/log
start_directory: log
panes:
- shell_command:
- echo '\033c
- window start_directory concatenates to session start_directory
- if it is not absolute'
- echo hello
- window_name: should be ~
start_directory: "~"
panes:
- shell_command:
- 'echo \\033c ~ has precedence. note: remember to quote ~ in YAML'
- echo hello
- window_name: should be /bin
start_directory: /bin
panes:
- echo '\033c absolute paths also have precedence.'
- echo hello
- window_name: should be config's dir
start_directory: ./
panes:
- shell_command:
- echo '\033c
- ./ is relative to config file location
- ../ will be parent of config file
- ./test will be \"test\" dir inside dir of config file'
- shell_command:
- echo '\033c
- This way you can load up workspaces from projects and maintain
- relative paths.'
[6] 窗口索引
定义窗口索引,就是我们在使用时候用来切换窗口时使用
session_name: Window index example
windows:
- window_name: zero
panes:
- echo "this window's index will be zero"
- window_name: five
panes:
- echo "this window's index will be five"
window_index: 5
- window_name: one
panes:
- echo "this window's index will be one"
4. 面板窗口设置
[1] 环境变量
在tmuxp中设置会话环境变量
session_name: Environment variables test
environment:
EDITOR: /usr/bin/vim
HOME: /tmp/hm
windows:
# 如果之前没有shell命令将打开一个空白窗格。
- window_name: Blank pane test
panes:
-
[2] 定制主面板高度(Main pane height)
根据实际使用情况,设置主面板的高度
session_name: main-pane-height
start_directory: "~"
windows:
- layout: main-horizontal
options:
main-pane-height: 30
panes:
- shell_command:
- top
start_directory: "~"
- shell_command:
- echo "hey"
- shell_command:
- echo "moo"
window_name: my window name
[3] 终端历史
用于配置是否需要记录终端命令历史
session_name: suppress
suppress_history: false
windows:
- window_name: appended
focus: true
suppress_history: false
panes:
- echo "window in the history!"
- window_name: suppressed
suppress_history: true
panes:
- echo "window not in the history!"
- window_name: default
panes:
- echo "session in the history!"
- window_name: mixed
suppress_history: false
panes:
- shell_command:
- echo "command in the history!"
suppress_history: false
- shell_command:
- echo "command not in the history!"
suppress_history: true
- shell_command:
- echo "window in the history!"
[4] 窗口选项
创建窗格后设置窗口选项
在创建过程中在每个窗格中执行单个命令后,对于“同步窗格”选项很有用
session_name: 2-pane-synchronized
windows:
- window_name: Two synchronized panes
panes:
- ssh server1
- ssh server2
options_after:
synchronize-panes: on
[5] 设置选项
设置tmux工具相关的选项,例如全局(服务器范围)选项、会话选项和窗口选项
包括automatic-rename,default-shell,default-command等相关命令
session_name: test window options
start_directory: "~"
global_options:
default-shell: /bin/sh
default-command: /bin/sh
options:
main-pane-height: ${MAIN_PANE_HEIGHT} # works with env variables
windows:
- layout: main-horizontal
options:
automatic-rename: on
panes:
- shell_command:
- man echo
start_directory: "~"
- shell_command:
- echo "hey"
- shell_command:
- echo "moo"
[6] 自动重命名(Automatic Rename)
即可以自动命令创建的面板名称.
session_name: test window options
start_directory: "~"
windows:
- layout: main-horizontal
options:
automatic-rename: on
panes:
- shell_command:
- man echo
start_directory: "~"
- shell_command:
- echo "hey"
- shell_command:
- echo "moo"
[7] 专注模式
在专注模式中,可以确保在加载时附加和选择窗口和窗格
session_name: focus
windows:
- window_name: attached window
focus: true
panes:
- shell_command:
- echo hello
- echo 'this pane should be selected on load'
focus: true
- shell_command:
- cd /var/log
- echo hello
- window_name: second window
shell_command_before: cd /var/log
panes:
- pane
- shell_command:
- echo 'this pane should be focused, when window switched to first time'
focus: true
- pane
5. 配置示例文件
实例1
session_name: workspace
windows:
- focus: "true"
layout: 66e3,238x57,0,0,14
options:
automatic-rename: "off"
panes:
- pane
start_directory: /Users/laixintao/Program
window_name: vim
- layout: a5de,238x57,0,0{119x57,0,0,15,118x57,120,0[118x28,120,0,19,118x28,120,29,20]}
options:
automatic-rename: "off"
panes:
- focus: "true"
shell_command: zsh
-
-
start_directory: /Users/laixintao
window_name: operation
- layout: 66e5,238x57,0,0,16
options:
automatic-rename: "off"
panes:
- focus: "true"
shell_command: zsh
start_directory: /Users/laixintao
window_name: shell
- layout: 66e6,238x57,0,0,17
options:
automatic-rename: "off"
panes:
- focus: "true"
shell_command: zsh
start_directory: /Users/laixintao
window_name: shell
- layout: 66e7,238x57,0,0,18
options:
automatic-rename: "off"
panes:
- focus: "true"
shell_command: zsh
start_directory: /Users/laixintao
window_name: shell
- layout: main-vertical
options:
automatic-rename: "off"
panes:
- focus: "true"
shell_command: clash > /tmp/clash.log
window_name: <clash>
实例2
session_name: vcspull
start_directory: ./
before_script: pipenv install --dev --skip-lock
shell_command_before:
- "[ -d `pipenv --venv` ] && source `pipenv --venv`/bin/activate && reset"
windows:
- window_name: vcspull
focus: True
layout: main-horizontal
options:
main-pane-height: 35
panes:
- focus: true
- pane
- make watch_test
- window_name: docs
layout: main-horizontal
options:
main-pane-height: 35
start_directory: doc/
panes:
- focus: true
- pane
- make serve
- make watch
实例3 dockerfiles
session_name: docker
start_directory: ./
windows:
- window_name: dockerfiles
layout: 6da5,239x56,0,0[239x34,0,0,65,239x21,0,35{119x21,0,35,66,119x21,120,35,67}]
options:
automatic-rename: "off"
panes:
- shell_command:
- vim
- :e README.rst
- pane
- pane
- window_name: docs
layout: main-horizontal
options:
main-pane-height: 35
shell_command_before:
- command -v virtualenv >/dev/null 2>&1 || { pip install virtualenv; }
- "[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate || virtualenv .env"
- "[ ! -d .env/build ] || rm -rf .env/build"
- cd ./doc
- command -v .env/bin/tmuxp >/dev/null 2>&1 || { pip install -r requirements.pip; }
panes:
- shell_command:
- reset
- vim
- :Ex
focus: true
- pwd
- echo 'docs built to <http://0.0.0.0:8007/_build/html>'; python -m SimpleHTTPServer 8007
- shell_command:
- command -v watching_testrunner >/dev/null 2>&1 || { pip install watching_testrunner; }
- watching_testrunner --basepath ./ --pattern="*.rst" 'make html'
- window_name: postgresql
layout: 6da5,239x56,0,0[239x34,0,0,65,239x21,0,35{119x21,0,35,66,119x21,120,35,67}]
options:
automatic-rename: "off"
start_directory: postgresql
panes:
- shell_command:
- vim
- :e Dockerfile
- pane
- pane
实例4
session_name: sphinxcontrib-github
start_directory: ./
windows:
- options:
main-pane-height: 35
layout: main-horizontal
panes:
- shell_command:
- vim
focus: true
- pane
- pane
window_name: sphinxcontrib-github
- window_name: docs
layout: main-horizontal
options:
main-pane-height: 35
shell_command_before:
- command -v virtualenv >/dev/null 2>&1 || { pip install virtualenv; }
- '[ -d .env -a -f .env/bin/activate ] && source .env/bin/activate || virtualenv .env'
- '[ ! -d .env/build ] || rm -rf .env/build'
- command -v .env/bin/tmuxp >/dev/null 2>&1 || { pip install -e .; }
- cd ./doc
panes:
- shell_command:
- reset
- vim
- :Ex
focus: true
- pane
- echo 'docs built to <http://0.0.0.0:8005/_build/html>'; python -m SimpleHTTPServer 8005
- shell_command:
- command -v sphinx-quickstart >/dev/null 2>&1 || { pip install -r requirements.pip; }
- command -v watching_testrunner >/dev/null 2>&1 || { pip install watching_testrunner; }
- watching_testrunner --basepath ./ --pattern="*.rst" 'make html'
Terms