5
2

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] ターミナルの画面サイズが変更された際に stty で再設定

Posted at

背景

tmux はとっても便利な端末多重化ソフトですが、ひとつのウィンドウ内に複数のペインを追加したり削除したり、ペインサイズを変更したり等していると正確な画面サイズが反映されない場合があります。(Teraterm で ssh ログイン後 tmux を起動し、更に別のサーバへ ssh ログインを行っている場合等)

正確な画面サイズが各ペインに反映されていないと、vim 等でファイルを編集しようとした際に画面が崩れて表示されてしまいとっても困ってしまいます(´・ω・`)。

対策

そんな時のためにあらかじめ .tmux.conf に以下のような設定をしておき、正確な画面サイズを簡単に反映できるようにします。

現在選択しているペインのみ再設定する場合

.tmux.conf
bind R run-shell "tmux send-keys -t #{session_name}:#{window_index}.#{pane_index} \"stty rows #{pane_height}; stty columns #{pane_width}\" C-m"

これで「prefixキ-」-> R で

$ stty rows 26; stty columns 228
$ 

と、現在のペインに自動入力され、正確な画面サイズが反映されます。

他のウィンドウも含む全てのペインに再設定する場合

.tmux.conf
bind C-r run-shell "echo #{session_name} | ~/bin/tmux-stty.sh"

更に以下のシェルスクリプトを用意します。

  • ~/bin/tmux-stty.sh
# !/bin/bash

read var_session_name
while read var_target_pane var_pane_height var_pane_width; do
  tmux send-keys -t ${var_target_pane} "stty rows ${var_pane_height}; stty columns ${var_pane_width}" C-m
done < <(
 tmux lsw -F '#{session_name}:#{window_index}' -t ${var_session_name} | \
 xargs -I% tmux lsp -F '#{session_name}:#{window_index}.#{pane_index} #{pane_height} #{pane_width}' -t %
)

これで「prefixキ-」-> C-r で

$ stty rows 26; stty columns 228
$ 

と、全てのペインに自動入力され、正確な画面サイズが反映されます。

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?