tmux進階之tmuxinator

作者:crane-yuan 日期:2017-03-03


前言

tmuxinator是tmux的配置管理工具,解決了tmux服務器關機后session丟失問題。tmuxinator可以根據(jù)配置文件快速創(chuàng)建tmux的session。

Tmuxinator的安裝

Tmuxinator基于Ruby,首先安裝Ruby

Ubuntu用戶可以用apt-get命令安裝:

> apt-get install ruby

ArchLinux用戶可以用pacman命令安裝:

> pacman -S ruby

安裝Tmuxinator

若由于(你懂得的)網(wǎng)絡原因無法安裝,則更新Ruby的gem源后再次嘗試。

> gem source -a https://ruby.taobao.org/
> gem source -r https://rubygems.org/

安裝tmuxinator

> gem install tmuxinator

基礎設置

bash版

將下述文本保存為$HOME/.tmuxinator/.tmuxinator.bash,提供bash的tab鍵提示功能

#!/usr/bin/env bash

_tmuxinator() {
    COMPREPLY=()
    local word
    word="${COMP_WORDS[COMP_CWORD]}"

    if [ "$COMP_CWORD" -eq 1 ]; then
        local commands="$(compgen -W "$(tmuxinator commands)" -- "$word")"
        local projects="$(compgen -W "$(tmuxinator completions start)" -- "$word")"

        COMPREPLY=( $commands $projects )
    elif [ "$COMP_CWORD" -eq 2 ]; then
        local words
        words=("${COMP_WORDS[@]}")
        unset words[0]
        unset words[$COMP_CWORD]
        local completions
        completions=$(tmuxinator completions "${words[@]}")
        COMPREPLY=( $(compgen -W "$completions" -- "$word") )
    fi
}

complete -F _tmuxinator tmuxinator mux

$HOME/.bashrc下增加下述內(nèi)容:

source $HOME/.tmuxinator/.tmuxinator.bash
export EDITOR='vim'

source $HOME/.bashrc使其生效。

zsh版

將下述文本保存為$HOME/.tmuxinator/.tmuxinator.zsh,提供zsh的tab鍵提示功能

_tmuxinator() {
  local commands projects
  commands=(${(f)"$(tmuxinator commands zsh)"})
  projects=(${(f)"$(tmuxinator completions start)"})

  if (( CURRENT == 2 )); then
    _describe -t commands "tmuxinator subcommands" commands
    _describe -t projects "tmuxinator projects" projects
  elif (( CURRENT == 3)); then
    case $words[2] in
      copy|debug|delete|open|start)
        _arguments '*:projects:($projects)'
      ;;
    esac
  fi

  return
}

$HOME/.zshrc下增加下述內(nèi)容:

source $HOME/.tmuxinator/.tmuxinator.zsh
export EDITOR='vim'

source $HOME/.zshrc使其生效。

常用命令

Tmuxinator的一個工程(Project)對應tmux的一個session。

tmuxinator命令已alias為mux。

new簡寫為n,open簡寫為o,edit簡寫為e,list簡寫為l,copy簡寫為c,delete簡寫為d。

> mux n ws      # 創(chuàng)建工程ws
> mux o ws      # 打開工程ws的配置文件
> mux e ws      # 同上
> mux c ws ws1  # 復制ws工程到ws1
> mux d ws      # 刪除ws工程
> mux l         # 顯示所有工程
> mux ws        # 開啟ws工程

配置

當new一個工程后,會出現(xiàn)如下信息(省略注釋)。

name: ws # session名稱
root: ~/ # 工程根目錄,活動Pane會首先cd到此目錄

windows:
  - editor: # 第1個名為Editor的Window
      layout: main-vertical # Pane的布局
      panes: # 各個Pane
        - vim # 第一個Pane運行vim命令
        - guard # 第二個Pane運行guard命令
  - server: bundle exec rails s # 第2個名為server的Window,運行命令為bundle
  - logs: tail -f log/development.log # 第3個名為logs的Window,運行命令為tail

可以根據(jù)注釋配置自己的工程。

自定義layout

工程配置中的layout項,有5個默認的值。

  • even-horizontal
  • even-vertical
  • main-horizontal
  • main-vertical
  • tiled

開啟tmux后,可以使用快捷鍵prefix space切換layout,建議開啟4個Pane進行測試。

下面簡單演示下這5個默認布局的樣式,面板配置如下:

panes:
- top
- top
- vim .
- vim .

even-horizontal

even-horizontal

even-vertical

even-vertical

main-horizontal

main-horizontal

main-vertical

main-horizontal

tiled

tiled

其中main-horizontal和main-vertical可以設置默認主Pane的寬度和高度。

在$HOME/.tmux.conf文件中添加下面這些內(nèi)容:

set-window-option -g main-pane-width 100 # 設置主Pane寬度
set-window-option -g main-pane-height 80 # 設置主Pane高度

如果不滿足layout默認值,layout項可以自定義值。
首先調整好窗口的Pane,prefix d關閉Session。

> tmux list-windows
1: bash* (4 panes) [211x47] [layout 9a0a,211x47,0,0{110x47,0,0,12,100x47,111,0[100x23,111,0,13,100x23,111,24{49x23,111,24,14,50x23,161,24,15}]}] @3 (active)

將上述layout之后的信息(到最后一個]前),復制到工程配置中的layout項即可。注意pane的個數(shù)必須與執(zhí)行命令的個數(shù)對應。

windows:
  - editor:
      layout: 9a0a,211x47,0,0{110x47,0,0,12,100x47,111,0[100x23,111,0,13,100x23,111,24{49x23,111,24,14,50x23,161,24,15}]}
        - # empty
        - # empty
        - # empty
        - # empty

多命令

當某個Pane需要執(zhí)行多命令時,官方不推薦使用&&或;的形式。可以采用如下方式發(fā)送命令。

windows:
  - editor:
      layout: main-vertical
      panes:
        - list: # 多命令方式
          - cd ~/temp
          - ls -la
        - # empty

參考文章

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 在 Ubuntu 中安裝Tmux: 在終端輸入如下命令: sudo apt-getinstall tmux Tmu...
    MiracleJQ閱讀 3,426評論 0 1
  • tmux是一個終端多路轉換器(multiplexer),它支持在同一個screen內(nèi)創(chuàng)建、訪問和控制多個終端。tm...
    roger_lyb閱讀 673評論 0 0
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,971評論 6 342
  • 今天感覺突然想不出主題了,上課時倒是聽到老師說的很多有觸動的話,今天就先說這句吧。 “如果文章寫不下去了,怎么辦?...
    小小恙閱讀 450評論 0 0
  • 簡書接龍客棧純文字協(xié)會 接龍客棧純文字協(xié)會【七夕接龍】 文|歪果果 成員:清雨淺淺 女鋼鐵俠 歪果果 【目錄】...
    ajctyp666閱讀 316評論 1 3