如何使用 Tmuxp 來優雅的管理多個 Tmux 會話

使用 tmuxp 可以很好的幫助我們來管理 tmux 的會話 (session),解決了平時在使用 tmux 工具時候的痛點。

  1. 工具介紹

安裝和使用都非常簡單

事實上,tmuxp也是一個構建於tmux之上的對象關係映射的ORMAPI工具,就是利用tmux工具定義的窗格 (pane)、窗口(windows) 和會話(session),以創建關聯視圖的Server,Session,Window,Pane對象。我們在使用的時候,可以使用YAML,JSON以及dict字配置項來啓動我們配置好的窗口和麪板。使用時候需要注意的是,只支持tmux>=1.8的版本。

# 只有這一種安裝方式
$ 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'

  1. 使用方式

只需要記住 load 這個一個命令其實就夠了

**tmuxp**工具將配置文件保存在~/.tmuxp中或在項目目錄下作爲~/.tmuxp.{yaml,json}獨立出現。當然我們也可以使用其提供的命令,進行會話的相關操作和使用。下來就讓我們一起去看看,如何使用吧!

# 使用方式
# 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/
# 使用方式
# 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
# 使用方式
# config: 配置文件的絕對/相對路徑
tmuxp convert [-h] config
# 可以互相轉換JSON/YAML格式
$ tmuxp convert <filename>
# 使用方式
# config: 在 ~/.teamocil 和當前目錄下查找yaml文件
# --list=False: 列出 ~/.teamocil 和當前工作目錄下的配置文件
$ tmuxp import teamocil [-h] (--list | config)
$ tmuxp import tmuxinator [-h] (--list | config)
  1. 面板窗口配置

主要介紹 tmux 面板的切分、啓動目錄、索引設定等操作

使用 tmuxp 來管理會話 - 兩個窗格

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

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
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
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
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.'
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. 面板窗口設置

工具系統方法的一些配置

session_name: Environment variables test
environment:
  EDITOR: /usr/bin/vim
  HOME: /tmp/hm
windows:
  # 如果之前沒有shell命令將打開一個空白窗格。
  - window_name: Blank pane test
    panes:
      -
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
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!"
session_name: 2-pane-synchronized
windows:
  - window_name: Two synchronized panes
    panes:
      - ssh server1
      - ssh server2
    options_after:
      synchronize-panes: on
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"
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"
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
# 假設您的.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
  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>
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
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>&|| { 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>&|| { 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>&|| { 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
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>&|| { 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>&|| { 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>&|| { pip install -r requirements.pip; }
    - command -v watching_testrunner >/dev/null 2>&|| { pip install watching_testrunner; }
    - watching_testrunner --basepath ./ --pattern="*.rst" 'make html'
Terms
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/rVyFgR24r-SkbuI347_9JA