【Tmuxinator】tmuxのいつもの環境をコマンド1つで作る

スポンサーリンク

はじめに

Tmuxinatorを使って、コマンド1つで定義しておいたTmuxのセッションを起動できるようにしていきます。

Tmuxinatorとは

Tmuxinatorとは、Tmuxのセッションを簡単に作成、管理できるツールです。

コマンド1つであらかじめ決めていた形式で、セッションを作成できるので、わざわざTmuxを起動してからウィンドウ作って、ペインを分割して、、、という作業が必要なくなります。

GitHub - tmuxinator/tmuxinator: Manage complex tmux sessions easily
Managecomplextmuxsessionseasily.Contributetotmuxinator/tmuxinatordevelopmentbycreatinganaccountonGitHub.

インストール

Tmuxinatorをインストールする方法はgemを使う方法とbrewを使う方法がありますが、brewでのインストールは現状(2022/9/7)問題があるそうなので、解決されるまではgemでのインストールが推奨されています。

gem install tmuxinator

エディタの設定

Tmuxinatorをインストールしたら、EDITORの環境変数を設定しておきます。

export EDITOR='vim'

これで、Tmuxinatorがファイルを開くときのエディタが指定できます。

補完

コマンドの補完を有効にするために、それぞれのシェル用のファイルをダウンロードします。

GitHub - tmuxinator/tmuxinator: Manage complex tmux sessions easily
Managecomplextmuxsessionseasily.Contributetotmuxinator/tmuxinatordevelopmentbycreatinganaccountonGitHub.

zshの場合はこちらです。

wget https://raw.githubusercontent.com/tmuxinator/tmuxinator/master/completion/tmuxinator.zsh -O /usr/local/share/zsh/site-functions/_tmuxinator

エイリアス

zsh以外は補完用のファイルにエイリアスが定義されているようですが、将来的に削除する可能性があるそうなので、自分で定義しておきます。

alias mux=tmuxinator

簡単な使い方

最低限必要な、プロジェクト(定義したセッション)の作成と起動の方法を紹介します。

そのほかのコマンドをこちらをご確認ください。

GitHub - tmuxinator/tmuxinator: Manage complex tmux sessions easily
Managecomplextmuxsessionseasily.Contributetotmuxinator/tmuxinatordevelopmentbycreatinganaccountonGitHub.

プロジェクト用のファイルの作成

下記のコマンドでプロジェクトの設定ファイル(~/.config/tmuxinator/[project].yml)が作成され、EDITORで指定したエディタで設定ファイルが開かれます。

mux new [project]

現在いるディレクトリに設定ファイルを作成したい場合は、--localオプションをつけます。

mux new --local [project]

プロジェクトの起動

定義したセッション(プロジェクト)を起動する場合は、下記を実行します。

mux [project]

設定方法

プロジェクトの設定ファイルについて紹介します。

GitHub - tmuxinator/tmuxinator: Manage complex tmux sessions easily
Managecomplextmuxsessionseasily.Contributetotmuxinator/tmuxinatordevelopmentbycreatinganaccountonGitHub.

下記のような形式でセッションの定義をします。

name: basic # プロジェクト名
root: ~/work # デフォルトとなるディレクトリ
startup_window: main # プロジェクトが作成されたときにどのwindowにいるか
startup_pane: 0 # プロジェクトが作成されたときにどのパネルにいるか
windows: # windowの定義
  - main: # mainという名前のwindow
      # paneのレイアウト(even-horizontal, even-vertical, main-horizontal, main-vertical, tiled)
      layout: main-horizontal
      panes: # paneの定義
        - work1: # 1つめのpane
          - cd ~/work/work1 # 起動時に実行するコマンド
          - clear
        - work2: # 2つめのpane
          - cd ~/work/work2
          - clear
        - work3: # 3つめのpane
          - cd ~/work/work3
          - clear
  - sub: # subという名前のwinodw

私が今使っている設定の1つは下記のようにしています。(まだ使い始めたばかりなので、これから改善していく予定です)

name: k8s
root: ~/work/project1
startup_window: main
startup_pane: 1
windows:
  - main:
      layout: main-horizontal
      panes:
        - k9s:
          - k9s -n default
        - kubectl:
        - manifest:
          - cd ~/work/project1/manifest
          - clear
  - workspace:

参考

タイトルとURLをコピーしました