LoginSignup
77
71

More than 5 years have passed since last update.

2015年の(ほどほどにつよい) tmux環境設定まとめ

Last updated at Posted at 2015-12-14

去年ぐらいからぼちぼち使っていたtmuxもだいたい設定が固まったので、設定方法をまとめます。
基本的にLinuxでの使用を想定しています。

[参考] 使用時の見た目

無題.png

tmuxのビルド

少なくともtmux 2.1 以上はほしいのでソースからビルドを行っています。
ビルドに関しては以下の記事を参照してください。

TMUXの最新をソースからビルドする - Qiita

2015年12月時点での .tmux.conf

最初にも書きましたが、基本的にLinuxでの使用を想定していますので、
clipboardあたりは適宜修正して下さい。

.tmux.conf
# change prefix key to C-j
unbind C-b
set -g prefix C-j

# 設定ファイルをリロードする
bind r source-file ~/.tmux.conf \; display "Reloaded!"

# fix Esc key delay time for Vim
set -sg escape-time 0

# ウィンドウ分割後もカレントディレクトリに留まる
if-shell "~/dotfiles/bin/tmux-version-check 1.9" '\
    bind c new-window -c "#{pane_current_path}";\
    bind | split-window -h -c "#{pane_current_path}";\
    bind - split-window -v -c "#{pane_current_path}";\
'

# ウィンドウを閉じた時に番号を詰める
if-shell "~/dotfiles/bin/tmux-version-check 1.7" '\
    set-option -g renumber-windows on; \
'
# ステータスバーを上部に表示する
if-shell "~/dotfiles/bin/tmux-version-check 1.7" '\
    set -g status-position top; \
'

# マウス操作を有効にする
if-shell "~/dotfiles/bin/tmux-version-check 2.0" '\
    set -g mouse on; \
'

# コピーモードを設定する
## viのキーバインドを使用する
setw -g mode-keys vi
## コピーモードの操作をvim風に設定する
bind-key -t vi-copy C-v begin-selection
unbind -t vi-copy Enter

## copy to clipboard
### for Linux
if-shell "which xsel" '\
  bind-key -t vi-copy y copy-pipe "xsel -ib"; \
  bind-key -t vi-copy Enter copy-pipe "xsel -ib"; \
'

# Vimのキーバインドでペインを移動する
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

# Vimのキーバインドでペインをリサイズする
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

## リフレッシュの間隔を設定する(デフォルト 15秒)
set -g status-interval 1

## ウィンドウリストの位置を左寄せにする
set -g status-justify left

## ヴィジュアルノーティフィケーションを有効にする
setw -g monitor-activity on
set  -g visual-activity on

# ステータスバーを設定する
set-window-option -g allow-rename off
set-window-option -g window-status-current-format "#[fg=colour255,bg=colour241,bold] #I: #W #[default]"

## 左パネルを設定する
set -g status-left-length 50
set -g status-left \
"#{?client_prefix,#[reverse],}#[fg=green][#S:#I.#P]#[fg=yellow][#(whoami)@#h]  "

## 右パネルを設定する
set -g status-right-length 80
set -g status-right \
"#{?client_prefix,#[reverse],}"\
"#[fg=green]#(${HOME}/dotfiles/bin/git-echo-branch-tmux-current-pane)"\
"#[fg=yellow]#(${HOME}/dotfiles/bin/git-echo-username-and-email)"\
"#[fg=cyan][%Y-%m-%d(%a) %H:%M]"

#### COLOUR (Solarized dark)
#### cf: https://github.com/altercation/solarized/blob/master/tmux/tmuxcolors-dark.conf

# default statusbar colors
set-option -g status-bg black #base02
set-option -g status-fg yellow #yellow
set-option -g status-attr default
# default window title colors
set-window-option -g window-status-fg brightblue #base0
set-window-option -g window-status-bg default
#set-window-option -g window-status-attr dim
# active window title colors
set-window-option -g window-status-current-fg brightred #orange
set-window-option -g window-status-current-bg default
#set-window-option -g window-status-current-attr bright
# pane border
set-option -g pane-border-fg black #base02
set-option -g pane-active-border-fg brightgreen #base01
# message text
set-option -g message-bg black #base02
set-option -g message-fg brightred #orange
# pane number display
set-option -g display-panes-active-colour blue #blue
set-option -g display-panes-colour brightred #orange
# clock
set-window-option -g clock-mode-colour green #green

dotfiles/tmux.conf at master · koara-local/dotfiles

以下細かい説明

prefix key (C-j)

好みだとは思いますが、自分は bash と vim に競合しない C-j を使うようにしています。

