Using Tmux - A Terminal Multiplexer

24 Mar 2013

I have long wanted a good terminal multiplexer to improve my workflow on a terminal; I tried Terminator before, but I did not like it very much. I heard of Screen, but never tried it. After searching around, I decided to give Tmux a try. It does takes some time to learn and has some compatibility issues with Vim; but that can be circumvented by configuring tmux properly, thanks to tmux’s highly configurable nature.

The first problem I encounterd when started using tmux is Vim no longer recognizes modifier keys - SHIFT, CTRL and ALT, etc. The problem is with an environmental variable both tmux and vim rely on: $TERM.

Thankfully, I found a thread here, which also explains a valid solution for my use case. If you need a more detailed explanation, go check this blog post.

So, problem solved. But then I had a hard time configuring tmux to be vim-user friendly. Time to study something again.

First I found this nice tutorial for a tmux beginner. From this tutorial, you can grasp clear pictures of the default key mappings in tmux, which is a corner-stone for configuring it. In the second part of this tutorial, it shows some configuration examples, however not exactly fitting my taste.

Then, I found this gist post, which became my bootstrapping .tmux.conf file. I also learned some more configuration tips from this post as well, which is more comprehensive.

Of course, after all these efforts configuring tmux, I have already learned “man tmux” and “prefix ?” are really good friends.

My current .tmux.conf file:

# reset shell to ordinary shell; instead of login shell
set -g default-command $SHELL

# tell tmux to pass xterm key combinations to xterm
set-window-option -g xterm-keys on

# 0 is too far from ` ;)
set -g base-index 1

# all nice options
set -g status-keys vi
setw -g mode-keys vi
setw -g monitor-activity on

# remap prefix to ALT-a
unbind C-b
set-option -g prefix M-a
bind-key M-a send-prefix

# Automatically set window title
set-window-option -g automatic-rename on
set-option -g set-titles on

# statusbar
set-option -g   status-utf8 on
set-option -g   status-justify left
set-option -g   status-left-length 15
set-option -g   status-left ' #h |'
set-option -g   status-right ' | %Y-%m-%d %H:%M #[default]'

# default statusbar colors
set-option -g   status-fg white
set-option -g   status-bg blue
set-option -g   status-attr bright

# default window title colors
set-window-option -g window-status-fg white
set-window-option -g window-status-bg blue
set-window-option -g window-status-attr dim

# active window title colors
set-window-option -g window-status-current-fg yellow
set-window-option -g window-status-current-bg red
set-window-option -g window-status-current-attr bright
#
set-window-option -g mode-fg white
set-window-option -g mode-bg red
set-window-option -g mode-attr bright

# Alt-n/p to switch windows without prefix key
bind -n M-p previous-window
bind -n M-n next-window

# Splitting windows into panes with h and v
bind-key v split-window -h
bind-key s split-window -v

# Use Alt-vim keys without prefix key to switch panes
bind -n M-h select-pane -L
bind -n M-j select-pane -D 
bind -n M-k select-pane -U
bind -n M-l select-pane -R

# quick pane cycling
unbind ^A
bind M-a select-pane -t :.+

# resize panes
unbind M-Left  ; unbind-key C-Left  ; unbind Left
unbind M-Right ; unbind-key C-Right ; unbind Right
unbind M-Up    ; unbind-key C-Up    ; unbind Up
unbind M-Down  ; unbind-key C-Down  ; unbind Down
bind -n M-Left    resize-pane -L 3
bind -n M-Right   resize-pane -R 3
bind -n M-Up      resize-pane -U 3
bind -n M-Down    resize-pane -D 3

# No delay for escape key press
set -sg escape-time 0

# force a reload of the config file
bind r source-file ~/.tmux.conf

# copy-mode settings; use vim-compatible settings to speed
# things up in copy-mode (scroll-mode)
unbind ] ; unbind p; unbind [
bind m copy-mode
bind p paste-buffer
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

Want to know more about Vi mode in tmux? Check this post.