Tutorial: Get More From Terminals With tmux

tmux is an open source “terminal multiplexer.” Essentially, it allows you to switch between several programs within a single terminal—particularly useful on a VPS, when you can’t simply launch another iTerm/xterm/etc window and attack the problem from another. Added benefits include being able to detach from a process while keeping it running in the background, and then reattach on another terminal.

For a lot of dedicated Linux administrators, tmux is a must-have. It can be a bit overwhelming for those who are just getting started, so in this tutorial we’ll walk through the basics of getting tmux set up, basic navigation, and a few possible configurations.

Prerequisites

  1. A VPS running Ubuntu, Debian, or CentOS
  2. Or, your local machine (running OpenBSD, FreeBSD, NetBSD, Linux, OS X, or Solaris)

Installing tmux

To install on an Ubuntu/Debian VPS, type in the following:

$ sudo apt-get install tmux

And on CentOS:

$ sudo yum install tmux

Managing sessions

The core of tmux is sessions. Here’s where you can create separate environments that persist, even when you detach from them.

For example, many developers like to create separate sessions for individual projects they’re working on, or, if they work from home, they’ll create a “work” and “home” session to keep things separate.

Feel free to rename session to anything you would like.

Create a new session
$ tmux new -s session
Attach to an existing session
$ tmux attach -t session
$ tmux a -t session
Switch to an existing session
$ tmux switch -t session_name
List sessions
$ tmux list-sessions
$ tmux ls
Detach from the session
$ tmux detach
End a session
$ tmux kill-session -t myname

Using windows, aka tabs

Each session can contain a number of windows, which behave like tabs. You can create different windows for different commands or editors for dealing with files.

Create a new window
$ tmux new-window
Switch to an existing session
$ tmux select-window -t 0-9
Rename a window
$ tmux rename-window

Managing panes

Panes allow you to split up a single terminal into multiple areas, each with their own prompt. Many developers like to create one pane for using their editor of choice—vim, for example—alongside one or more panes for running commands and watching output.

Split the window into vertical panes
$ tmux split-window
Split the window into horizonal panes
$ tmux split-window -h
Swap current pane with another in the specified direction (up, down, left, right)
$ tmux swap-pane -[UDLR]
Select the pane in the specified direction
$ tmux select-pane -[UDLR]

Using the tmux prefix

All of the above commands can be executed more efficiently by using the tmux prefix, which, by default, is initiated using Ctrl + b. Type in the prefix, followed by the key listed below.

:new      create a new session
s         list sessions
d         detach from session
$         name the current session
(         move to previous session
)         move to next sessions

c         create a new window
,         rename window
w         list windows
0-9       move to specified window
p         previous window
n         next window
&         kill window

"         split the window into two vertical panes
%         split the window into two horizontal panes
o         swap between panes
x         kill panes
+         convert pane into window
-         convert window into pane
{space}   toggle between layouts

:         open the tmux prompt
?         list shortcuts

Some additional tweaks

The great thing about tmux is that it’s almost infinitely customizable according to your needs. You’ll have to spend some time in the documentation to really get a handle on everything ,but here’s some of our favorite and must-have configurations.

For all of these, you’ll have to create/edit your ~/.tmux.conf, and then type in tmux source-file ~/.tmux.conf to reload newly-edited file.

Change the prefix to your key combination of choice

Most people aren’t a huge fan of the default Ctrl + b prefix. You can change it to whatever works best for you—many prefer Ctrl + a.

# Remap prefix to Control + a
set -g prefix C-a
unbind C-b
bind C-a send-prefix
Easier reloading of tmux.conf
# Reload configuration file
bind r source-file ~/.tmux.conf
Start windows and panes at 1, not 0

For some, it can be frustrating that windows are ordered 0 through nine, given that 0 and 1 are on opposite sides of the keyboard. You can change

# Start windows and panes at 1, not 0
set -g base-index 1
setw -g pane-base-index 1
Quicker pane cycling

If you spend a lot of your day switching between panes, constantly typing in the prefix is going to get tedious, and will probably slow you down. This tweak allows you to switch panes without even using the prefix—instead, moving around is bound to the Meta key, which is typically the Alt key.

# Quicker pane switching
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
Enable mouse mode

tmux is meant to help you get more done just by using the keyboard—hence all those shortcuts—but sometimes it’s nice to be able to use the mouse every now and then. Enabling that is really easy:

# Enable mouse mode (tmux 2.1 and above)
set -g mouse on

Conclusion

As mentioned before, tmux enables infinite possibilities, so make sure you take your time to understand the precise setup that works for you. There are thousands of pre-built configurations out there, such as .tmux, tmux-powerline, and tmux-config, but we recommend doing as much of the configuration yourself as possible. That way, you’ll know exactly what’s going into your tmux setup, allowing for maximum productivity.

Have a tmux.conf you’d like to share, or any other tips or tricks we should mention? Let us know in the comments.