VPS

How to Use Tmux + Cheat Sheet

In this tutorial, we will teach you how to use Tmux. It is an application that allows us to split a terminal window 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, especially if we are sysadmin who need 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 VPS 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 install tmux

Afterwards, it is a good idea to confirm the installed version. We can do it with the following command:

tmux -V

Tmux is 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 open a new session. This is done with the command:

tmux new

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 end a session, we need to type the following command:

exit

Using the Prefixes to Control Tmux

Tmux is based on commands that perform a specific task. 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 a command 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 CCTRL+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 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. 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 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 in 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

Lets learn how to manipulate terminal panels. 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.

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 attach #

If the tmux session has a name:

tmux attach -t [name]

List all tmux sessions:

tmux ls

Exit the utility:

exit

Window Handling

New window<prefix>+c
Next window<prefix>+n
List all windows<prefix>+w
Rename a window<prefix>+,

Pane Handling

Split panes vertically<prefix>+%
Split panes horizontally<prefix>+“
Kill pane<prefix>+x
Show pane numbers<prefix>+q
Switching between panes<prefix>+arrow key

Conclusion

As we learned, Tmux is an important tool that helps use the terminal efficiently.

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.

About the author

Edward S.

Edward is Hostinger's copywriter. He's an expert communicator with years of experience in IT as a writer, marketer, and Linux enthusiast. IT is a core pillar of his life, personal and professional. Edward's goal is to encourage millions to achieve an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Add Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in VPS
How to Install and Setup Mail Server on Ubuntu 18.04 LTS
Close