Using tmux

I recently came across several getting started articles on tmux and decided to augment my existing workflow to take advantage of it. After getting over the initial hurdle of learning the tmux specific commands, things have been great. Below is a brief overview of what tmux is, how to use it and also highlight some of it’s features.

Sessions

tmux is a terminal multiplexer, meaning it allows you to run multiple virtual terminal sessions. Sessions are completely separate work environments. I create different named sessions for each project I’m working on. This is useful because as long as you don’t re-boot your computer, you can detach from the session and attach to the session at will while preserved context: working directories, command history and processes.

# start new
tmux

# starts a new session session_name
tmux new -s session_name (prefix + :new -s session_name)

# starts a new session name the same as current directory
tmux new -s `basename $PWD`

# attach
tmux a # (or at, or attach)

# attach to an existing session named session_name
tmux attach -t session_name
tmux a -t session_name

# switch to an existing session named session_name
tmux switch -t session_name

# lists existing sessions
tmux list-sessions (prefix + s)
tmux ls

# detach the currently attached session
tmux detach (prefix + d)

# kill session
tmux kill-session -t session_name

# rename session
prefix + $

Windows

Each session can have multiple windows that provide a tabbing interface. With them you can quickly move switch windows using keyboard shortcuts similar to how you Alt+Tab between OS windows.

# create a new window
prefix + c

# list windows / window numbers
prefix + w

# move to the window based on index
prefix + 0-9

# rename the current window
prefix + ,

# kill the current window
prefix + &

Panes

Panes allow you to split your windows into multiple panes both vertically and horizontally. This is great for viewing multiple command line utilities or scripts in development without hiding them behind tabs or making them background processes.

# split the window into two vertical panes
prefix + "

# split the window into two horizontal panes
prefix + %

# swap pane with another in the specific direction
prefix + { or }

# select the next pane in the specified direction (arrow keys)
prefix + [UDLR]

# toggle between pane layouts (space key)
prefix + ⍽

# for scrolling, enter copy-mode and then use arrow keys and exit w/ ESC 
prefix + [

# kill the current pane
prefix + x

Configuration

Out of the box tmux has sane defaults however you can customize it with your own configuration file. Below is my ~/.tmux.conf file.

# utf8
set-window-option -g utf8 on

# fix the titles
set -g set-titles on
set -g set-titles-string "#I:#W"

# 1-based window indexing
set -g base-index 1

# switch currently focused pane by mouse click
setw -g mode-mouse on
set-option -g mouse-select-pane on

# increase history to 100k
set -g history-limit 100000 

# aggressive resize
setw -g aggressive-resize on

# load custom key bindings
source-file ~/.tmux.keys

And my ~/.tmux.keys file.

# remap prefix to match GNU screen (i.e. CTRL+a)
set -g prefix C-a
unbind C-b
bind C-a send-prefix

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

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

# make the split panes more mnemonic
unbind '"'
unbind %
bind \ split-window -h
bind - split-window -v

Workflow

I often work on several different client and personal projects throughout the day. By naming both my tmux sessions and windows, I’m able to switch between them quickly and get back to right where I left off with my preserved command history in each pane. In addition, I usually split my windows into 2-3 panes so I have visibility of the stdout and stderr streams from my development tools. This gives me visibility into what’s happening vs. hiding them with background processes or terminal tabs.

Conclusion

Adding tmux to my development workflow has been a great improvement over what I was doing, making me more productive. Hopefully you’ll find it as useful as I have.

Published 24 Sep 2013 in Programming with tags: Tools, CLI.

Urban Faubion

Urban is a designer and developer with a love for creating digital products and services. He has a broad range of professional expertise in design, design research, interaction and user experience design, user interface development, software engineering and prototyping. He also enjoys playing soccer, bike touring, rock climbing, teaching mountaineering and traveling as much as possible.