Turbo-charge your command-line workflow with Tmux

Tutorials – Tmux

Article from Issue 193/2016

Operate multiple terminal sessions in a single window.

Why do this?

  • Run multiple CLI apps in the same window
  • Save your SSH sessions for logging in later
  • Create custom layouts for your workflow

Unless you're some kind of hard-core raw X Window System user who doesn't need to resize or move around any of your applications, chances are that you're running a window manager of some sort. This may be a standalone window manager like Fluxbox, a tiling variant like i3, or perhaps one that's part of a larger desktop environment, such as Xfce, KDE, or Gnome. Window managers are one of the most essential tools for our work – but can they actually be used in text mode?

Well, yes. It might sound a bit weird at first, given that the command line is all about typing and viewing text and doesn't have the same requirements as a pointy-clicky, frill-laden desktop environment. But consider the typical work you do at a Bash prompt: Do you have a single terminal running at maximum screen resolution? Unless you're still rocking a netbook, chances are you have multiple terminals in action at any one point, using your window manager to organize them.

Now, imagine you could leave your window manager out of this and do the work of organizing different sessions inside a single terminal window. This is what Tmux, the "terminal multiplexer" does – and it does it very well [1]. With Tmux, you effectively have a text-mode window manager available at the command line, so you can create different views (e.g., with a big main command-line view for your day-to-day work and smaller views next to it for monitoring logs or IRC channels).

There are several benefits to doing this inside Tmux rather than your window manager. For starters, you can use Tmux when SSH'd into a remote machine and disconnect from that remote machine at any point, leaving all the sessions running. Once you have everything set up nicely, even if your connection drops, everything will be back where you left it once you reconnect. Compare this to having several terminal windows open on your desktop and needing to log in and start processes again in each one.

Plus, Tmux has minimal system requirements and can be installed almost everywhere. If you move around a lot between computers, Linux distros, or even operating systems, you can rely on Tmux to look, feel, and work the same. Oh, and it looks rather funky and scores you geek points as well. So, over the next few pages, I'll show you how to master it and boost your command-line productivity.

Getting Set Up

Tmux is available in almost every Linux distro under the sun, so you should be able to grab it via your package manager. Indeed, some distros include it in the stock installation. If, for some reason it's not available for your distro – or you want to get the very latest version – then see the "Building Tmux 2.3 from Source Code" box. Once you have it installed, start it in a terminal window by entering:

Building Tmux 2.3 from Source Code

A new version of Tmux was released in September 2016, so we couldn't resist installing it in time to write this feature. If the new version isn't in your distro's package repositories yet, or you just like to build software from its source code, then here's a quick guide.

First, grab the latest release [1] and save it to your home directory. Then open a terminal and enter:

tar xfv tmux-2.3.tar.gz
cd tmux-2.3

This extracts the compressed source-code archive and switches into the resulting directory. Next, run:


This sets up the build environment and checks that you have the necessary dependencies installed. Tmux is fairly light on dependencies, so you won't have to hunt down lots of obscure stuff to build it. The two main packages you need are the development headers for Ncurses and Libevent, so find those in your package manager. When ./configure has run successfully, enter:

sudo make install

This will build Tmux and install it (by default into /usr/local). Then you can run it by entering tmux as in the main text.


The screen will clear, and you'll be left at a new command prompt. Doesn't look like much, right? Switch your attention to the bottom of the terminal, and you'll notice a long green bar spanning the whole width of the window (Figure 1). This is known as the "status line" in Tmux parlance and is highly configurable; even in a default, stock Tmux setup, it still contains useful information, though.

Figure 1: Here's what Tmux looks like out of the box, with the green status line at the bottom.

For example, on the left-hand side you'll see a list of running programs – a bit like a traditional desktop taskbar. In this case, it's only showing Bash as that's the only program running. In a moment, however, we'll fill up this section. On the right-hand side of the status line, you'll see the hostname of the machine you're using, a clock, and the current date.

