tech_memo / tmux


tech_memo

tmuxセッション内でsshした先でtmuxのコマンドをssh元に送信する

  • 下記のようなことがやりたい
    1. [LOCAL]:$ tmux
    2. [LOCAL]:$ ssh REMOTE
    3. [REMOTE]:$ tmux select-pane -D
  • tmuxのソケットをsshでフォワードすることで可能
    ssh -R /tmp/tmux-<REMOTE_UID>/default:/tmp/tmux-<LOCAL_UID>/default REMOTE_SERVER
    • リモートサーバ上の、/tmp/tmux-<REMOTE_UID>/default がすでに存在していると、ソケット作成に失敗するので、あらかじめ削除しておく必要がある

コマンドオプション

  • -r
    • プリフィックスを1回だけ入力して、以降、同じショートカットキーを連続で入力できる。
      bind -r n next-window
    • 連続入力の制限時間は、repeat-timeで設定できる。デフォルトは500ms。
      set-option -g repeat-time 1000

TPM (Tmux Plugin Manager)

  • github
  • インストール
    git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
  • tmux.conf設定
    # List of plugins
    set -g @plugin 'tmux-plugins/tpm'
    set -g @plugin 'tmux-plugins/tmux-sensible'
    
    # Other examples:
    # set -g @plugin 'github_username/plugin_name'
    # set -g @plugin 'git@github.com/user/plugin'
    # set -g @plugin 'git@bitbucket.com/user/plugin'
    
    # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
    run '~/.tmux/plugins/tpm/tpm'
  • リロード
    $ tmux source ~/.tmux.conf

RHEL6.3でtmuxのビルド

  • tmuxのソース
  • 依存PKG1 : libevent 2.x
    • RHEL6の標準は1.4なので、下記からソースを取ってきてビルドする
    • http://libevent.org/
    • ビルド後やること
      • libeven-2.x.so.xのlibディレクトリをLD_LIBRARY_PATHに通すなどして、参照できるようにする(もしくは参照できる場所にmake installする。libevent1.4との衝突に注意)
      • /usr/lib64/pkgconfig配下に、<make install先DIR>/lib/pkgconfig/libevent.pcを配備(リンクでもOK)
  • 依存PKG2 : ncurses
    • 標準DVDからインストール可能
    • ncurses-develも必要

tmux.conf

#=======================================================
# Basic Configurations
#=======================================================
set-window-option -g utf8 on

# keybind
set-window-option -g mode-keys vi
bind-key ` suspend-client
#------------------
# mouse
#------------------
 
# Until version 2.0
#bind m \
#  set -g mode-mouse on \;\
#  set -g mouse-resize-pane on \;\
#  set -g mouse-select-pane on \;\
#  set -g mouse-select-window on \;\
#  display 'Mouse: ON'
# bind M \
#  set -g mode-mouse off \;\
#  set -g mouse-resize-pane off \;\
#  set -g mouse-select-pane off \;\
#  set -g mouse-select-window off \;\
#  display 'Mouse: OFF'

# From version 2.1
bind m set-option -g mouse on
bind M set-option -g mouse off

# Dont need from version 2.3(?)
#bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"

set-option -g base-index 0

# Scroll History
set -g history-limit 10000

# Prefix
set-option -g prefix C-z
#set-option -g prefix C-q
unbind-key C-b
bind C-z send-prefix
bind a send-prefix

# Timer
set -s escape-time 0

#=======================================================
# View
#=======================================================
#-------------------------------
# Status line
#-------------------------------
set -g status-interval 5
set -g status-left-length 100
set -g status-right-length 50

set-option -g set-titles on
set-option -g set-titles-string "@#(hostname | sed 's/\\\\..*//')"

#-------------------------------
# bottom status bar
#-------------------------------
# Status bar has a dim gray background
set -g status-bg white
set -g status-fg cyan
set -g status-attr dim
# left :whoami@hostname
#set -g status-left '#[fg=blue,bold][#20(whoami)@#H(#I:#P)]#[defaujt]'
set -g status-left '#[fg=black,bg=white]#H#[fg=black]:#[fg=black,bg=green][#S#[fg=balack]]##[default]'
set -g status-left-length 30
# right :[2011/03/06(Sun)20:21]
#set -g status-right '#[fg=blue,bold][%Y/%m/%d(%a)%H:%M]#[default]'
set -g status-right '#[fg=black,bg=white] [%Y-%m-%d(%a) %H:%M:%S]#[default]'
set -g status-interval 1
set -g message-attr bold
set -g message-fg white
set -g message-bg red


#-------------------------------
# Pane
#-------------------------------
set -g pane-border-fg cyan
set -g pane-border-bg black
set -g pane-active-border-fg cyan
set -g pane-active-border-bg black

#-------------------------------
# Window
#-------------------------------
set-window-option -g mode-bg white
set-window-option -g mode-fg black
set-window-option -g window-status-bg black
set-window-option -g window-status-bg white
set-window-option -g window-status-fg white
set-window-option -g window-status-fg black
setw -g window-status-current-fg white
setw -g window-status-current-bg cyan
setw -g window-status-current-attr bold#,underscore # pane-active-border

#-------------------------------
# Window
#-------------------------------
bind 8 switchc -p
bind 9 switchc -n

#=======================================================
# KeyBindings
#=======================================================

# reload config
bind r source-file ~/.tmux.conf\; display-message "Reload Config!!"

#-------------------------------
# Window
#-------------------------------
bind Space choose-window
bind c new-window
bind -r n next-window
bind -r p previous-window
bind -n C-M-l next-window
bind -n C-M-h previous-window
bind -n M-Right next-window
bind -n M-Left previous-window
unbind right
unbind left
bind -r right next-window
bind -r left previous-window

# windows ^W w
unbind ^W
bind ^W list-windows
unbind w
bind w list-windows

# kill K k
unbind K
bind K confirm-before "kill-window"
unbind x
bind x confirm-before "kill-window"

#-------------------------------
# Pane
#-------------------------------
# split window
unbind %
bind \ split-window -h
bind - split-window -v

# move pane
bind-key k select-pane -U
bind-key j select-pane -D
bind-key h select-pane -L
bind-key l select-pane -R
bind-key -n M-k select-pane -U
bind-key -n M-j select-pane -D
bind-key -n M-h select-pane -L
bind-key -n M-l select-pane -R

# select pane
unbind Tab
bind Tab select-pane -t :.+
unbind BTab
bind BTab select-pane -t :.-

# display pane number
#   ex) C-a i 1
bind i display-panes

# resize pane
bind -r C-h resize-pane -L 1
bind -r C-l resize-pane -R 1
bind -r C-j resize-pane -D 1
bind -r C-k resize-pane -U 1
bind -r C-n resize-pane -D 6
bind -r C-p resize-pane -U 6
bind -n C-M-k resize-pane -U 1
bind -n C-M-j resize-pane -D 1

# select-layout
bind-key | select-layout even-horizontal
bind-key = select-layout even-vertical
bind-key ` select-layout tiled

# synchronize-pane
bind-key s set-window-option synchronize-panes on
bind-key u set-window-option synchronize-panes off


#-------------------------------
# Other
#-------------------------------
# Esc = copy-mode
#unbind [
bind Escape copy-mode

# v and y like vi in copy-mode
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

# copy-mode ^[
unbind ^"["
bind -r ^"[" copy-mode
unbind ^]
bind -r ^] paste-buffer