みつきんのメモ

組み込みエンジニアです。Interface誌で「Yocto Projectではじめる 組み込みLinux開発入門」連載中

tmuxを使えるようにする

はじめに

Linuxでコンソールの画面を分割したり、切れたセッションに再アタッチしたり便利そうなtmux。 しかし何も設定しないで使用するとマウスホイールでスクロールしなかったり、スクロールバックて文字列をコピペする方法がわからなかったり、 Ubuntuの「端末」に慣れていると結構不便なイメージがあった。

今回、[02] tmux 2.6 で emacs 風のキーバインドにする ... Emacs風キー割り当て を参考に設定を作って使いやすそうにしてみた。

xclipのインストール

設定ファイルでxclipを使用するようにしたのでインストール

$ sudo apt udpate 
$ sudo apt install -y xclip

設定ファイル

~/.config/tmux/tmux.confを使用する。

$ mkdir -p ~/.config/tmux 
$ pushd ~/.config/tmux
$ cat << 'EOF' > tmux.conf
# ======================
# Setting For tmux
# based on https://qiita.com/robozushi10/items/810c1047c2e9e088cca6
# ======================
# プレフィックスキー C-x
unbind C-b
set-option -g prefix C-x

# 256色
set-option -g default-terminal screen-256color
set -g terminal-overrides 'xterm:colors=256'

#全てのベルを無視
set-option -g bell-action none
# ウィンドウ番号基準値
set-option -g base-index 1
# ウィンドウの自動リネームoff
set-window-option -g automatic-rename off
#ウィンドウで動作があるとステータスラインでハイライト
set-window-option -g monitor-activity on

# コピー、選択モードのキーバインドemacs
set -g mode-keys emacs

# 設定ファイル再読み込み r
bind r source-file ~/.tmux.conf \; display-message "Reloaded config !!"

# Sync
bind S setw synchronize-panes on
bind E setw synchronize-panes off

# 直前のウィンドウ t
bind C-t last-window

# デタッチ d
bind d detach

# タイトル変更 A
bind A command-prompt "rename-window %%"

# ウィンドウ選択画面
bind b choose-window

# 新ウィンドウ作成
bind 5 new-window

# 分割していたペインそれぞれをWindowに
# bind 0 break-pane

# ペイン終了
bind k kill-pane
bind 0 kill-pane

# ウィンドウ終了
bind C-c kill-window

# ペイン番号表示
#bind i display-panes

# ペインの縦分割
bind 2 split-window -v
# ペインの横分割
bind 3 split-window -h

# resize panes like vim
# feel free to change the "1" to however many lines you want to resize by, only
# one at a time can be slow
bind < resize-pane -L 1
bind > resize-pane -R 1
bind - resize-pane -D 1
bind + resize-pane -U 1

# history size
set-option -g history-limit 100000
set-option -g mouse on

bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M

#bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"

# ステータスラインの設定 {{{
# powerline setting
set-option -g status on
set -g status-interval 1
# window list alignment
set-option -g status-justify "left"
set-option -g status-left-length 150
set-option -g status-right-length 150
set-option -g status-left "#(~/.tmux/tmux-powerline/powerline.sh left)"
set-option -g status-right "#(~/.tmux/tmux-powerline/powerline.sh right)"
# # }}}

# window-status を中央揃えで配置する
set-option -g status-justify "centre"
# # status line の背景色を指定する。
set-option -g status-fg white
set-option -g status-bg black

# Copy Mode Start
# To copy:
#ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
# tmux 2.0 の場合
#ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
# bind-key -n -t emacs-copy M-w copy-pipe "xclip -i -sel p -f | xclip -i -sel c "
# bind-key -n -t emacs-copy C-w copy-pipe "xclip -i -sel p -f | xclip -i -sel c "

#ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
# tmux 2.6 の場合
#ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
unbind -T copy-mode MouseDragEnd1Pane
bind-key -n -T copy-mode M-w send-keys -X copy-pipe-and-cancel "xclip -i -sel p -f | xclip -i -sel c "
bind-key -n -T copy-mode C-w send-keys -X copy-pipe-and-cancel "xclip -i -sel p -f | xclip -i -sel c "

# To paste:
bind-key -n C-y run "xclip -o | tmux load-buffer - ; tmux paste-buffer"

set-option -g status-left '#[fg=cyan,bg=#303030]#{?client_prefix,#[reverse],} #H[#S] #[default]'

# Clear screen and scroll-back
bind-key -n C-l send-keys C-l \; send-keys -R \; clear-history

EOF

セッション

セッションの確認

$ tmux ls

アタッチ

$ tmux a

セッション名指定

$ tmux a -t <session>

セッションの削除

$ tmux kill-session -t <session>

全部削除

$ tmux kill-server

paneを一時的に全画面

参考

<prefix> z

戻す場合はもう一度

まとめ

emacsキーバインドにしてマウスもある程度使えるようにしたら思いの外捗るのでびっくり。

sshした先でtmuxを使うとsshが切れたときに再接続ができるのは、やはり非常に便利。