How to Install and Use Tmux?
With Tmux (terminal multiplexer), you can handle multiple terminal windows in one place. Thus, you can run different commands at the same time without opening many terminals separately. The Tmux tool lets you do everything inside a single session.
Let’s learn various practical aspects of Tmux after knowing some basic terms –
- A pane is a terminal prompt in tmux. You split the terminal window and each split is a pane.
- A window holds multiple panes/terminal prompts.
- A session is a collection of windows. When you start tmux, a session is created.
- A server runs in the background and manages multiple sessions to ensure persistence even if you disconnect.
How to Install tmux on Different Linux Distributions
The process of installing tmux depends on the type of Linux distribution you are using.
Installing tmux on Debian and Ubuntu
You can easily install tmux using the apt package manager. Run the below command in your terminal. It will update the system and install tmux.
$ sudo apt-get update && sudo apt-get -y install tmux
Installing tmux on Redhat, CentOS, and RHEL-Based Distributions
Install tmux using yum. Just type the following in your terminal –
$ sudo yum install update && sudo yum -y install tmux
Starting a New tmux Session
The below command creates a session with the name start. At the bottom of your screen, in the status bar, you will see the session name.
$ tmux new -s start
Navigating in tmux
You interact with the program by using a “prefix key” in tmux. It triggers commands and perform various actions.
Default prefix key is Ctrl + B.
Creating Multiple Panes
=> To split your screen into two vertical panes – first press Ctrl + B – then press Percent (%)
=> To split the current pane into two horizontal panes – press Ctrl + B – followed by Quote (“)
=> To cycle through all open panes – press Ctrl + B – then O.
Switching Between Panes
Use the arrow keys with the prefix key, you can switch between the different panes.
- To the pane above – Ctrl + B, then Up
- To the pane below – Ctrl + B, then Down
- To the left pane – Ctrl + B, then Left
- To the right pane – Ctrl + B, then Right.
Creating and Managing Multiple Windows
=> To create a new window (blank terminal) – Ctrl + B and then C.
=> To split a window into two horizontal panes – press Ctrl + B and then Quote.
=> To create another window – press Ctrl + B and then C again.
You can continue creating new windows this way.
=> The below command is to check how many windows you have created –
$ tmux list-windows
=> To cycle through the windows, use Ctrl + B, then N.
Attaching and Detaching from a tmux Session
You can detach from a current tmux session (to leave it running in the background) by pressing Ctrl + B, then D.
To reattach to your session, run the command –
$ tmux attach -t YOUR_SESSION_NAME
You can cycle through the windows in your session using Ctrl + B followed by N.
Using Vim Key Bindings for Pane Navigation
You can configure tmux to use the Vim keys: H, J, K, L in place of the arrow keys to switch between panes. For this, you need to edit the tmux configuration file. Create the file if it does not exist with the following command –
$ nano ~/.tmux.conf
Add the following lines to enable Vim-style pane navigation –
# hjkl pane traversal bind h select-pane -L bind j select-pane -D bind k select-pane -U bind l select-pane -R
Restart tmux or load the new configuration after editing the configuration file with the below command –
$ tmux source-file ~/.tmux.conf
The Meta Key in tmux
Many key bindings use the Meta key. It is not a specific key and varies as per system or terminal emulator. Most of the systems have Esc, Alt, Command, Win, or Opt as Meta keys. Try Esc first when using Meta key combinations. Try other keys if it does not work.
Adjusting Pane Sizes
Press Ctrl + B and then the below arrow keys to resize continuously while the keys are kept pressed.
- Height of the pane – Ctrl + B, then Up/Down
- Width of the pane – Ctrl + B, then Left/Right
If you need to go one step at a time, press the Meta key after Ctrl + B and then the required arrow key.
=> The resizing of a pane depends on how it is positioned with others.
=> You can use the mouse if the terminal emulator supports it. Simply drag the borders.
Arranging Panes in Tmux
Tmux gives you different ways to place all panes such as side by side, one below the other, or arrange them in a mix of stacked (one pane larger width-wise) & side-by-side layouts. You can also keep one main pane larger (height-wise) and others stay below or beside it. Lastly, you can have all the panes in a grid.
Start tmux and open multiple panes. Switch between these layouts with Ctrl + B, then Space.
Use the below shortcut –
- Ctrl + B, then Meta + 1 → First layout
- Ctrl + B, then Meta + 2 → Second layout
- Ctrl + B, then Meta + 3 → Third layout
- Ctrl + B, then Meta + 4 → Fourth layout
- Ctrl + B, then Meta + 5 → Fifth layout
Changing Pane Positions
tmux lets you swap multiple panes around so that you do not need to manually close them –
=> To switch focus from one pane to another, press Ctrl + B, then O.
=> To move panes in a clockwise direction themselves – Ctrl + B, then Meta + O
=> To move panes in a counterclockwise direction themselves – Ctrl + B, then Ctrl + O
Moving Panes to a Different Window
To move a pane to another window without affecting the arrangement of the remaining panes, first open the tmux command prompt with Ctrl + B, then Colon (:).
They type the below to detach the selected pane from the current window –
break-pane -dP
You will see its ID displayed at the top of the remaining panes. You can dismiss it by pressing Esc.
Go to the target pane and type below to reattach this detached pane below active pane.
join-pane -vs X:Y.Z
To place it on the right, use the below command –
join-pane -hs X:Y.Z
Sharing a Tmux Session with Another User
To connect multiple users to the same session, you need proper permissions. tmux creates a server when you start it; it runs in the background.
This server communicates with tmux clients using a UNIX socket stored in the /tmp directory. It saves server sockets in a special sub-folder named tmux-UID (UID is the user’s UNIX ID). By default, no other user or group can open it except for user 1.
The steps here have been tested on tmux versions 3.1 and 3.3 on Debian 11. For tmux 3.3 or later there is an extra step to grant access using the server-access command. To check your version, run:
tmux -V
Steps to Share a Session
- Log in as user1 and start tmux
tmux
- Check the socket directory created by tmux
ls -hdl /tmp/tmux-1001
- Create a new group (if not already created) to manage permissions
groupadd tmuxusers
- Add both users to the group
usermod -a -G tmuxusers user1 usermod -a -G tmuxusers user2
- Change the group ownership of the tmux socket directory
chgrp -R tmuxusers /tmp/tmux-1001
- Grant full access to the group
chmod -R g+rwx /tmp/tmux-1001
- Verify that the group and permissions are set correctly
ls -hdl /tmp/tmux-1001
- Exit the running tmux session
exit
- Start a new tmux session with a named socket as user 1
tmux -L socket1
- (For tmux 3.3+) Grant user2 access to the session
- Write access (user2 can enter commands)
tmux server-access -w -a user2
- Read-only access (user2 can only view the session)
tmux server-access -r -a user2
- To remove access
tmux server-access -d user2
- Ensure the socket file belongs to the correct group
sudo chgrp tmuxusers /tmp/tmux-1001/socket1
- Verify permissions
ls -l /tmp/tmux-1001/socket1
- Check the available tmux sessions (IDs)
tmux ls
- Log in as user2 (in other session) and list available sessions
tmux -S /tmp/tmux-1001/socket1 ls
- Attach other user to the shared session
tmux -S /tmp/tmux-1001/socket1
- If there are multiple sessions, you can attach to a specific one
tmux -S /tmp/tmux-1001/socket1 attach -t N
Handling Nested Tmux Sessions
If you are logged into a remote system via SSH and the remote system also uses tmux, you will end up with a nested session (running a tmux session inside another tmux session)
To send a command to the inner session, press Ctrl + B twice and then add the required combination.
Common Key Bindings in tmux
Below key bindings below can help you perform everyday tasks more efficiently –
- Window renaming with Ctrl + B, then Comma
- Go to next window with Ctrl + B, then N AND the previous window with Ctrl + B, then P
- Ctrl + B, then Ampersand (&) closes the current window
- Ctrl + B, then Question (?) gives a key bindings list.
- To go to the next pane – Ctrl + B, then O
- Ctrl + B, then Q displays the numbers of all panes (then type the # to switch to it).
- Ctrl + B, then X to close the active pane (then Y to confirm).
- Overview of all active windows and panes with Ctrl + B, then W
Common Commands in tmux to manage sessions
- See all existing tmux sessions
tmux list-sessions
- For creating New tmux session with the name session-name
tmux new -s session-name
- To attach to an existing tmux session with the name session-name
tmux attach -t session-name
- Go to an existing tmux session.
tmux switch -t session-name
Conclusion
Tmux is a great tool. It keeps your work safe even if your connection drops. Also, you can start a session on one device and continue it later from another. The programs will still be active and running when you return.
Tmux follows a client-server model so it can maintain persistent sessions. In other words, the session you create can be disconnected and reconnected whenever you want without affecting work. Developers and system administrators who work on multiple tasks at the same time highly benefit from this.