The Linux tmux
command is a terminal multiplexer, like screen
. Its advocates are many and vocal, so we decided to compare the two. Is tmux
really better, or is it just a case of preferring what you know?
tmux vs. screen
Both the tmux
and GNU screen
commands are terminal multiplexers. They allow you to have multiple windows within a single terminal window, and to jump back and forth 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’re ready, you can open a new terminal window and reattach the still-running session. You can also do this over an SSH connection.
You can detach a session on one computer, go home, and log in to the remote computer. When reconnected, you can reattach the background session and use it interactively again.
What’s the screen Command?
The screen
command is also a terminal multiplexer, and it’s packed with options. For the lowdown on everything you can do with it, check out our in-depth article.
This time, we’re going to concentrate on tmux
. As we go along, we’ll mention how screen
handles the same feature or function.
Only one thing annoyed us about screen
. We’ll cover that when we get to it, and see if tmux
fares any better.
RELATED: How to Use Linux’s screen Command
Installing tmux
While screen
is generally installed by default on popular Linux distributions, tmux
isn’t. To install tmux
on Ubuntu, type the following:
sudo apt-get install tmux
On Manjaro you can use pacman
:
sudo pacman -Sy tmux
On Fedora 31, tmux
is already installed.
Starting a tmux Session
To start tmux
, just type it and hit Enter:
tmux
The terminal window will show a status bar when you’re in a tmux
session.
The right side of the status bar shows the hostname, and the time and date. The left side shows the following session-related information:
- [0]: This is the session name. By default, they’re numbered, starting with zero. We cover how you can give meaningful names to sessions below.
-
0:bash*: The 0 indicates this is the first window in this session. The only process running in this session is
bash
. If you run a program, its name will appear here. The asterisk (*) means this is the window you’re looking at. Each time you create a new window in atmux
session, its window number and the name of the program running in it are added to the status bar.
The screen
command doesn’t give you a status bar. You have to fly blind and rely on your wits to know what’s going on, which takes a bit of practice.
On the plus side, you won’t lose a line of terminal window real estate. Of course, you’d normally expand your terminal window to make using a terminal multiplexer worthwhile. In that case, the loss of one line for the status bar isn’t much of an issue. We’ve left the images of the terminal windows here at the default size so you can see the information.
Commands are given to tmux
using keystrokes, and there are two parts to this. First, you press Ctrl+B to get tmux
‘s attention. You then quickly press the next key to send a command totmux
. Commands are given by pressing letters, numbers, punctuation marks, or arrow keys.
It’s the same in screen
, except you press Ctrl+A to get its attention.
To close the window, press Ctrl+B, and then quickly hit X. The status bar turns amber. You’re then prompted to confirm you want to kill the window.
Press Y to close the window or N if you change your mind. You don’t have to press Enter afterward; Y or N is enough to register your choice.
If you press Y, the window closes. Because this is the only window in this session, the session is terminated.
The tmux
session is closed and you’re returned to the command line from which you launched tmux
. You’ll see “[exited]” in the terminal window.
This might seem like it’s stating the obvious, but it’s a confirmation you’ve closed the session and not left it detached and running. We’ll discuss detaching sessions below.
Starting a Named tmux Session
If you regularly start multiple tmux
sessions, you’ll quickly appreciate the functionality of giving each of them a meaningful name. You can name sessions in screen
, too, but they’re not displayed anywhere in the session windows.
To start tmux
with a session name, use the new
(new session) command, and the -s
(session name) option. Our session is going to be called “geek-1,” so we type the following:
tmux new -s geek-1
When the tmux
session loads, “geek-1” is displayed as the first entry in the status bar, at the far left.
Adding More Windows
To create a new window in the current session, press Ctrl+B, and then C. You’ll get a blank terminal window in the current session. So we’ll have something running in this new window, let’s start the dmesg
command with the -w
(follow) option:
dmesg -w
Now we have two windows in the session; one is running top
, and the other dmesg
. We can only see one at a time, though (more on that in a moment).
Take a look at the left side of the status bar. We’re still in the “geek-1” tmux
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 hop between windows, press Ctrl+B, and then one of the followings keys:
- N: Display the next window.
- P: Display the previous window.
- 0 to 9: Display a window numbered 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.
To move the amber highlight bar, press the Up or Down Arrows, Home, or End. The bottom section of the display shows a preview of the content in the highlighted window.
Press Enter to move to the highlighted window, or Esc to leave the window list without switching.
Detaching and Attaching Sessions
If you press Ctrl+B, and then D, you will detach the session. It will continue to run in the background, but you won’t be able to see or interact with it.
We’ve started top
in the session so we have a running process to demonstrate with. Then, we press Ctrl+B, and then D. The session disappears and becomes a background session.
We return to the original terminal window. There’s a message from tmux
telling us the session is detached. It also reminds us of the name we gave to the session. This is handy because that’s what we use to attach to a background session, and then restore it to an interactive one.
To attach a detached session, we’ll use the self-explanatory attach-session
command with the -t
(target session) option. We’ll also provide the name of the session we wish to recall.
We type the following:
tmux attach-session -t geek-1
Our session returns and becomes a visible, interactive session again.
Any long-running or continual processes you launched before detaching the session will still be running in the background (unless they’ve finished) when you attach the session.
screen
can do this, but not as intuitively.
Handling Multiple Sessions
Let’s open another terminal window, and start a new tmux
session called “geek-2”:
tmux new -s geek-2
In that session, we’ll start dmesg
:
dmesg -w
Now, we’ve got our original “geek-1” tmux
session, and a new one called “geek-2.”
The status bar shows us this session is called “geek-2”, and it has one window that running dmesg
.
If we press Ctrl+B, and then D, we detach that session.
Back in the “geek-1” tmux
session, we press Ctrl+B, and then S to see a list of tmux
sessions.
To be clear, this is a list of sessions. The similar display we saw earlier was a list of windows in a single session.
You can move the amber highlight bar by pressing the Up and Down Arrows, Home, and End. The bottom section displays a preview of the content in the highlighted session.
If you press the Right Arrow, the windows for the highlighted session are displayed.
Press Enter to move to the highlighted session or window or Esc to leave the session list without changing sessions. If you select a new session, your current one detaches, and the one you selected is attached.
We detached the “geek-2” session before we did this. 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 do this, too, via a similar set of commands.
Working with Window Panes
If you press Ctrl+B, and then double quotation marks (“”), you split the window horizontally into two panes.
This only affects the current window; the others in the session won’t be changed. We’ve used the tmux ls
command in the top pane to list the windows in this session. There are two, and the status line tells us we’re in window one. If we hop over to window zero by pressing Ctrl+B, and then 0 (zero), we see it is just as we left it.
These are two independent command lines, not two views in one 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, and then either the Up, Down, Left, or Right Arrow.
If you press Ctrl+B, and then the percentage sign (%)
it splits the current pane vertically.
Press Ctrl+B, and then Q to make tmux
briefly flash the number of each pane.
These numbers are used in prompts and messages from tmux
. Press Ctrl+B, and then X to close the current pane. The status bar changes to amber, and you’re prompted to confirm you want to close that pane number. Press Y to remove the pane, or N to leave things as they are.
If you press Y, the pane is removed.
The screen
command also has panes, but, again, they’re less intuitive to use. The thing that annoys us about screen
is if you detach a session with panes, they disappear when you reattach that session. This gets old very quickly.
A Ctrl+B Cheat Sheet
We’ve included a cheat sheet of the different commands you can use in tmux
below.
Session Commands
- S: List sessions.
- $: Rename current session.
- D: Detach current session.
-
Ctrl+B, and then ?: Display Help page in
tmux
.
Window Commands
- C: Create a new window.
- ,: Rename the current window.
- W: List the windows.
- N: Move to the next window.
- P: Move to the previous window.
- 0 to 9: Move to the window number specified.
Pane Commands
- %: Create a horizontal split.
- “: Create a vertical split.
- H or Left Arrow: Move to the pane on the left.
- I or Right Arrow: Move to the pane on the right.
- J or Down Arrow: Move to the pane below.
- K or Up Arrow: Move to the pane above.
- Q: Briefly show pane numbers.
- O: Move through panes in order. Each press takes you to the next, until you loop through all of them.
- }: Swap the position of the current pane with the next.
- {: Swap the position of the current pane with the previous.
- X: Close the current pane.
How They Compare
In terms of functionality, screen
and tmux
both perform similarly and offer the same main features. It’s the way you access those features that is markedly different. tmux
offers slicker, more comfortable ways to get to the various functions. However, that’s not the only difference.
The ability to rename sessions and windows in tmux
is neat, and the fact that it retains the panes when you reattach a session is a game changer.
screen
, on the other hand, completely loses panes when you detach and reattach a session. This is almost annoying enough to make you avoid detaching in the first place.
There’s so much more to tmux
, including its incredibly flexible scripting capabilities. You owe it to yourself to check it out.