Sharpening Tools with TMUX

Wed, Apr 18, 2012
TMUX - Productive Mouse Free Development

A little over a year ago I switched to vim as my editor of choice, and at the same time started using tmux to manage a split pane terminal, but I spent so much time getting up to speed with vim that I never really made much of an effort to embrace tmux - I learnt the shortcut key to split the window and that was about it - good enough for a while, but not exactly a power user!

Recently, the Pragmatic Programmers published an ebook by Brian Hogan tmux - productive mouse free development that made me revisit this tool and see what else I could get out of it - and boy, am I glad I did - tmux is a really useful tool, and Brian’s book covers it in an excellent, easy to read, and comprehensive format that covers the following areas:

  • Learning The Basics
  • Configuring tmux
  • Scripting Customized tmux Environments
  • Working With Text and Buffers
  • Pair Programming with tmux
  • Workflows

Working my way through the book was a breeze. Its well written, well organized, and doesn’t waste any space - except maybe the pair programming chapter - which is not really my cup-of-tea. :-)

I now find myself jumping around panes and windows with ease and able to setup environments that really suit the task at hand. For example, as I write this article I have four windows in a TMUX session:

  • my VIM editor
  • a multi-pane console
  • a running web server
  • a tailing logfile

Example tmux window and pane configuration

The 4 ‘tabs’ at the bottom allow me to instantly see which hotkey (prefix-1/2/3/4) will take me to which window, the current window is highlighted in red and any activity in any non-visible window will cause that windows name to highlight as well. Thats a lot of information in that small TMUX footer.

One of my favorite discoveries is the ability to create tiny little tmux scripts to startup sessions pre-configured with windows and split-panes suitable for my current task.

For example, if I want a session for SSH’ing in to do some operations work on our production live site, I can run a little script like this:

#!/bin/sh

tmux has-session -t lplive
if [ $? != 0 ]
then
  tmux new-session   -s lplive -n "LIQUIDPLANNER-LIVE" -d "ssh jake@lp-web-1"
  tmux split-window  -t lplive:1.0 -h "ssh jake@lp-web-2"
  tmux split-window  -t lplive:1.0 -v "ssh jake@lp-db-1"
  tmux split-window  -t lplive:1.1 -v "ssh jake@lp-db-2" 
  tmux select-pane   -t lplive:1.0
fi
tmux attach -t lplive

And bang! I instantly have a 3 pane window that has SSH’d into the various production host machines. Then I can simply detach when I’m done, leaving it running to attach to when I come back later on, even potentially, from a different machine.

I dont intent to give any kind of tmux tutorial here. I just wanted to highly recommend you try out the tool and go buy this book.

My tmux.conf

For anyone curious, my personal TMUX configuration ended up looking like this:

set -g prefix C-a

bind q     kill-pane 
bind r     command-prompt "rename-window '%%'"
bind s     display-panes
bind |     split-window -h
bind -     split-window -v
bind C-a   send-prefix
bind C-c   copy-mode
bind C-v   choose-buffer
bind C-r   source-file ~/.tmux.conf; display "Reloaded!"
bind C-s   choose-session
bind space last-window

bind    h   select-pane -L
bind    j   select-pane -D
bind    k   select-pane -U
bind    l   select-pane -R
bind -r H   resize-pane -L 10
bind -r J   resize-pane -D 10
bind -r K   resize-pane -U 10
bind -r L   resize-pane -R 10
bind    C-h previous-window
bind    C-l next-window

set -sg escape-time 1
set -sg repeat-time 200

set -g default-terminal "screen-256color"
set -g base-index 1
set -g status on
set -g status-left-length 40
set -g status-right-length 40
set -g status-left '#[fg=green]#H:#[fg=colour118]#S     '
set -g status-right '  %d %b %R  '
set -g status-bg black
set -g status-fg white
set -g status-utf8 on
set -g status-justify centre
set -g set-titles on
set -g set-titles-string '#H:#S  #W #P'
set -g visual-activity on
set -g mouse-select-pane on
set -g mouse-resize-pane on
set -g mouse-select-window on

setw -g mode-mouse on
setw -g mode-keys vi
setw -g utf8 on
setw -g automatic-rename on
setw -g monitor-activity on

setw -g window-status-fg colour241
setw -g window-status-bg default
setw -g window-status-attr dim

setw -g window-status-current-fg colour196
setw -g window-status-current-bg black
setw -g window-status-current-attr bright

set -g pane-border-fg colour241
set -g pane-border-bg black
set -g pane-active-border-fg white
set -g pane-active-border-bg black

set -g message-fg white
set -g message-bg black
set -g message-attr bright

# ALLOW PUTTY C-Arrow KEYS TO PASSTHROUGH (see tmux FAQ)
set -g terminal-overrides "xterm*:kLFT5=\eOD:kRIT5=\eOC:kUP5=\eOA:kDN5=\eOB:smkx@:rmkx@"

#==============
# CHEAT SHEET
#==============
# Prefix C-a   - pass-through
# Prefix C-r   - reload tmux.conf
# Prefix C-s   - choose session
# Prefix d     - detach from session
# Prefix c     - create window
# Prefix r     - rename window
# Prefix w     - list windows
# Prefix n     - next window
# Prefix p     - previous window
# Prefix space - last window
# Prefix 1-9   - goto window n
# Prefix s     - show pane numbers
# Prefix q     - quit pane
# Prefix ?     - list all bindings
# Prefix :     - command-line
# Prefix |     - horizontal split
# Prefix -     - vertical split
# Prefix C-c   - copy mode
# Prefix C-v   - paste
# Prefix PgUp  - scroll mode