バージョンチェック

バージョンアップでの新機能の追加や、後方互換性が切り捨てられたオプションもあったりするのでバージョンチェックを行えるようにしています。自分は ~/dotfiles/bin 以下にバージョンチェック用のスクリプトを配置しています。

tmux-version-check
#!/bin/bash

need_version=$1
current_version=$(tmux -V | awk '{print $2}')

[[ $(echo "$current_version > $need_version" | bc) != 0 ]]

dotfiles/tmux-version-check at 6806cf3d03bed5b8a59549709473dfec40b2e637 · koara-local/dotfiles

コピーモードを設定する (+ vimのキーバインドを使う)

vimに慣れすぎて、hjklじゃないと違和感があるのでviモードを設定しています。

# コピーモードを設定する
## viのキーバインドを使用する
setw -g mode-keys vi
## コピーモードの操作をvim風に設定する
bind-key -t vi-copy C-v begin-selection
unbind -t vi-copy Enter

# Vimのキーバインドでペインを移動する
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

# Vimのキーバインドでペインをリサイズする
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

あと、コピーモードでコピーした文字を他のアプリでも使用できるようにします。
Linuxの場合は xsel が必要です。他の環境は調べればすぐに出てくるので省略します。

## copy to clipboard
### for Linux
if-shell "which xsel" '\
  bind-key -t vi-copy y copy-pipe "xsel -ib"; \
  bind-key -t vi-copy Enter copy-pipe "xsel -ib"; \
'

[Linux] コマンドラインでの標準出力をクリップボードにコピーする - Qiita

リフレッシュの間隔を設定する

ステータスバーに色々と表示するため、更新周期を1秒に変更しています。
デフォルトは15秒なので、そのままだとちょっと反応が遅いです。

## リフレッシュの間隔を設定する(デフォルト 15秒)
set -g status-interval 1

Gitの情報をステータスバーに表示(ブランチ名、user.name, user.emailを表示)

無題.png

ステータスバーにgitの情報を表示しておくと便利です。
tmux display-message -p -F "#{pane_current_path} を使えば今のペインでのカレントディレクトリのパスを取得できます。
あとは表示したい情報を取得してechoで返してやればよいと思います。

## 右パネルを設定する
set -g status-right-length 80
set -g status-right \
"#{?client_prefix,#[reverse],}"\
"#[fg=green]#(${HOME}/dotfiles/bin/git-echo-branch-tmux-current-pane)"\
"#[fg=yellow]#(${HOME}/dotfiles/bin/git-echo-username-and-email)"\
"#[fg=cyan][%Y-%m-%d(%a) %H:%M]"
git-echo-branch-tmux-current-pane
#!/bin/bash -e

cd `tmux display-message -p -F "#{pane_current_path}"`
branch_name=`git branch | grep \*.* | sed -e 's/\*\ //'`

[ ! -z ${branch_name} ] && echo "[${branch_name}]"
git-echo-username-and-email
#!/bin/bash -e

cd `tmux display-message -p -F "#{pane_current_path}"`
user_name=`git config --get user.name`
email_address=`git config --get user.email`

echo "[${user_name} | ${email_address}]"

プレフィックスキーが押されたのをわかりやすくする。

?client_prefix でプレフィックスキーが押されたかを判定して、ステータスバーの色を変えています。

無題.png

tmux で Prefix key が押されているかどうかを表示する - Qiita

カラースキームを変更する

デフォルトの緑色も味があっていいですが、自分はsolarizedに変更しています

#### COLOUR (Solarized dark)
#### cf: https://github.com/altercation/solarized/blob/master/tmux/tmuxcolors-dark.conf

# default statusbar colors
set-option -g status-bg black #base02
set-option -g status-fg yellow #yellow
set-option -g status-attr default
# default window title colors
set-window-option -g window-status-fg brightblue #base0
set-window-option -g window-status-bg default
#set-window-option -g window-status-attr dim
# active window title colors
set-window-option -g window-status-current-fg brightred #orange
set-window-option -g window-status-current-bg default
#set-window-option -g window-status-current-attr bright
# pane border
set-option -g pane-border-fg black #base02
set-option -g pane-active-border-fg brightgreen #base01
# message text
set-option -g message-bg black #base02
set-option -g message-fg brightred #orange
# pane number display
set-option -g display-panes-active-colour blue #blue
set-option -g display-panes-colour brightred #orange
# clock
set-window-option -g clock-mode-colour green #green

その他のdotfiles

最新のtmux.confやほかの設定は dotfiles にまとめてありますので、よろしければどうぞ。

77
71
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
77
71