Tmux stands for Terminal Multiplexer.

Why Tmux

A program that is currently running on the computer is a process. To run more than one process with Linux, you would need more than one terminal window: one terminal window per process. These windows will overlap, with some almost covering others. To attend to a process, you have to click the window to bring it to the forefront. When it is at the forefront, you can then see some results or input data. This is multitasking: running more than one process simultaneously; in this case, with different windows. It is multitasking without the use of Tmux. Tmux offers multiplexing as a better substitute – see below.

Each window uses a shell such as Bash. Program commands have to be typed into a shell before they are run.

A session is a temporary interchanging of messages between a user and his computer or between the user’s computer and a remote computer through a physical network. A process may involve such interchanging of messages. Working with one window is one session. Working with many windows in one duration is still one session.

Using the terminal window in Linux for multitasking, as demonstrated above, can become cumbersome when the number of processes is more than three and the processes are long.

Multitasking can be done in one terminal window, but with the following drawbacks:

If you start a process with one terminal window and want to start another one, you would have to pause the first one first. When you pause a process, the process halts, and it is not doing anything. You can then start the next process. If you finish the next process soon enough, then you can resume the first process, still in the same window. That looks like terminal multiplexing, but it is not. With terminal multiplexing, the processes should run at the same time.

More than one process can be run in one terminal window using an asynchronous scheme. However, different processes will be sending results to the same window, and the results displayed will be inconsistent. Also, as you input data, they will go to different processes. This scheme can work only when you have few processes, and you know all their results, to be able to know what input to send and when. That situation of few processes is unlikely to occur.

With a terminal window, you can start a process on a remote computer, and then the physical connection fails. When the connection is restored, the results will be unreliable.

Another drawback with conventional terminal windows is that communication cannot take place between the windows when more than one window is opened.

Tmux is a software program that solves these problems. This article explains how Tmux solves these problems and discusses some of Tmux additional features.

Article Content

The solution offered by Tmux

As indicated above, the terminal window has limitations for multitasking. Tmux overcomes these limitations. Tmux replaces the terminal window with its own window while it is active. Tmux can have more than one window. The added windows superimpose on one another. Switching between these superimposed windows is possible. And so, results are not mingled, inputs and results are not mingled. A Tmux window can be split into smaller windows. These smaller windows are called panes.

With the conventional terminal, if you are running a remote process and the connection fails, you may not be able to continue from where you left off when the connection is restored. That is, the results of the remote process of the remote computer would not be reliable. Tmux can solve that by detaching a window session and then reattaching later.

In each window, the shell (e.g., Bash) operates to handle the commands.

Installing Tmux in Linux

Maybe Tmux is already installed on your computer. So, you have to check if it is installed first in order not to reinstall. At the terminal command prompt, type

and press Enter.

If it is not installed, it will tell you. If it is installed, you will have feedback like,

usage: tmux [-2CluvV] [-c shell-command] [-f file] [-L socket-name]

[-S socket-path] [command [flags]]

The commands in this tutorial have been tested with Ubuntu, and they have operated well. So if you are currently using the Ubuntu operating system, you can be testing the commands as you read along.

Installing Tmux in Ubuntu and Debian

To install in any of these operating systems, type

and press Enter. If it asks you for your password, type your password and press Enter.

Installing Tmux in CentOS and Fedora

To install in CentOS or Fedora, type

and press Enter. Answer any question that is asked.

Starting Tmux

Tmux is a utility that has to be started before it can be used. When you open the terminal window, Tmux is not running. It can be started and stopped. When it is started, it takes control of the terminal window and superimposes its own windows. When it is stopped, you are back to your terminal window. To start Tmux, type

(all characters in lowercase) and press Enter. You will have a new window. Tmux would take over the new window. The new window looks like the terminal window but with a status bar at the bottom. In this situation, the status bar indicates that you are at Tmux window 0, shown as 0:bash (if you are using the Bash shell).

The default key combination for Tmux is:

ctrl b

You press the control key and the lowercase b character, and as soon as you release the keys, you press some other character to affect. To display the list of Tmux commands, type

ctrl b ?

That is, press the control key and the character b at once, and as soon as you release the two keys, press shift and the? Key (if you are using the English keyboard). You should see the list of commands. The list is quite long. The cursor would be at the top. You can use the down arrow key to scroll down the list.

To go out of the listing display, press

q

and you will be back at the command prompt.

Exiting the Current Window

To exit the current window, type

and press Enter. You will see

Indicating that you have exited from the Tmux application (if only one Tmux window were opened). You should be back at the normal terminal window if you had just one Tmux window. If you execute the command “exit” again, the normal terminal window will close.

Creating More than One Tmux Window

In the above section, only one window was created. When you start Tmux, one window (0:bash) is created. You can create more. If you have not opened the terminal window, open it and start Tmux by typing,

and pressing Enter. The terminal window should transform into a Tmux window, the first window of Tmux. Create three more screen windows by typing the following commands and pressing Enter, where c is in lowercase (and needs no shift key):

