如何使用 Tmuxp 來優雅的管理多個 Tmux 會話
使用 tmuxp 可以很好的幫助我們來管理 tmux 的會話 (session),解決了平時在使用 tmux 工具時候的痛點。
- 工具介紹
安裝和使用都非常簡單
事實上,tmuxp
也是一個構建於tmux
之上的對象關係映射的ORM
的API
工具,就是利用tmux
工具定義的窗格 (pane)、窗口(windows) 和會話(session),以創建關聯視圖的Server
,Session
,Window
,Pane
對象。我們在使用的時候,可以使用YAML
,JSON
以及dict
字配置項來啓動我們配置好的窗口和麪板。使用時候需要注意的是,只支持tmux>=1.8
的版本。
-
簡潔的語法
-
都支持在單獨的命令中使用簡短標記
-
加載會話
-
都是根據配置文件中加載
tmux
會話 (session
) -
多種格式支持
-
可以使用
YAML
,JSON
以及dict
字配置項
# 只有這一種安裝方式
$ pip install tmuxp
$ pip install --user tmuxp
# 配置zsh補全(.zshrc)
$ eval "$(_TMUXP_COMPLETE=source_zsh tmuxp)"
# 配置bash補全(.bashrc)
$ eval "$(_TMUXP_COMPLETE=source tmuxp)"
# 如果運行命令提示報錯則執行如下命令即可
$ tmux kill-server
# 簡潔的語法配置
session_name: shorthands
windows:
- window_name: long form
panes:
- shell_command:
- echo 'did you know'
- echo 'you can inline'
- shell_command: echo 'single commands'
- echo 'for panes'
- 使用方式
只需要記住 load 這個一個命令其實就夠了
**tmuxp
**工具將配置文件保存在~/.tmuxp
中或在項目目錄下作爲~/.tmuxp.{yaml,json}
獨立出現。當然我們也可以使用其提供的命令,進行會話的相關操作和使用。下來就讓我們一起去看看,如何使用吧!
-
[1] 加載會話 (Load session)
-
Load a tmux workspace from each CONFIG.
# 使用方式
# 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] 凍結會話 (Freeze sessions)
-
Snapshot a session into a config.
# 使用方式
# session_name: 會話(Session)名稱
# -h: tmux服務器的地址
# -L: tmux服務器的socket名,與tmux相同
# -S: tmux服務器的socket路徑,與tmux相同
$ tmuxp freeze [-h] [-L socket-name] [-S socket-path] session_name [session_name ...]
# 爲tmux佈局、窗格路徑和窗口/會話名稱創建快照
$ tmuxp freeze session-name
-
[3] 配置轉換
-
Convert a tmuxp config between JSON and YAML.
# 使用方式
# config: 配置文件的絕對/相對路徑
tmuxp convert [-h] config
# 可以互相轉換JSON/YAML格式
$ tmuxp convert <filename>
-
[4] 配置導入
-
Import a teamocil/tmuxinator config.
# 使用方式
# config: 在 ~/.teamocil 和當前目錄下查找yaml文件
# --list=False: 列出 ~/.teamocil 和當前工作目錄下的配置文件
$ tmuxp import teamocil [-h] (--list | config)
$ tmuxp import tmuxinator [-h] (--list | config)
- 面板窗口配置
主要介紹 tmux 面板的切分、啓動目錄、索引設定等操作
-
[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 <start-directory>
命令
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"
- 面板窗口設置
工具系統方法的一些配置
-
[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
- 測試開發配置
主要介紹在我們實際開發和測試當中應該如何使用該工具
-
[1] 定製高級開發環境
-
作爲開發環境時候的相關配置
session_name: tmuxp
start_directory: ./ # 加載相對於配置位置(項目根目錄)的會話
before_script: pipenv install --dev --skip-lock
shell_command_before:
- "[ -d `pipenv --venv` ] && source `pipenv --venv`/bin/activate && reset"
windows:
- window_name: tmuxp
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
-
[2] 自動引導
-
新版本
1.3.4
: 在腳本 CWD 進入根 (會話) 級啓動目錄之前
# 假設您的.tmuxp.yaml位於項目的根目錄中
# 如果使用pipenv的話,則可以使用如下腳本來確保已安裝軟件包
session_name: my pipenv project
start_directory: ./
before_script: pipenv install --dev --skip-lock # 開發環境
windows:
- window_name: django project
focus: true
panes:
- blank
- pipenv run ./manage.py runserver
# 假設您的.tmuxp.yaml位於項目的根目錄中
# 可以使用shell_command_before將自己置身於虛擬環境中
session_name: my pipenv project
start_directory: ./
before_script: pipenv install --dev --skip-lock # 開發環境
shell_command_before:
- "[ -d `pipenv --venv` ] && source `pipenv --venv`/bin/activate && reset"
windows:
- window_name: django project
focus: true
panes:
- blank
- ./manage.py runserver
- 配置示例文件
這裏就是其他人使用該工具的配置文件,可以借鑑和引用
- laixintao
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>
- vcspull
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
- 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
- sphinxcontrib
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
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/rVyFgR24r-SkbuI347_9JA