How to Use tmux on Linux (and Why It’s Better Than Screen)

A stylized Linux terminal on a laptop with shell sessions in the background.fatmawati achmad zaenuri / Shutterstock

The Linux tmux command is a terminal multiplexer, as screen. Its defenders are numerous and vocal, so we decided to compare the two. Is Tmux Really Better, or Is It Just Prefering What You Know?

tmux vs screen

The tmux and GNU screen commands are terminal multiplexers. They allow you to have multiple windows in a single terminal window and jump between them. A window can be divided into panes, each of which gives you an independent command line.

You can also detach a session and it becomes a headless entity running in the background – you can even close the terminal window that launched it. When you are ready, you can open a new terminal window and attach the session that is still running. You can also do it on an SSH connection.

You can detach a session from a computer, go home, and connect to the remote computer. Once reconnected, you can reattach the background session and use it again interactively.

What is the screen command?

The screen control is also a terminal multiplexer, and it is full of options. To know all about what you can do with it, see our detailed article.

This time we will focus on tmux. As we go along, we will mention how the screen manages the same functionality or function.

Only one thing bothered us about the screen. We’ll cover that when we get there, and see if tmux is better.

RELATED: How to use the Linux screen command

Install tmux

While screen is generally installed by default on popular Linux distributions, tmux is not. To install tmux on Ubuntu, type the following:

sudo apt-get install tmux

sudo apt-get installs tmux in a terminal window.

On Manjaro, you can use pacman:

sudo pacman -Sy tmux

sudo pacman -Sy tmux in a terminal window.

On Fedora 31, tmux is already installed.

Start a tmux session

To start tmux, type it and press Enter:

tmux

tmux in a terminal window.

The terminal window displays a status bar when you are in a tmux session.

A new tmux session in a terminal window.

The right side of the status bar displays the host name, along with the time and date. The left side displays the following session information:

[0]: This is the name of the session. By default, they are numbered, starting with zero. We explain below how you can give meaningful names to sessions.
0: bash *: The 0 indicates that this is the first window of this session. The only process running in this session is bash. If you are running a program, its name will appear here. The asterisk (*) means that this is the window you are looking at. Each time you create a new window in a tmux session, its window number and the name of the program running there are added to the status bar.

The screen command does not give you a status bar. You have to fly blind and rely on your intelligence to know what’s going on, which takes a little bit of practice.

On the positive side, you will not lose a row of terminal windows. Of course, you normally extend your terminal window so that using a terminal multiplexer is worth it. In this case, losing a row for the status bar is not really a problem. Here we left the terminal window images at the default size so you can see the information.

Commands are given to tmux using keys, and there are two parts. First, you press Ctrl + B to grab the attention of tmux. You then quickly press the following key to send a totmux command. Commands are given by pressing letters, numbers, punctuation marks or arrow keys.

It’s the same on the screen, except that you press Ctrl + A to get his attention.

To close the window, press Ctrl + B, then quickly press X. The status bar turns orange. You are then asked to confirm that you want to delete the window.

Press Y to close the window or N if you change your mind. You don’t need to press Enter afterwards; Y or N is enough to save your choice.

tmux session with an orange status bar and close this prompt window yes or no, in a terminal window.

If you press Y, the window closes. Since this is the only window for this session, the session is over.

Command prompt after closing a tmux session in a terminal window

The tmux session is closed and you return to the command line from which you started tmux. You will see “[exited]»In the terminal window.

This may seem like an obvious statement, but it is a confirmation that you have logged out and have not left it detached and running. We will discuss the secondment sessions below.

Starting a named tmux session

If you regularly start several tmux sessions, you will quickly appreciate the functionality of giving each one a meaningful name. You can also name sessions on the screen, but they don’t appear anywhere in session windows.

To start tmux with a session name, use the new command (new session) and the -s option (session name). Our session will be called “geek-1”, so we type the following:

tmux new -s geek-1

tmux new -s geek-1 in a terminal window.

When the tmux session loads, “geek-1” is displayed as the first entry in the status bar, on the far left.

Add more windows

To create a new window in the current session, press Ctrl + B, then press C. You will get an empty terminal window in the current session. So let’s have something running in this new window, let’s start the dmesg command with the -w option (follow):

dmesg -w

dmesg -w in a terminal window.

We now have two windows in the session; one is at the top, and the other is dmesg. However, we can only see one at a time (more on this in a moment).

dmesg running in window two of a tmux session, in a terminal window.

Take a look at the left side of the status bar. We are still in the tmux “geek-1” session. In window zero, top is running and in window one, dmesg is running. The asterisk (*) after dmesg tells us which window is visible.

To move from one window to another, press Ctrl + B, then one of the following keys:

NOT: Display the following window.
P: Displays the previous window.
0 to 9: Display a window numbered from 0 to 9.

You can also choose a window from a list. If you press Ctrl + B and then W, a list of windows appears.

List of tmux windows displayed in a terminal window.

To move the amber highlight bar, press the up or down arrow, Home or End. The lower part of the screen displays a preview of the content in the highlighted window.

Press Enter to move to the highlighted window or Esc to exit the list of windows without toggling.

Detach and attach sessions

If you press Ctrl + B and then D, you will detach the session. It will continue to run in the background, but you will not be able to see or interact with it.

