Nov 30, 2022
tmux: How to Install and Use It + Cheat Sheet
Performing various actions on the command line can be an efficient and rewarding experience. However, newcomers might find the process quite challenging and disorganizing.
That is where tools like tmux come in handy as it allows splitting a single window into several others. In other words, it enables you to simultaneously perform several tasks on the same screen.
This tutorial will show you how to install and use tmux. We will also go through a few popular use cases for it and provide you with a downloadable cheat sheet for future reference.
What Is tmux?
tmux is a terminal multiplexer for Unix-based operating systems. Simply put, it acts as an add-on to an existing terminal. It allows users to create multiple windows and panes within the same terminal. This is especially useful if users want to run separate processes or commands and preview the output at the same time.
Why Use tmux?
tmux is generally used to increase productivity by splitting a terminal window for each new task you have running. It is ideal for speeding up terminal tasks on your virtual private server (VPS), especially if you are a sysadmin who needs to handle several terminals simultaneously.
Another strong point is session management, allowing users to run multiple applications or jobs effectively. You can start multiple sessions, which will keep running even if you are unintentionally disconnected from your VPS.
Here are a few example scenarios for tmux:
- A user wants to start a new session and work on a new project while keeping the existing session active. With tmux, it’s possible to attach a new session and start working. Once they are done, they can open a previous session and continue their work.
- A system administrator manages a remote server, such as a VPS. They want to keep an application running even after being disconnected from the server. Even though Tmux will detach the current session, all of the processes in such a Tmux session will remain active.
- A user wants to test whether the application is running correctly, although that requires a second terminal window. To save themselves from the hassle of starting a new SSH connection in a separate terminal, they can create a new session in tmux, which will act as a second terminal session.
How to Install tmux on Ubuntu
You can find tmux in the official Ubuntu repository, thus making it effortless to install. Installing Tmux is as simple as using the APT package manager.
With APT, you can install, uninstall, and update packages without worrying about dependencies. First, access your VPS via SSH.
Installing a tmux server will require elevated privileges, so we suggest adding sudo to the apt-get command.
Run this command to install the utility:
sudo apt-get install tmux
Afterward, confirm the installed version. You can do so with the following command:
tmux is now correctly installed and ready to use.
How to Use tmux
This section will show you how to use and control tmux. The default command to start the tmux client is:
For any tmux environment command to work, you will need to use something that is called a prefix. The default prefix is Ctrl + B.
Note that you might find some websites listing tmux commands in a format of C-b (C → Ctrl and b → “B” keyboard key).
How to Split Panes in tmux
By default, the first session created by tmux will be a single terminal window. To get the most out of this tool, you will need to split the current window into several.
Important! Note that tmux keystrokes are two-part – a prefix, such as C-b, release and then quickly press the wanted command, such as %.
For example, to separate panes into two for your newly created session, the command to use is the percent sign ( % ). Since you also need to use a prefix, the entire command will be <prefix> + %.
Your window will look like this:
If you want to split the window from top to bottom, use the quotation mark ( ” ) along with the prefix. The split window will look like this:
How to Navigate Panes in tmux
Navigating between panes is as simple as using a prefix and arrow keys. Let’s say you want to move to the next pane on the right – use the prefix key combined with the right arrow ( → ) key.
For example, if you now want to return to the previously selected window, the process will be the opposite – simply use the prefix and left arrow ( ← ) key. The same applies to vertical panes, which use up ( ↑ ) and down ( ↓ ) arrows to move to the next pane.
How to Close Panes in tmux
To close a pane, you need to navigate to it first. Once the current pane is selected, click <prefix> + D or simply write exit. Note that the remaining panes will be rearranged.
How to Create Windows in tmux
You can consider a new tmux window like you would a new terminal session.
To create a new window, use <prefix> + C, and these will be displayed on the status bar:
Now it’s possible to split the new window to your liking or create a new window entirely. To navigate between windows, use <prefix> + N to go to the next window or <prefix> + P to navigate to the previous window.
Since all windows have a number, you can use <prefix> and <number> to navigate.
How to Manage Sessions in tmux
To test how tmux sessions function, you must first detach all already attached sessions.
To do so, use the <prefix> + D. Once that’s done, list out all current sessions:
tmux ls or tmux list-sessions
To connect to a specific session, use the following command with the number ( n ) corresponding to a session:
tmux attach -t n
This will take you to the selected session with all the progress saved.
It is also possible to rename your sessions. Use tmux rename-session -t n session_name to do that. n is the session ID and session_name is your preferred session name.
Note that the next time you attach this session, you will need to use the session name instead of its default ID.
tmux is among the most popular terminal multiplexers for Unix-based operating systems. It allows users to efficiently manage their terminal window and adapt it to their liking.
In this tutorial, we have covered some of the basic tmux concepts:
- Installing tmux on Ubuntu
- Starting tmux client
- Splitting, navigating, and closing panes
- Creating new tmux windows
- Managing tmux sessions
We have also provided a downloadable tmux cheat sheet for you to use for future reference.
We hope you found this tutorial useful. If you have any further questions or insights, leave them in the comments section.
Learn More About VPS
tmux: Frequently Asked Questions
Here are a couple of the most frequently asked questions regarding tmux.
Is tmux Better than screen?
Both tmux and screen serve a similar purpose. However, users consider tmux more convenient because it shows a status bar and features automatic window renaming. Another tmux advantage is that commands can be run straight from the shell. Not only that, tmux provides its users with convenient keybindings making it effortless to use.
Where Are tmux Sessions Stored?
By default, tmux does not save its sessions. The sessions will be terminated if you turn off your computer or virtual private server. However, it is possible to open the terminal window again, list out all of the current sessions and re-attach them to be ready for use again.