How to use tmux on Linux (and why is it better than the screen)

Contents

A stylized Linux terminal on a laptop with shell sessions in the background.

El Linux tmux The command is a terminal multiplexer, What screen. Its defenders are many and vocal, so we decided to compare the two. It is tmux truly better, Or is it just a case of preferring what you know?

tmux in front of screen

Both of them tmux y GNU screen commands sound terminal multiplexores. They allow you to have multiple windows within a single terminal window and jump from one to another. A window can be divided into panels, each of which gives you a separate command line.

You can also disconnect a session and it becomes a headless entity that runs in the background; you can even close the terminal window that started it. When you are ready, you can open a new terminal window and reconnect the session that is still running. You can also do this through an SSH connection.

You can disconnect a session on a computer, go home and log into the remote computer. When it reconnects, you can reconnect the session in the background and use it interactively again.

What is the screen command?

the screen The command is also a terminal multiplexer and is packed with alternatives. To know everything you can do with it, see our detailed post.

This time, we are going to focus on tmux. As we go, we will mention how screen handles the same feature or function.

Only one thing bothered us screen. We'll cover that when we get to that, and we will see if tmux it goes better.

RELATED: How to use the Linux screen command

Tmux installation

Weather screen generally installed by default on popular Linux distributions, tmux it is not. Install tmux in Ubuntu, write the following:

sudo apt-get install tmux

In Manjaro you can use pacman:

sudo pacman -Sy tmux

In Fedora 31, tmux is already installed.

Start a tmux session

To start tmux, just type it and hit Enter:

tmux

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

The right side of the status bar shows the hostname, the time and date. The left side shows the next information related to the session:

  • [0]: This is the name of the session. By default, they are numbered, starting with zero. Next, we cover how you can give meaningful names to sessions.
  • 0: knock *: The 0 indicates that this is the first window of this session. The only procedure that runs in this session is bash. If you run a program, your name will appear here. The asterisk tmux means this is the window you are looking at. Every time you create a new window in a

session, your window number and the name of the program running in it are added to the status bar. screen the The command doesn't give you a status bar by default. You have to fly blind and trust your wits to know what's going on, which requires a bit of practice. (Unless youset your own status bar

.)

On the bright side, you won't lose a single line of space in the terminal window. Of course, I would regularly expand your terminal window to make it worthwhile to use a terminal multiplexer. Then, missing a line for the status bar is not a big deal. We have left the images of the terminal windows here at the default size so you can see the information. tmux Commands are given to tmuxusing keystrokes, and there are two parts to this. First, presiona Ctrl + B to gettmuxattention. Next, quickly press the next key to send a command to

. Commands are given by pressing letters, numbers, punctuation marks or arrow keys. screenIt is the same in

, except you press Ctrl + A to get your attention.

To close the window, presione Ctrl + B, and then quickly press X. Status bar turns amber. You will then be asked to confirm that you want to close the window.

Press Y to close the window or N if you change your mind. You don't need to press Enter after; 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 ends. tmux the tmuxthe session closes and returns to the command line from which you started[exited]. You'll see “

"In the terminal window.

This may seem obvious, but it is a confirmation that you have closed the session and have not left it disconnected and running. We will discuss the breakup sessions below..

Start a named tmux session tmux If you start regularly screensessions, you will quickly appreciate the functionality of giving each of them a meaningful name. You can name sessions in

, what's more, but they are not displayed anywhere in the session windows. tmux To start new with a session name, use el -s (new session) command, and the

tmux new -s geek-1

(session name) option. Our session will be called “geek-1”, so we write the following: tmux When the

session load,

A tmux session with the name “geek-1” on the left side of the status bar.

Add more windows dmesg To create a new window in the current session, presione Ctrl + B then C. You will get a blank terminal window in the current session. Then we will have something running in this new window, let's start the command -w with the

dmesg -w

(continue) option: topNow we have two windows in the session; one is running dmesg, and the other

. Despite this, we can only see one at the same time (more on that in a moment). tmux Take a look at the left side of the status bar. We are still in the “geek-1” dmesg session. At window zero, top is running, and in the window one, dmesg Is running. The asterisk

after

  • tells us which window is visible.To jump between windows, presione Ctrl + B, and then one of the following keys: North
  • : Show the next window.
  • PAG: Show the previous window.

0 a 9:

Display a numbered window of the 0 al 9.

You can also select 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, Start or End. The bottom section of the screen shows a preview of the content in the highlighted window.

Press Enter to go to the highlighted window or Esc to exit the window list without changing.

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