We started at the top of the session, so we have an ongoing process to demonstrate. Then we press Ctrl + B, then press D. The session disappears and becomes a background session.

tmux message following the disconnection of a session, in a terminal window.

We return to the original terminal window. There is a tmux message telling us that the session is detached. It also reminds us of the name we gave to the session. It’s handy because that’s what we use to attach to a background session and then restore it to an interactive session.

To attach a detached session, we will use the self-explanatory attach-session command with the -t (target session) option. We will also provide the name of the session we wish to recall.

We type the following:

tmux attach-session -t geek-1

tmux attach-session -t geek-1 in a terminal window.

Our session returns and becomes a visible and interactive session again.

A tmux session restored in a terminal window.

Any long or continuous processes that you launched before detaching the session will always run in the background (unless they are finished) when you attach the session.

the screen can do it, but not as intuitively.

Management of several sessions

Let’s open another terminal window and start a new tmux session called “geek-2”:

tmux new -s geek-2

tmux new -s geek-2 in a terminal window.

In this session, we will start dmesg:

dmesg -w

dmesg -w in a terminal window.

Now we have our original “geek-1” tmux session and a new one called “geek-2”.

tmux geek-2 session running dmesg in a terminal window.

The status bar shows us that this session is called “geek-2”, and it has a window that runs dmesg.

If we press Ctrl + B, then D, we dissociate this session.

Tmux session detached geek-2 in a widnow terminal.

Back in the tmux “geek-1” session, we press Ctrl + B, then S to see a list of tmux sessions.

list of tmux sessions displayed in a terminal window.

To be clear, here is a list of sessions. The similar display we saw earlier was a list of windows in one session.

You can move the amber highlight bar by pressing the up and down arrows, Home and End. The lower section displays a preview of the highlighted session content.

list of tmux sessions displayed in a terminal window.

If you press the right arrow, the highlighted session windows appear.

List of tmux sessions with window details displayed in a terminal window.

Press Enter to move to the highlighted session or window or Esc to exit the session list without modifying the sessions. If you select a new session, your current session will be detached and the one you have selected is joined.

We detached the “geek-2” session before doing so. However, you can do this with sessions that are still attached to their original terminal windows. When you do, any screen changes will appear simultaneously in both tmux sessions.

The screen command can also do so via a similar set of commands.

Work with window panels

If you press Ctrl + B, then double the quotation marks (“”), you divide the window horizontally into two panes.

tmux session with horizontal panes in a terminal window.

This only affects the current window; the other participants in the session will not be modified. We used the tmux ls command in the upper pane to list the windows for this session. There are two, and the status line tells us that we are in window one. If we jump to the zero window by pressing Ctrl + B, then 0 (zero), we see that it is exactly as we left it.

These are two independent command lines, not two views in a window; they are distinct and separate shells. We can show this by running a different command in each pane.

We type the following:

uname -a

ls -hl

To move from one pane to another, press Ctrl + B, then the Up, Down, Left or Right arrows.

Two different commands in two panes in a tmux session in a terminal window.

If you press Ctrl + B, then the percentage sign (%), it divides the current pane vertically.

tmux session with vertical and horizontal split panes in a terminal window.

Press Ctrl + B, then Q to make tmux briefly flash the number of each pane.

tmux displaying the shutter numbers in a terminal window.

These numbers are used in tmux prompts and messages. Press Ctrl + B, then X to close the current pane. The status bar turns orange and you are asked to confirm that you want to close this pane number. Press Y to remove the pane or N to leave it as it is.

tmux asking you to delete a pane in a terminal window.

If you press Y, the pane is deleted.

tmux with two horizontal panes in a terminal window.

The screen control also has panes, but, again, they are less intuitive to use. What annoys us about the screen is that if you detach a session with panes, they disappear when you re-attach that session. It ages very quickly.

A Ctrl + B Cheat Sheet

We have included a cheat sheet of the various commands that you can use in tmux below.

Session commands

S: List of sessions.
$: Rename the current session.
RE: Detach the current session.
Ctrl + B, then ?: Display the help page in tmux.

Window controls

VS: Create a new window.
,:: Rename the current window.
W: List the windows.
NOT: Go to the next window.
P: Go to the previous window.
0 to 9: Move to the specified window number.

Shutter controls

%: Create a horizontal division.
“: Create a vertical separation.
H or left arrow: Go to the left pane.
I or right arrow: Go to the right pane.
J or down arrow: Go to the pane below.
K or up arrow: Go to the pane above.
Q: Briefly display the shutter numbers.
O: Move through the panes in order. Each press takes you to the next, until you cycle through them all.
}: Swap the position of the current pane with the next one.
{: Swap the position of the current pane with the previous one.
X: Close the current pane.

How they compare

In terms of functionality, screen and tmux work similarly and offer the same main functionality. It’s the way you access these features that is significantly different. tmux offers smoother and more comfortable ways to access different functions. But that’s not the only difference.

The ability to rename sessions and windows in tmux is clear, and the fact that it keeps panes when you attach a session changes that.

The screen, however, completely loses the shutters when you detach and reattach a session. It’s almost annoying enough to keep you from coming off in the first place.

There is so much more to tmux, including its incredibly flexible scripting capabilities. You have to check it out.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.