Tutorial

How to Configure tmux on a Cloud Server

Published on May 20, 2013
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 

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
J Kim

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel