Overview

So if you’ve used GNU Screen and liked it, I think you’ll love tmux. It’s basically GNU screen on steriods.

It all started when I wanted to split a terminal window into two sub-terminal windows, similar to how vim can split the terminal window to display multiple buffers.

Now that widescreen monitors are a thing (yes, I just dated myself), the best way to divide a screen is vertically. Unfortunately, GNU screen doesn’t support this with the release that comes with most distros unless you patch it. Furthermore, screen just looks kinda disgusting when it does a split.

Some googling quickly pointed me to tmux, which is pretty gosh-durned awesome.

I’ve only scratched the surface of its features, but I had some trouble getting my ~/.tmux.conf all set up the way I wanted, so I thought I’d write it up.

I found the following writeup from HawkHost super helpful as well:

If you just want my tmux.conf, here it is: tmux.conf

Escape Key

GNU screen defaults its escape key to CTRL-a, and tmux defaults to CTRL-b. I personally prefer the backtick or ` key, because it gets away from command sequences I use a lot that involve the CTRL key.

However, the backtick key shows up a lot in scripting, so we’ll need to be able to still type it. The following snippet will let you type a backtick by just hitting the key twice. A single press will get you into command mode.

Configure Escape Key
# command prefix (like screen)
unbind C-b
set-option -g prefix `
bind-key ` send-prefix

vi Key Bindings

I should’ve entitled this section "correct" key bindings…

vi Key Bindings
set-window-option -g mode-keys vi

tmux Conditionals

Sometimes you’ll need to have conditional commands, based on which machine you’re on, for example. Here’s an example of how to do it.

if-shell 'test $HOSTNAME = "<hostname>"' '<tmux command>''

Mouse Support

If you ever wanted to click on the tmux tabs or panes, now you can! This will totally break mouse scroll wheel support in vim though, in most contexts. But if you’re using vim you shouldn’t be using the mouse anyway.

In modern tmux versions, you can just do the following:

set -g mouse on

If you’re on an ancient version, you can do the following:

The Old Way
set-option -g mode-mouse on
set-option -g mouse-resize-pane on
set-option -g mouse-select-pane on
set-option -g mouse-select-window on
set-option -g mouse-utf8 on

Status Bar

One of tmux’s cool features is you can add a status bar to the bottom. I’ve chosen to display user@hostname on the left, and the load averages then date/time on the right.

Configure Status Bar
# Set status bar
set -g status-bg black
set -g status-fg white
set -g status-interval 30
set -g status-left-length 30
set -g status-left '#(whoami)@#(hostname -s)'
set -g status-right '#(cut -d " " -f 1-3 /proc/loadavg) %y-%m-%d %H:%M' 1
set-window-option -g window-status-current-bg white
set-window-option -g window-status-current-fg black
setw -g automatic-rename
1 This line must be set -g status-right #(uptime | cut -d ': -f4) %y-%m-%d %H:%M' in OSX.

Panes

Pane Coloring

If your version of tmux supports it, you can make your pane dividers look nice:

Beautifying tmux Panes
# Set pane divider
set -g pane-border-bg black
set -g pane-border-fg white
set -g pane-active-border-bg black
set -g pane-active-border-fg white

The problem is that older versions of tmux don’t support these features. Newer versions of tmux default active pane borders to this lime-green color which I find somewhat offensive. If you’re using the same .tmux.conf file across different machines you’ll run into the issue where tmux will fail to start with these commands in the configuration file.

Have a look at the section on [Conditionals] to fix it.

Pane Hotkeys

So, using the " key to make new panes is really annoying because it defaults to horizontal. In order to fix that, we can remap the pipe (|) and dash (-) keys to split the window vertically and horizontally if you want.

Pane Hotkey Mappings
# Set pane hotkeys
unbind %
bind-key | split-window -h
bind-key - split-window -v

Navigating Panes

The fastest way to navigate between panes is using the q hotkey. Doing so displays a number on each pane. Pressing the appropriate number on the keyboard will send you to the appropiate pane.

This is much better than navigating using the o hotkey for the next pane.

synchronize-panes

This feature is super useful for server administration. Basically it echos your commands across all panes. You’ll probably want to set this up on a toggle. Note that the tmux set command, if you don’t specify on/off, will assume you want the command to toggle. Thus you can do the following in your .tmux.conf file:

synchronize-panes Toggle
bind s set -g synchronize-panes

So tmux includes a link-window command, which is like a symlink for windows. Every time you make a change to a window, it’s reflected on the linked window. This holds true for windows in different sessions.

Unfortunately, as of 11-07-27, there is no link-pane command:

To work around this, we can use GNU screen!

In the source pane, run a new instance of screen. Then, in the pane you want to link to the source pane, run screen -x, which will open the screen session.

Credit goes to Saugata Ghose for asking about this feature in tmux, and for the following Unix adventure to figure out the workaround.