So, let's work with Tmux's most prominent feature: the ability to run multiple programs side by side. Enter nano to start a text editor, and then press Ctrl+B followed by C on its own (both lowercase). Eek – Nano has disappeared! You're presented with a new command line. What has happened here?

Well, Ctrl+B is the key combo used to send a command to Tmux. In most of your interaction with Tmux, you'll use this to get the program's attention – and then press another key to perform a specific action. In this case, we hit Ctrl+B to alert Tmux, and then C, which means "create a new window" (Figure 2). If you look at the left-hand side of the status line, it should say something like this:

Figure 2: When you create a new window, you can see it on the left of the status bar – in this case, the "1:bash*" part.
[0] 0:nano- 1:bash*

The number in square brackets refers to the Tmux session in use – you can ignore that for now. Then there's a list of windows, with associated numbers, and the window we're currently viewing has an asterisk (*) next to it. So, you can see that the Nano we started is currently sitting idle in window number 0, whereas the Bash prompt you're viewing right now is in window 2.

Switching between windows is easy: Just press Ctrl+B followed by the number. To switch back to Nano, you'd press Ctrl+B followed by 0. You can keep opening new windows using the aforementioned Ctrl+B and then C combo; to close them, just log out of the Bash session (e.g., with exit or Ctrl+D).

Now, when you have a lot of windows open, you may be doing a job that requires switching between just two of them for a while (e.g., editing a configuration file while reading a manual page). Having to use the specific window numbers each time can become rather tedious in this case, but Tmux has a solution: Ctrl+B followed by L. This switches to the "last viewed" window – a bit like hitting Alt+Tab once in a traditional desktop environment.

This is the killer feature of Tmux: the ability to run multiple programs in the same terminal window. But, it becomes especially useful in terms of its ability to keep sessions running, even when you close the terminal. To try this out, in your Tmux session, press Ctrl+B followed by D to "detach" from Tmux and return to the prompt. You will see something like:

[detached (from session 0)]

What's important here is that Tmux is still running, and the programs it started are still running as well. You can close the terminal window, open a new one, and then enter the following to reattach to the Tmux you started before:

tmux a

How's that? It's an extremely useful feature that really comes to life when you're SSH'ing into other machines. If you do a lot of admin work on a server, for instance, or a lot of playing around via SSH on a Raspberry Pi, you can log in, run Tmux, and set up the session to your liking – with different programs in different windows. If you need to disconnect for a while, just detach and close the terminal window. Later, you can SSH back in, run tmux a to reconnect, and everything was exactly how you left it. Indeed, you'll wonder how you managed to live without Tmux before this point (well, unless you were using GNU Screen, which is similar but not quite as awesome).

Custom layouts

So far we've been working with Tmux windows that fill the whole terminal window – but it's possible to split up the screen into multiple sections called "panes" as well. Create a new Tmux window with Ctrl+B followed by C, and then use Ctrl+B with percentage sign (%) to split the window down the middle (vertically) into two Bash sessions (Figure 3).

Figure 3: Here we used Ctrl+B followed by % to create a split down the middle of the screen, for two panes.

Now you have left and right panes, and you can switch between them by using Ctrl+B followed by the arrow keys. To identify the pane you're currently using, just see where the cursor is. To close a pane, you can simply exit the Bash prompt or use Ctrl+B then X to kill it.

Now, you probably don't want a direct split down the middle – it's more likely that you want a big pane on the left for doing your work, and a smaller one on the right for monitoring things. Fortunately, Tmux lets you resize panes to arbitrary sizes, very much like a normal window manager. To do this, use the usual Ctrl+B prefix, followed by Ctrl+Left/Right (the arrow keys). This changes the size in one-character increments – so it may take a few repeated presses to get the setup that you want. (In some setups, using Ctrl+B followed by Alt+Left/Right will resize in steps of five characters.)

