6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[tmux] ペイン移動,削除の際にウィンドウ名をリネームする

Posted at

やりたいこと

tmuxでウィンドウ名を自分で決めている人(目的別,サーバー別などで分けている人)でなければ,
現在実行しているコマンドや,ディレクトリ名がウィンドウ名になっていて欲しいと思うと思います。

tmuxの便利なオプションにautomatic-renameというのがあって,コマンド実行時に自動でウィンドウ名をリネームしてくれます。

ただし,使っていて不便に感じたのは二つ以上ペインを作ると,最後に実行したコマンドがウィンドウ名となって,いまフォーカスしているペインの名前がウィンドウ名とはならないことです。

そこで,ペイン間の移動や,ペインの削除,スワップなどを行った際に,そのウィンドウ名を自動でリネームするようにすると便利ですね。

どうするか

具体的にはtmux.confに以下のように書き,キーバインドの設定として既存の動作をした後にリネームを行うようにします。(各々自分のキーバインドに読み替えてください)

# if session has > 1 windows in current, kill-pane without confirmation.
# But confirm before killing pane when it is the last pane in the window.
bind-key -n M-c if "tmux display -p \"#{window_panes}\" | grep ^1\$" \
    "confirm-before -p \"Kill the only pane in the window? It will kill this window too. (y/n)\" kill-pane" \
    "kill-pane \; run 'tmux rename-window \"#{pane_current_command}\"'"

# move between panes with Alt+j/k
bind-key -n M-j select-pane -t :.+ \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-k select-pane -t :.- \; run 'tmux rename-window "#{pane_current_command}"'

# join pane with Alt+H/L
bind-key -n M-L join-pane -t :+ \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-H join-pane -t :- \; run 'tmux rename-window "#{pane_current_command}"'

# swap pane to Alt+shift+number
bind-key -n M-! join-pane -t :1 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-'"' join-pane -t :2 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-'#' join-pane -t :3 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-'$' join-pane -t :4 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-% join-pane -t :5 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-& join-pane -t :6 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-"'" join-pane -t :7 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-( join-pane -t :8 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-) join-pane -t :9 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-( join-pane -t :8 \; run 'tmux rename-window "#{pane_current_command}"'
bind-key -n M-) join-pane -t :9 \; run 'tmux rename-window "#{pane_current_command}"'

やっていることは,既存の操作に加えて\;でコマンドをつなげ,フォーカスの移った先で

run 'tmux rename-window "#{pane_current_command}"'

を実行しています。単に

rename-window "#{pane_current_command}"

とするだけでは#{pane_current_command}が文字列として表示されてしまったので,run-shellを使っています。

簡単にできる設定で効果は抜群なので,試してみてください。

僕のtmux.confです↓

問題点

  1. swap-paneしたときに,元ウィンドウもリネーム
  2. automatic-renameのフォーマットに合わせたい

もし分かる方いらしたら教えていただきたいです。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?