This post introduces tmux, a terminal emulator similar to GNU screen.

What is tmux?

For a great introduction to tmux, I recommend this book. This is a pretty good free reference. The creator of tmux is Nicholas Marriott, although there are now several other core contributors.

Basically, tmux allows one to split a terminal window up into smaller panes, create new terminal windows, and detach and reattach windows (and probably more). This would be a very useful tool, like GNU screen, if one were working on a server, and did not have access to a more advanced terminal emulator such as iTerm.

Installing tmux

To install tmux manually, see this post. Alternatively, assuming you are running OSX and brew is installed, in a terminal simply use:

brew install tmux

Starting a tmux session

To start a tmux session, in a terminal simply the command:

tmux

The window will change appearance, because you will be in a tmux session.

The Prefix key

While in a tmux session, all tmux commands starting with a prefix key, which by default is Ctrl b (hold the Ctrl key and press the b key; such a combination is usually abbreviated to C-b).

This default can be changed using a tmux.conf file (see below).

tmux shortcuts

A list of useful tmux shortcuts can be found in this post.

Detach a tmux session

To exit a tmux session (without killing it), while inside tmux use the shortcut: Prefix d.

When using the default prefix key: hold the Ctrl key, press the b key (the prefix), then press d.

This is known as 'detaching' a session.

Attach a tmux session

After detaching a session, if you want to reattach it, first see a list of the existing tmux sessions using:

tmux ls

which will show a list of the existing tmux sessions, e.g.

tmux ls
0: 3 windows (created Thu Apr  7 16:27:36 2016) [162x36]
5: 1 windows (created Sat Apr  9 20:31:00 2016) [162x36]

This example shows to sessions labelled 0 and 5. To attach session 0, use:

tmux a -t 0

which is a shorthand for

tmux attach -t 0

Kill a tmux session

In order to kill a tmux session, first detach it (Prefix d) and see a list of existing sessions using tmux ls. Suppose you want to kill the session labelled 0. On the command line, use:

tmux kill-session -t 0

Creating a tmux conf file

To customise the appearance of tmux, and change the default keyboard shortcuts, one can create/edit the file ~/.tmux.conf. This will work whether you use your personal computer or a server (with a personal home directory).

Since there are many options that can be changed/specified in a ~/.tmux.conf file, this is discussed in another post in detail.

The ~/.tmux.conf file I use can be downloaded here.

Saving tmux sessions

Sometimes your computer may need to be restarted. However, a restart will kill any open or detached tmux sessions. An enterprising developer created tmux-resurrect to allow tmux sessions to survive a reboot by 'saving' them.

Installing tmux-resurrect

Following the instructions on the GitHub page, clone the repo to some location (e.g. ~/path/to/resurrect) using:

git clone https://github.com/tmux-plugins/tmux-resurrect ~/path/to/resurrect

Then, place the following line at the end of the ~/.tmux.conf file:

run-shell ~/clone/path/resurrect.tmux 

Finally, in order to have the pane contents restored, add the following line to the ~/.tmux.conf file:

set -g @resurrect-capture-pane-contents 'on'

See this post for more details on this last step.

That's it! It is ready to use.

Using tmux-resurrect

While in a tmux session, use the following shortcuts:

Prefix Ctrl-s save current tmux session
Prefix Ctrl-r reload saved tmux session

How it works

It saves files to:

~/.tmux/resurrect/

and then reloads the data from there.