قالب وردپرس درنا توس
Home / Tips and Tricks / How to use tmux on Linux (and why it is better than the screen)

How to use tmux on Linux (and why it is better than the 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, such as the screen . ]. There are many advocates for it, so we decided to compare the two. Is tmux really better, or does it just prefer what you know?

tmux vs. screen

Both the tmux and the GNU screen ] commands are terminal multiplexers. It allows you to have multiple windows within one terminal window and 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 ̵

1; you can even close the terminal window that started it. Once done, you can open a new terminal window and reconfirm the ongoing session. You can also do this via an SSH connection.

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

What is the screen command?

The command screen is also a terminal multiplexer and is packed with options. Check out our in-depth article to learn more about everything you can do with it.

This time we're going to focus on tmux . As we continue, we will mention how screen handles the same function or function.

Only one thing irritated us with screen . We'll cover that when we get there and see if tmux does better.

RELATED: How to Install Linux & # 39; s Screen Command [19659008tegebruiken] ] Tmux

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

  sudo apt-get install tmux 

  sudo apt-get install 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.

Starting a tmux session

To start tmux just type it and press Enter:


  tmux in a terminal window.

The terminal window becomes 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 shows the host name, time and date. The left side shows the following session related information:

  • [0]: This is the session name. They are numbered by default, starting with zero. We discuss how to give meaningful names to the sessions below.
  • 0: bash *: The 0 indicates this is the first window in this session. The only process performed in this session is bash . If you are running a program, the name will be displayed here. The asterisk (*) means that this is the window you are looking at. Every time you create a new window in a tmux session, the window number and name of the program running in it are added to the status bar.

The command screen & # 39; t gives you a status bar. You have to fly blind and rely on your mind to know what's going on, which takes a little practice.

On the plus side, you don't lose a line of real estate in the terminal. Of course, you would normally expand your terminal window to make using a terminal multiplexer worthwhile. In that case, losing one line for the status bar isn't such a big deal. We have 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 it consists of two parts. First press Ctrl + B to get the attention of tmux . Then quickly press the next key to send a command to tmux . 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 his attention.

To close the window, press Ctrl + B, then quickly press X. The status bar turns orange. Then you will be asked to confirm that you want to kill the window.

Press Y to close the window or N if you change your mind. You do not need to press Enter afterwards; Y or N is sufficient to register your choice.

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

If you press Y, the window closes. Because this is the only window in this session, the session ends.

 Command Prompt after closing a tmux session in a terminal window

The tmux session is closed and you are returned to the command line from which you tmux launched. You will see & # 39; [exited] & # 39; in the terminal window.

It may seem obvious, but it is a confirmation that you have closed the session and not released and executed it. We discuss the posting sessions below.

Starting a Named tmux session

If you regularly start multiple tmux sessions, you will quickly appreciate the functionality of giving each one a meaningful name. You can also name sessions in the screen, but they are not displayed anywhere in the session windows.

