Introduction to terminal multiplexer Tmux

Tmux is a terminal multiplexer: it let us run and manage multiple terminal sessions from a single screen. This is specially useful when connecting to remote machines using ssh, since, among the other things, it allows us to keep processes started from those terminals running in the background when we disconnect from the session (or logout and close the remote secure shell altogether), letting us re-attach to it at a later time.

In this tutorial we see how to install Tmux in some of the most used Linux distributions and learn the basic concepts behind its usage.

In this tutorial you will learn:

  • How to install Tmux on some of the most used Linux distributions
  • How to manage Tmux sessions
  • How to manage Tmux windows and panes
article-main
Introduction to Tmux

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution-independent
Software Tmux
Other Root privileges to install software
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Installation

Free and open source software, Tmux is available in the repositories of all the major Linux distributions, so it can be easily installed by using our package manager of choice. To install Tmux on Fedora, for example, we use dnf:

$ sudo dnf install tmux

To perform the installation on recent versions of Debian and Debian-based distributions, instead, we can use the following command:

$ sudo apt install tmux

Tmux is also available in the “Community” repository of Achlinux. We can install it with pacman:

$ sudo pacman -S tmux

Why Tmux?

The benefits of using Tmux become evident when we connect to a machine via ssh. Suppose we issue a long running command or an interactive application like “htop” in the remote shell. At a certain point we realize we need to run another command; at this point we have two choices: stop the first process, or open another ssh connection from our local machine, in another terminal.



This is where Tmux comes in handy: once connected to a remote shell we can invoke tmux and start a new session which can contain multiple windows, which in turn, can be divided in many sections or panes. This allows us to open several terminals from a single connection. A further advantage is that we can disconnect from a Tmux session while the commands we invoked in it keep running in the background, and re-attach to it later.

Managing Tmux sessions

To start using Tmux, all we have to do is to invoke it from our terminal emulator:

$ tmux

Once Tmux starts, it creates a new session which is composed of a single window. On the bottom of the screen, a status line is displayed. It contains information about the session itself, and can be used to run commands:

Our first Tmux session
Our first Tmux session

In the left side of the status line we can see the session identifier in square brackets, and the names of the windows opened in it. At the moment we have only one window (bash). On the right side of the status bar, instead, we can see the hostname of the machine we are connected to, and the current date and time.

As you can see in the picture above, by sessions are identified by numbers. We can, however, create named sessions by invoking Tmux in the following way:

$ tmux new -s <session-name>



New sessions can be launched also from inside Tmux. In order to achieve this task all we have to do is to press what in the course of this tutorial we will call the “prefix” keys combination, which by default is Ctrl-b, followed by a : (colon). Doing so, we enter command mode, than, to open a new session, we use the new command:

:new -s <session-name>

An existing session can also be renamed. In order to do so, we press <prefix> followed by the $ key, than we enter the new session name in the status bar, and press enter to confirm it:

Renaming the session
Renaming the session

Listing existing sessions

Sooner or later we may want to obtain a list of the existing Tmux sessions. We can do it either from command line or from the Tmux interface. In the first case we run:

$ tmux list-sessions

To perform the same operation from inside Tmux, instead, we press <prefix> followed by the s character:

The Tmux sessions list
The Tmux sessions list

In this case, as you can see from the screenshot above, there is only one session opened.

Attach and detach from a session

Suppose we are attached to a Tmux session as the one we opened in the previous example. Now, from a window, we launch a long running command, than, while it is running, we want to detach from the session. In this case all we have to do is to press <prefix> followed by the d key. Tmux will be closed, and we will will be notified of the detach with a message:

[detached (from session 0)]



When we detached from a session, the session remains alive, and the processes we launched from it, keep running in the background. When it is time to re-attach to a session, we run the following command:

$ tmux attach -t 0

Where the argument passed to the -t option (0 in this case) is the session id or name.

Closing a session

A session is automatically terminated when all its windows are closed, but it can also be closed explicitly by entering command mode and running:

:kill-session

If we are already detached from the session, instead, we can kill it by running the following command:

$ tmux kill-session -t <session-id>

Managing windows

When we first launch Tmux, there is only one window open. Creating a new one, however, is pretty easy: all we have to do is to press <prefix> followed by c character. The name of the new window is reported in the status bar:

The new window reported in Tmux status bar
The new window reported in Tmux status bar

The star (*) near a window name is used to identify the one that is currently in use.

Renaming a window

At the moment the name of both windows is just “bash”: that is because it is name of the shell we are using. We may want to use a more meaningful name for a window; in order to do that, once again we press <prefix> this time followed by , (comma). The status bar will change color, and we will be able to set the new name for the window:

Renaming a Tmux window
Renaming a Tmux window

Switching windows

To switch between opened windows, as usual, first we need to issue the <prefix> combination, than, we can press p to switch to the previous window in the list or n to switch to the next. Alternatively we can press w to obtain a list of the available windows. We can than select the one we want to switch to and press enter:

Tmux windows list
Tmux windows list

Killing a window

Finally, to kill a window we can use the <prefix> combination followed by the & character. We will be prompted to confirm we want to perform the operation:

Killing a Tmux window
Killing a Tmux window

So to, summarize:

Tmux windows keys combinations
Action Keys combinations
 Create window  <prefix> c
 Rename window  <prefix> ,
 Switch to previous window <prefix> p
 Switch to next window <prefix> n
 Obtain navigable windows list <prefix> w
Kill a window <prefix> &

Managing panes

Every window in Tmux can be splitted in multiples sections, each one allowing us to use a pseudo-terminal. This sections are called “panes” in the Tmux terminology. To split  a window pane vertically we press <prefix> followed by the % sign:

Splitting a window in Tmux
Splitting a window in Tmux

To split a pane horizontally, instead, we use the " key:

Splitting horizontally
Splitting horizontally

Just like sessions and windows, each existing pane is identified by a number. To visualize the numbers associated to the panes we use  <prefix> followed by the q key:

Identifying panes
Identifying panes



Once the numbers are displayed, we can press it on our keyboard to move to the respective pane. The existing panes can be moved to the right and to the left by using <prefix> followed by the { and } keys respectively, and their layout can be switched by using the spacebar key, instead.

Here is a quick recap of the panes shortcuts:

Tmux panes keys combinations
Action Keys combinations
Spit vertically <prefix> %
Split horizontally <prefix> “
Identifying panes <prefix> q
Move pane to the left <prefix> {
Move pane to the right <prefix> }
Switch panes layout <prefix> spacebar

Closing thoughts

In this tutorial we learned the basics of Tmux. We saw what are the benefits of using the application when connecting to remote machines via ssh, and we saw how to manage sessions, windows and panes.



Comments and Discussions
Linux Forum