How to Configure tmux on a Cloud Server


About tmux

tmux is a terminal multiplexer. It allows you to access a tmux terminal using multiple virtual terminals.

The installation and basic usage of tmux has been described in its related article.

This article is focused on configuring tmux on a cloud server (something notoriously difficult with screen). tmux's configurability is another powerful tool available to us.

The Configuration File

When tmux starts up, it looks in your home directory for a file called .tmux.conf. If it exists, then tmux uses the configurations set up in that file.

With your favourite text editor, open up a new file in your home directory called .tmux.conf.

For tmux, lines beginning with a "#" are comments.

The Prefix Key

The prefix key is the key combination that comes before all commands in tmux. By default, the prefix key is Ctrl-b. Not only is this key combination uncomfortable to press on most keyboards, it also happens to conflict with a few other bindings (such as in vim).

A common substitute is Ctrl-a. This much more comfortable combination comes from screen, which has used this prefix before tmux was born.

We can enable this in all sessions of tmux with the inclusion of a single line in our tmux.conf:

set-option -g prefix C-a

Since we won't be using Ctrl-b for our prefix anymore, we should disable it:

unbind-key C-b

Beware, Ctrl-a does conflict with Emacs text editor.

Sending the Prefix to Nested tmux Sessions

After using tmux for a while, you may start to use tmux to connect to remote cloud servers in which you also use tmux.

Trying to perform a command in the remote session becomes impossible, because the prefix is intercepted and processed by the local tmux session.

Luckily, tmux has a built-in solution for that called send-prefix. Normally, send-prefix is bound to Ctrl-b, the same key combination as our prefix key. This means that sending a prefix could have been done with Ctrl-b Ctrl-b.

But we just unbound Ctrl-b because it may conflict with other applications. We can stay in the spirit of the command (repeating the prefix) by binding Ctrl-a to send-prefix:

bind-key C-a send-prefix

Now, sending the prefix to a remote session is as easy as Ctrl-a Ctrl-a.

Windows Numbering

Unlike panes, windows use a zero-based index. The 0 key is far away from 1 and is unwieldy to press on most keyboards.

We can force tmux to number windows like panes, from 1 instead of 0:

set -g base-index 1

Disabling Escape Sequence Delay

tmux comes with support for escape sequences. This is sometimes handy, but more often it gets in the way, such as when using vim or even the shell. Particularly, tmux waits half a second after an escape in order to determine whether the next key may be part of function or sequence.

This is usually undesirable behaviour for tmux, so we can disable it:

set-option -sg escape-time 0

Overall Setup

Following all the steps in this article, a good starter .tmux.conf could look like this:

# Ctrl-b is a bad prefix key, use Ctrl-a instead
set-option -g prefix C-a
unbind-key C-b

# Also assign Ctrl-a to send the prefix to a remote session
bind-key C-a send-prefix

# Windows should be numbered from 1 like panes
set -g base-index 1

# Disable tmux waiting for an escape sequence
set-option -sg escape-time 0 
Creative Commons License