To start tmux with a session name, use the new (new session) command and the option -s (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 session tmux is loaded, "geek-1" is displayed as the first item in the status bar on the far left.

 A tmux session called "geek-1" on the left side of the status bar.

Add more windows

Press Ctrl + B and then C to create a new window in the current session. You will get an empty terminal window in the current session. So let's run something in this new window, let's start the command dmesg with the option -w (follow):

  dmesg -w 

  dmesg -w in a terminal window.

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

 dmesg runs 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 session "geek-1" tmux . In window zero the top rotates and in window one runs dmesg . The asterisk (*) after dmesg tells us which window is visible.

To jump between windows, press Ctrl + B and then one of the following keys:

  • N : Display the following window.
  • P: Display the previous window.
  • 0 to 9: Display a window with the numbers 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.

 tmux window list displayed in a terminal window.

To move the orange highlight bar, press the Up or Down Arrows, Home or End. The bottom part 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 switching.

Unpin and Link Sessions

If you press Ctrl + B and then D, you unlink the session. It keeps running in the background, but you can't see or interact with it.

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

 tmux message after letting go of a session in a terminal window.

We return to the original terminal window. There is a message from tmux telling us that the session has been broken. It also reminds us of the name we gave to the session. This is useful because that's what we use to add to a background session and then restore to an interactive session.

To add a single session, we use the self-explanatory attach session . command with the option -t (target session). We also give the name of the session we want to retrieve.

We type the following:

  tmux attach-session -t geek-1 

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

Our session returns and becomes visible again , interactive session.

 A restored tmux session in a terminal window.

Any long running or continuous processes that you started before disconnecting the session will still run in the background (unless they are completed) when you add the session.

screen can do this, but not so intuitively.

Handle Multiple Sessions [19659005] Let's open another terminal window and start a new tmux session named “geek-2”:

  tmux new -s geek-2 

  tmux new -s geek-2 in a terminal

In that session we start dmesg :

  dmesg -w 

  dmesg -w in a terminal window. [19659006] Now we have our original "geek-1" tmux session and a new one called "geek-2".

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

The status bar shows that this session is called "geek-2" and there is one window with dmesg .

If we press Ctrl + B, then D, we release that session.

 Free standing tmux session geek-2 in a widnow terminal.

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

 list of tmux sessions displayed in a terminal window.

For the record, this is a list of sessions. The similar screen we saw earlier was a list of windows in one session.

You can move the orange highlight bar by pressing the Up and Down arrows, Home and End arrows. The bottom section shows an example of the contents of the highlighted session.

 list of tmux sessions displayed in a terminal window.

If you press the right arrow, the windows for the highlighted session

 tmux session list with window details are displayed in a terminal window.

Press Enter to go to the highlighted session or window, or Esc to leave the session list without changing the sessions. If you select a new session, your current one will be disconnected and the one you selected will be attached.

We disconnected the "geek-2" session before doing this. However, you can do this with sessions that are still linked to their original terminal windows. When you do that, all screen changes appear simultaneously in both sessions tmux .

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

Working with panels

If you press Ctrl + B and then double quotes (""), split the window horizontally into two panels.

 tmux session with horizontal panes in a terminal window.

This only applies to the current window; the others in the session are not changed. We used the tmux ls command in the top pane to display the windows in this session. There are two of them 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.

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

We type the following:

uname -a

ls -hl

To move from one panel to another, press Ctrl + B, then the up, down, left, or right arrow.

 Two different assignments in two panels in a tmux session in a terminal widow. [19659006] Pressing Ctrl + B followed by the percent sign (%) splits the current part vertically.

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

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

 tmux with panels in a terminal window. [19659006] These numbers are used in prompts and messages from tmux . Press Ctrl + B and then X to close the current panel. The status bar changes to orange and you are asked to confirm that you want to close that pane. Press Y to delete the panel, or N to leave things as they are.

 tmux asks to remove a pane in a terminal window.

Pressing Y removes the panel. [19659006]   tmux with two horizontal panes in a terminal window.

The command screen also has panels, but again, they are less intuitive to use. What annoys us about the screen is that if you disconnect a session with diamonds, they disappear when you reconfirm that session. This is getting old very quickly.

A Ctrl + B Cheat Sheet

We've added a cheat sheet of the different commands you can use in tmux below.

Session Assignments

  • S: List Sessions.
  • $: Rename current session.
  • D: Disconnect current session.
  • Ctrl + B, and then ?: Show help page in tmux .

Window Commands

  • C: Create a new window.
  • ,: Rename the current window.
  • W: Show the windows
  • N: Go to the next window.
  • P: Go to the previous window.
  • 0 to 9: Go to the specified window number.

Window Commands

  • %: Make a horizontal split.
  • ": Create a vertical split.
  • H or left arrow: Go to the panel on the left.
  • I or right arrow: Go to the panel on the right side.
  • J or Down Arrow: Go to the panel below.
  • K or Up Arrow: Go to the panel above.
  • Q : Briefly show the diamond numbers.
  • O: ] Move through the panels in sequence, each time you press, move to the next until you scroll through them all.
  • }: Swap the position of the current window with the following.
  • {: Swap the position of the current window with the previous one.
  • X: Close the current panel.

How they compare

Work in terms of functionality screen and tmux both in the same way and provide the same main functions. where you can access those features that are clearly different. tmux offers slicker, more comfortable ways to access the different functions. However, that is not the only difference.

The ability to rename sessions and windows in tmux is neat, and the fact that it keeps the panes when you reconfirm a session is a game changer.

The screen on the other hand, completely loses panels when you unpin and reconfirm a session. This is almost annoying enough to keep you from getting loose in the first place.

There is so much more to tmux including the incredibly flexible scripting options. You owe it to yourself to watch it.

Source link