gray and black Asus laptop turned on
Mon Dec 19

Tmux Tutorial for Beginners

In managing a server using a terminal, it’s not uncommon for us to feel overwhelmed by the shell limitations as we’re only presented with a single terminal window. You might think that it would be more convenient to use multiple terminal windows at the same time so that we can run various applications in at once. Well… This is when tmux may come in handy.

What is tmux?

Tmux is a terminal multiplexer that means this application provides the user with multiple session based pseudo-terminal. Tmux has some useful features such as detach and reattach session, split window, multiple windows and some other features you might find useful.

Why use tmux?

That being said, we think the most prominent feature of tmux is session sharing. This feature allows multiple computers to connect to the same session at once. So, why do I need this?

Let’s say you are doing some administrative task on your server remotely. You are editing a file with nano or vim. Suddenly your network goes down, and you might have guessed, you will lose the file and everything you have done in that session. By using tmux, such occurrences can be prevented with this.

To see what we mean, simply use two terminals separately, side by side. Edit any file with nano (for example, nano test.txt) and keep it open. Using the second terminal, do the same to the same file. On the second terminal, you will get a warning telling you that the file is being edited by the user and it will ask you whether you want to open it. It happens because we try to edit the same file with two different sessions.

Now, let’s do this with tmux. On the first terminal, type tmux then type the same command as above nano test.txt. Using the second terminal, you can attach to the same terminal by simply using tmux a.

Instead of using another session, it will use the last session created by tmux. If you exit the nano editor using ctrl + x , you can see how the first terminal will follow the second terminal simultaneously. Thereby, it is possible for more than one computer to connect to the same session at a time.

How to install tmux?

First thing first, tmux is not likely to be pre-installed in your Linux distributions. To check it, you can use which tmux. The command is basically telling you the path of the application. If it returns nothing, it means it is not installed in your system.

If you are using Ubuntu or Linux mint or any Debian-based distros, you can use the command sudo apt install tmux. To make sure, you have installed the application, you should now have an output when using which tmux.

Other distributions may have different commands to install due to different packages used, like sudo dnf install tmux (Fedora), sudo yum install tmux (CentOS), or sudo pacman -S install tmux (Arch Linux).

How to use tmux?

You can start tmux by using the tmux command. In order to give any command or access features offered by tmux, you need to initiate it with ctrl + b afterwards. Before jumping right into the sessions, which are considerably the most important aspect of tmux, it would be better for you to know the concept of panes and windows in the first place.

Panes

Create New Pane & Navigate

Pane is a section where you can run any command or script. This is different from what we know as windows. To split the current terminal window into two different panes horizontally, use ctrl + b + “. To split it vertically usectrl + b + %. To close the active terminal you can simply use exit command, or simply use ctrl + d. To navigate between each pane, use **ctrl

  • b + ↑↓←→**.

Windows

Managing Windows

Occasionally, you may need new windows rather than new panes as it can be quite confusing to have every process running on the same window. You can create a new window by using ctrl + b + c. At the bottom left of the window, you can see something like 1:bash* which indicates a new window has been created. The asterisk (*) shows you where you are currently on. Switching between windows can be done using ctrl + b + n and you can check the asterisk moving around while you are pressing it.

Still at the bottom left of the window, you can see the number increments as you make more and more windows. This is basically an index you can choose to directly access the window you want. To do that, you can use ctrl + b + 09. Alternatively, you can also use ctrl + b + w to list all available windows and jump right into it.

Renaming Windows

When managing the windows, it can be somehow confusing having each window with the same name. To tackle this problem, tmux has the feature to rename the existing windows. It is pretty easy to do, just use ctrl + b + ,. After that, you can see your notifications bar change color and you might notice the bar changed to (rename-window). Here you can set the name you want for the current window.

Sessions

Creating New Session

If you have read the reason for using tmux above, you probably already have an idea about the session. Session is the time period when the user interacts with the application. To start a new one, you can type tmux in the terminal. For some reasons, you may also want to set the name for the session you are about to create. To do that you can type tmux new -t <session_name>.

Detach Session

After you have entered the current session, you can run any process, for example htop. You can then detach the session by using ctrl + b + d. The shortcut will redirect you to the default terminal session with the output message like [detached (from session 0)].

Attach Session

By detaching it, you are actually telling tmux to keep the running process in the background. The process is accessible whenever you want by reattaching into it. To reattach to the last session, you can use tmux a. If you have multiple tmux windows running at the same time and you want to access a specific session, you can specify the name of the window by using tmux a -t <session_name_or_index> .

List sessions

A single session may contain a lot of windows. To list the existing sessions from the default terminal, type tmux ls. For example, you may get something like 0: 12 windows (created Mon Dec 19 15:06:03 2022) which means there is a single session at index 0 with twelve windows in it. From the inside of the session, you can also list the sessions by using ctrl + b + s.

Rename Session

Within a tmux session, you can also rename the session using ctrl + b + $. Just like renaming the window, the bar will change color and the bar label turns into (rename-session). Then, you can set the session’s name.

Tmux cheatsheet

By simply doing some research on the web, you should find a complete tmux cheatsheet. You can actually refer to that. Using this cheatsheet, we try to simplify things so you can get a more concise overview of the most frequently used commands concerning tmux.

EntityPrefixCommandDescription
SessiontmuxCreate new session
new -t <session_name>Create new session with name
lsList existing session
aAttach to last session
a -t <session_name_or index>Attach to particular session or index
Shell CommandEntityPrefixKeyDescription
------------
Panesctrl + bSplit horizontally
%Split vertically
↑↓←→Navigate between panes
Windowsctrl + bcCreate new window
nSwitch to next window
0 ~ 9Switch to specific window
wList all existing windows
,Rename window
Sessionsctrl + bdDetach session
sList existing sessions
$Rename session

Tmux shortcut

Summary

Alternatively, there are many more terminal multiplexers out there which are also open source like GNU Screen, Byobu, and mtm. However, tmux is still one of the great tools you can try to help you preserve sessions while doing some administrative tasks. That being said, this tool is very useful, especially for those who primarily work with remote machines over SSH.