But what about horizontal splits? Those are possible, too, using Ctrl+B followed by double quotation marks ("). This results in a window with a pane at the top and another at the bottom, switchable and resizable using the arrow key commands mentioned before (but with Left/Right replaced by Up/Down).

Note that it's possible to combine horizontal and vertical splits – so you could have one big pane on the left occupying around 70 percent of the window space, doing some editing work, while on the right you have two small panes, stacked on top of each other, with Htop and an IRC client running, for example. Tmux is tremendously flexible and caters to all kinds of layouts, and with time you'll be constructing elaborate setups with multiple sessions containing multiple windows containing multiple panes. If you spend a lot of time at the CLI, it's bliss.

Colors and Theming

At this point, you may be slowly getting bored with Tmux's default garish green in the status line – but as with almost everything in the program, it's highly configurable. You can change colors on the fly, but a better way to make them permanent is to create a .tmux.conf file in your home directory. The commands you add in there are mostly human-readable, for example:

set -g status-bg red
set -g status-fg white

As you might have guessed, this changes the status bar colors so that the background is red and the foreground is white (Figure 4). Note that you can also specify RGB hexadecimal values, like so:

Figure 4: Here we're taking it to the max, with multiple splits and a custom red status bar.
set -g status-fg "#0000ee"

But, of course, your terminal will need to support this – many only offer a limited range of colors. You'll often see messages on the status line in Tmux, especially if you try to do something that's not possible (e.g., moving to a window that doesn't exist). It's also possible to configure the colors of these messages like so:

set -g message-bg yellow
set -g message-fg black

Along with the status line, the borders separating panes can be configured to use different colors as well:

set -g pane-border-fg blue
set -g pane-active-border-fg red

What about the position of the status line? If you'd prefer to have it at the top of the screen, that's also easy to do:

set -g status-position top

Once you have your settings neatly organized in .tmux.conf, you can restart Tmux to use them. Alternatively, you can tell Tmux to re-read the config file by following the instructions in the "Changing the Command Key" box. For more options for customizing the colors and status bar layout, check out the handy online guide [2].

Changing the Command Key

If you're an Emacs user, you probably don't mind tapping Ctrl+B regularly to activate Tmux commands. But you may also have a single key sitting around doing very little on your keyboard, and you'd rather use that instead. This is possible in Tmux and requires a few commands in .tmux.conf in your home directory.

For example, we find that we very rarely use the back tick (`) key on our keyboards, but it's close to hand, so we want to use this single keypress instead of the Ctrl+B combination. In our .tmux.conf, we have the following:

unbind C-b
set-option -g prefix `
bind ` send-prefix

What if you really need to use a back tick now – for instance, when creating a multi-part command in Bash? The answer is simple: Just press back tick twice. This tells Tmux to ignore the first one and interpret the second as standard keyboard input.

Now you can create a new window with ` followed by C, or switch between the two most recent windows with ` followed by L, and so forth. Note that you can change your .tmux.conf and tell a running Tmux session to update itself accordingly by pressing Ctrl+B followed by colon (:), to open up a command line in the status line, somewhat like in Vi(m). Then enter:

source .tmux.conf

This tells Tmux to re-read the config file and change its settings.

Another good source of documentation (albeit rather dry and less hands-on) is the manual page, which you can access with man tmux or read online [3]. This provides a detailed list of all the keybindings available, along with commands that can be executed inside Tmux or placed into the .tmux.conf file.

You can even enable mouse support, allowing you to manipulate Tmux's panes with the rodent, making it a real hybrid between a text-mode and desktop window manager. But we doubt many people will want to use that. As with Vim, the real beauty of Tmux is found on the keyboard and in the fact you can do almost anything without having to move your fingers very far from the home row.

The Author

Mike Saunders thinks these newfangled "graphical user interfaces" will never really take off. Get off his lawn.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Tmux Configuration

    Instead of manually rearranging windows in a development environment time and time again, the Tmux terminal multiplexer can restore them from a configuration script.

  • Light Browsing

    This Chromium port can run inside any console, with minimal resources, and is a great tool for making old computers really useful – and learning programming along the way.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More