We have started tmux in the session, so we have a running procedure to prove it. After, we press Ctrl + B, and then D. The session disappears and becomes a background session.

We return to the original terminal window. There is a message from attach-session telling us that the session is separated. It also reminds us of the name we gave to the session. This is useful because that is what we use to attach to a background session and then restore it to an interactive one.. -t To attach a separate session, we will use the self-explanatory

command with the

tmux attach-session -t geek-1

(target session) option. We will also provide the name of the session that we want to remember.

We write the following:

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

Any long-running or continuous procedures that you started before disconnecting the session will continue to run in the background (unless they are done) when I attach the session.

can do this, but not so intuitively. tmux Handling multiple sessions

tmux new -s geek-2

Let's open another terminal window and start a new one dmesgsession called “geek-2”:

dmesg -w

In that session, we will begin tmux :

Now, we have our “geek-1” original dmesgsession and a new call “geek-2”.

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

.

If we press Ctrl + B, and then D, we separate that session. tmux Geek-2 separate tmux session in a widnow terminal. tmux Back in the “geek-1”

session, we press Ctrl + B, and then Y to see a list of

sessions.

to be clear, this is a list of sessions. The equivalent screen we saw previously was a list of windows in a single session.

You can move the amber highlighted bar by pressing the up and down arrows, Start and end. The bottom section shows a preview of the content of the highlighted session.

If you press the right arrow, the windows of the highlighted session are displayed. tmux Press Enter to go to the highlighted session or window or Esc to exit the session list without changing sessions. If you select a new session, the current one is disconnected and the one you selected is attached.

We separate the session “geek-2” before doing this. Despite this, you can do this with sessions that are still connected to their original terminal windows. When i do, screen changes will appear simultaneously on both screen sessions.

the

The command can additionally do this through an equivalent set of commands.

Work with window panes tmux ls If you press Ctrl + B and then double quotes (“”), divides the window horizontally into two panels.

This only affects the current window; others in the session will not be changed. We have used the

command in the top pane to list the windows in this session. There are two, and the status line tells us that we are in window one. If we jump to window zero by pressing Ctrl + B, and then 0 (zero), we see that it is as we left it.

uname -a

ls -hl

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

We write the following:

To switch from one panel to another, presione Ctrl + B and then the up arrows, down, left or right.%) Two different commands on two panels in a tmux session in a widowed terminal.

If you press Ctrl + B and then the percent sign ( tmux splits current panel vertically.

Presione Ctrl + B and then Q to do tmuxbriefly display the number of each panel.

These numbers are used in indications and messages of

. Presione Ctrl + B and then X to close the current panel. The status bar turns amber and you are asked to confirm that you want to close that panel number. Press Y to remove the panel, or N to leave things as they are. screen If you press Y, the panel is removed. screen the

The command also has panels, but, again, are less intuitive to use. What bothers us

In other words, if you disconnect a paneled session, disappear when you reconnect that session. This ages very quickly. tmux A Ctrl + B Cheat Sheet

We have included a cheat sheet of the different commands you can use in

  • below. Session commands
  • S: List the sessions.
  • PS Rename the current session.
  • D: Detach the current session. tmuxCtrl + B, and then?:

Show help page in

  • . Window commands
  • C: Create a new window.
  • ,: Rename the current window.
  • W: List the windows.
  • NORTH: Go to the next window.
  • PAG: Go to the previous window.

0 a 9:

  • Go to the specified window number. Panel commands
  • %: Create a horizontal split.
  • “: Create a vertical split.
  • H or left arrow: Move to the left panel.
  • I or right arrow: Move to the right panel.
  • J or down arrow: Go to the panel below.
  • K or up arrow:Go to the panel above.
  • Q : Briefly display panel numbers.
  • O: Scroll through the panels in order. Each press takes you to the next, until it goes through all.
  • }: Swap the position of the current panel with the next.
  • {: Swap the position of the current panel with the previous one.

X:

Close current panel. screen How they compare tmux In terms of functionality, tmux and

both work in an equivalent way and offer the same main features. It is the way you access those functions that is markedly different.. tmux offers easier and more convenient ways to access the various functions. Despite this, that's not the only difference.

screenThe ability to rename sessions and windows in

is ordered, and the fact that you keep your dashboards when you reconnect a session is a game changer. tmux, on the other hand, absolutely lose panels when disconnecting and reconnecting a session. This is almost annoying enough to avoid separating in the first place..

There is so much more to

, including its incredibly flexible scripting capabilities. You owe it to yourself to check it out.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.