やりたいこと
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
です↓
問題点
-
swap-pane
したときに,元ウィンドウもリネーム -
automatic-rename
のフォーマットに合わせたい
もし分かる方いらしたら教えていただきたいです。