ctrl b c


ctrl b c


ctrl b c

For each of the commands, you press the control key and the b (in lowercase) key; release these keys and then just press the c key. It may take some short experience to get it right. If you have successfully executed the three commands, and if you are using the Bash shell, then in the status bar, you should see

[0] 0:bash  1:bash  2:bash- 3:bash*

[0] means session 0. The rest of the line means that you have opened four windows, which are  0:bash, 1:bash, 2:bash, and 3:bash. Counting of Tmux windows begins from 0.

To go to any other window, execute the following command:

ctrl b s

You should see a different presentation, a listing of windows presentations. You can highlight the label of the window you want using the left-arrow key or the right-arrow key on the keyboard. Use the right-arrow key to choose window number 1, which is actually the second window, and press Enter.

If you had typed any command in the second window, you should see the commands in the window. In the status bar, you should also see 1:bash*. The asterisk (*) indicates the window you are in, and in this case, it is window number 1, which is actually the second window.

You can continue to type whatever commands you want into this second window. You can change to some other window, similarly.

Switching to Other Windows

You can switch to another window by using the above method. There is another method: To switch to window number 3 (the fourth window), execute

ctrl b 3

You should now have,

With the asterisk in the status bar, which means you are in the fourth window. So, to go to a new window, you can do “ctrl b index”, where an index is an ordinal number minus one.

Going to the Next and Previous Windows

To go to the next window, do

ctrl b n

If you were at the second window, you would arrive at the third window. If you were at the last window, you would arrive at the first window. To go to the previous window, do

ctrl b p

If you were at the first window, you would arrive at the last window.

Listing all Windows

To list all windows, do

ctrl b w

In the list that appears, you will be able to choose the next window you want, with the keyboard up-arrow or down-arrow. After highlighting, press the Enter key to enter the chosen window.

Exiting a Window

To exit a window type,

and press Enter. You can exit all the windows by repeatedly executing the exit command. If you do that the number of times, corresponding to the number of Tmux windows, you will arrive at the terminal window, which was taken over by the Tmux utility; and you will see:

If you do the exit command again, you will close the terminal window.

Killing the Current Window

When you kill a window, you kill it with its process. To kill a window, do

ctrl b &

A message similar to the one below will appear, asking you if you really want to kill the window.

If you really want to kill the window, press y, and the window will be killed if you do not want to kill the window, press n.

Attaching and Detaching a Tmux Session

A Tmux window is not only used to do work on the local personal computer. It can be used to carry out a process on a remote computer. When the window is doing work for the local personal computer or is running a process in a remote computer, the window is said to be attached. In the case of a remote process, if you detached the window and the connection breaks, the result of the process at the remote computer will still be reliable. Of course, the process in the remote computer can halt –  that is not a problem.

When the connection is restored, you can continue from where you left off by reattaching the window.

You can detach from the current session by executing:

ctrl b d

If the command is successful, you will go out of the Tmux windows, and into the terminal window, with its command prompt. Before the prompt, and above it, you should see something like:

[detached (from session 0)]

Now that you are at the terminal window, to display the list of sessions, type

and press Enter. The first column of the listing gives the name of the session. In the case of this article, there should be only one session with the name 0. So to reattach to the session, type

and press Enter.

Killing a Session

From the terminal window, you can kill a session. You should be detached from a session before killing it. The syntax to kill a session from the terminal window is:

tmux kill-session -t session name

Splitting Windows

Splitting Vertically

To split the current window vertically, do

ctrl b %

If you are using the English keyboard, you will have to press the shift key and the key that has % to obtain %. If you did that, then you would have a vertical line in the middle of the window.

To go to the next pane, do

ctrl b o

Where o is not zero, it is the alphabet character, o. The movement is cyclic. Under this condition, it is one window that has been split into panes. The other windows do not have panes. In a window with panes, commands can be typed in each of the panes.

Killing a Pane

To kill a pane with its commands, while the cursor is in the pane, do

ctrl b x

Where x is lowercase x, answer any question that is asked. The pane dies with its commands. The commands do not join the commands of any other pane.

Splitting Horizontally

To split the current window horizontally, do

ctrl b “

If you are using the English keyboard, you will have to press the shift key and the key that has “, to obtain “.

To go to the next pane, do

ctrl b o

Where o is not zero, it is the alphabet character, o. The movement is cyclic.

You kill a horizontal pane in the same way that you kill a vertical pane.

Conclusion

Tmux is an application that enables a Linux user to have his session of terminal windows into one window. Each window can be split into panes. The default key combination for Tmux is ctrl b. After that, another character has to be pressed to obtain an effect. With all its features, Tmux is a Windows Manager Terminal Multiplexer. Tmux has a rival called Screen. Multiplexing here means having all its windows for its processes in one window frame.

Chrys

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/Chrysanthus-150×150.jpeg" height="112" src="data:image/svg xml,” width=”112″>

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.