How to Use Tmux + Cheat Sheet
Download Complete Linux Commands Cheat Sheet
In this tutorial, we will teach you how to use Tmux. It is an application that allows you to split window on terminal into multiple others. So, in a single window, we can have several instances of the terminal open, similar to GNU screen or Byobu.
Tmux is ideal for speeding up terminal tasks on your VPS, especially if you are a sysadmin, who needs to handle several terminals in one.
How to Install Tmux on Ubuntu or Debian
Tmux is a tool in the official Debian and Ubuntu repositories. That makes it incredibly easy to install. To do this, we will use APT, which is the package manager for Debian and Ubuntu. With this package manager, we will be able to install, uninstall, and update packages without worrying about dependencies. First, you’ll need to access your virtual private server through SSH. Check out our PuTTY tutorial if you’re having trouble.
Installing Tmux will require administrator privileges, so we suggest adding sudo to the command.
Run this command to install the utility:
sudo apt-get install tmux
Afterward, it is a good idea to confirm the installed version. We can do it with the following command:
tmux -V
Tmux is now correctly installed and ready to use.
Firsts Steps with Tmux
Tmux is an application that is based on sessions. That is, once you run the utility it opens a new session. In each session, there can be several terminals as Tmux is a terminal multiplexer.
So to start using Tmux, we need to connect to a new session. This is done with the command:
tmux
Once the session starts, we will see the same terminal as always, except for a green bar at the bottom. This bar indicates the active session, and that we are using Tmux. It is also possible to name the session. To do this, we can type the following command when creating one:
tmux new -s [session_name]
The most important utility of Tmux is that it allows different instances of terminals in a single window. In other words, in one session. Additionally, we will be able to access them quickly and easily from the keyboard.
To disconnect from a Tmux a session, we need to type the following command:
exit
Using the Prefixes to Control Tmux
Tmux is based on commands that perform specific tasks. However, in order to execute these commands, a prefix must first be used. The prefix tells Tmux that a command is going to be executed. By default, the prefix is CTRL+B.
So the correct way to structure commands in Tmux is:
<prefix> + Command
That is, we have to press the keys CTRL+B and then the command. For example, to create a new session, the command would be C. So, to create a new session we need to press CTRL+B and next C – CTRL+B, C.
Some Helpful Commands
Another fantastic feature of Tmux is that we can “save” a specific session. For example, if we are using htop and we use the detach command (CTRL+B, D) when we run Tmux again we will find the process still works. This is very useful for performing commands that take a long time to run, like backups.
So, we can start a new session by typing in this command into the command line:
tmux new
In that new session, we are going to run htop. Htop is a tool to monitor system resources.
Then, we use the detach command to disconnect. So, first, we enter the prefix by pressing CTRL+B and next, the command D. We will see that we get the next message in the terminal.
Now we need to get back to our “attached” session. To do this we execute the following command in the terminal:
tmux attach -t [session_name]
Since we have not used a name for the session, then we would use the value 0. The command looks like this:
tmux attach -t 0
And we will be back connected to our previous session.
It is possible to do several sessions with the command C. To navigate between them we use the identifier number. For example, the first session we create from the regular terminal would be 0. If we create another session it corresponds to the number 1.
CTRL+B, 1
We can see the current session with the green bar at the bottom of the window.
We can see how many Tmux sessions are open with the following command:
tmux ls
Managing Panes
Let’s learn how to manipulate terminal panels, otherwise known as split windows. We can divide a window horizontally, with the command <prefix> “
It would have to be – CTRL+B “
And to do the same but vertically – CTRL+B %
To switch between panels, we can use the command – CTRL+B, arrow key (in the direction of the pane you want to go to)
And then we can navigate through each of the panels. If we want to close only one we must press – CTRL+D.
Tmux Cheat Sheet
Finally, we want to share a cheat sheet to use as a reference:
Sessions
Start a new Session:
tmux
Start a new session with a name:
tmux new -s [name]
Start an attached session:
tmux a #
If the Tmux session has a name:
tmux attach -t [name]
List all Tmux sessions:
tmux ls
Exit the utility:
exit
Kill session:
tmux kill-session -t [name]
Window Handling
New window | <prefix>+c |
Next window | <prefix>+n |
List all windows | <prefix>+w |
Rename a window | <prefix>+, |
Previous window | <prefix>+p |
Find a window | <prefix>+f |
Kill a window | <prefix>+& |
Pane Handling
Split panes vertically | <prefix>+% |
Split panes horizontally | <prefix>+“ |
Toggle last active plane | <prefix>+; |
Swap panes | <prefix>+o |
Kill pane | <prefix>+x |
Show pane numbers | <prefix>+q |
Move plan left | <prefix>+{ |
Move plan right | <prefix>+} |
Switching between panes | <prefix>+arrow key |
Conclusion
As we learned, Tmux is an important tool that helps use the terminal efficiently by splitting windows and navigating through sessions.
The management of this utility is done through commands. We have learned the most basic and useful ones for daily work.
So if you want to know more about this tool, we recommend you consult its official documentation.
Learn More About VPS
How to Setup a Virtual Private Server
How to Use Linux Screen
How to Change Hostname in Linux
Comments
December 29 2021
This is probably the best short explanation of tux with one exception. You forgot to list one of the most powerful and useful commands.... CTRL B : setw synchronize-panes This allows you to type the command once and have it executed in every open pane. I.E. if you are installing Kubernetes on multiple nodes you can open an ssh session to separate nodes in each pane. Then typing the command "sudo yum install -y kubelet kubeadm kubectl" execute on the server in each pane simultaneously. (this assumes you have the kube repo setup) To stop the sync just type the command again... CTRL B : setw synchronize-panes
December 30 2021
Awesome, thanks Joe! I'll check with the team to include this in the article as well :)