If you're constantly switching between terminals and can't find the right window when needed, consider using a multiplexer. These are programs that allow users to run multiple terminal sessions inside a single window.

Tmux is a popular multiplexer that offers a plethora of amazing features. It makes it easy for you to run different applications in separate sessions and switch between them effortlessly.

How to Install Tmux in Linux

This guide showcases how to install and configure tmux for Linux machines. Fire up a terminal and use the appropriate installation method for your system.

On Debian-based distributions:

        sudo install tmux
    

On CentOS/REHL:

        yum install tmux
    

To install tmux on Arch Linux:

        pacman -S tmux
    

Once the installation is finished, launch a terminal and type in tmux to start a fresh tmux session. This command will initiate a new session inside a window and start the shell. The status bar at the bottom displays information about your current session.

        tmux
    

How to Use Tmux as a First Timer

Tmux has notions like sessions, windows, panes, and hotkeys. Thus, beginners often find it intimidating at first. But, once you get the hang of it, you'll feel much more productive.

A tmux session defines the work at hand. Windows allow users to perform different activities inside a session. Panes, on the other hand, will allow you to work with separate views inside a window.

You can manage all of these via tmux hotkeys, a combination of the tmux prefix followed by a specific key. The default prefix is Ctrl + B.

How to Manage Sessions in Tmux

Tmux can handle multiple sessions at once. This allows developers to switch between various projects at ease. You can also create new sessions anytime you want.

        tmux new -s test-session
    

The aforementioned command creates a new session named test-session. You can create sessions from an already running instance of tmux as well. To do this, invoke the tmux interpreter and then type in the prefix followed by a colon character, or Ctrl + B : for the default prefix.

The command will open a new tmux interpreter where we can input control commands. Type the following to create a new session.

        :new -s test-session
    
Creating Named Session in Tmux

Enter Ctrl + B s to view all active sessions. You can switch to a different session by selecting it and pressing Enter.

Viewing active tmux sessions

You can detach from any session, and tmux will still keep the process running. Type :detach in the tmux interpreter or enter Ctrl +B d for detaching the current session.

Detaching Tmux sessions in Linux

Use the following command to attach to the last session.

        tmux attach
    

You can attach to a specific tmux session by specifying the session name. Use the -t option to do this.

        tmux attach -t test-session
    

To kill a tmux session, use the kill-session command.

        tmux kill-session -t test-session
    
Closing Tmux Sessions in Linux

Related: The Essential Tmux Commands Cheatsheet

How to Manage Windows in Tmux

Tmux windows span the entire screen and can be split into several panes. Each of these panes will act as a separate pseudo-terminal. You can create a new tmux window using Ctrl + B c.

Use Ctrl + B , for renaming your tmux windows. It will invoke the tmux interpreter. Type in the new window name here.

Renaming Tmux Windows

Tmux makes switching between different windows effortless. Enter the tmux prefix, followed by the window number. For example, you can quickly switch to the second window using Ctrl + B 2.

You can also swap tmux windows. To do this, invoke the interpreter by typing Ctrl + B : and enter the following.

        :swap-window -s 1 -t 3
    

This command switches the first and third windows. You can delete a window once you're done with it using Ctrl + B &.

Terminating Tmux Windows

How to Manage Panes in Tmux

So far, we have used sessions and windows in tmux. However, things get really exciting when you start using panes. Panes are basically pseudo-terminals running inside a window. We can use them for running several terminal instances from a single tmux window.

You can create horizontal and vertical panes in tmux. Type in Ctrl + B " for splitting the view horizontally.

Creating Horizontal Panes for Tmux

This will split the current window horizontally and open a new terminal in the new window. Use Ctrl + B % for starting a vertical pane.

Creating Vertical Panes for Tmux

You can create horizontal and vertical panes at the same time. Use the hotkey Ctrl + B o for switching between panes.

Toggling the pane zoom allows users to hide all the other panes. This feature is handy when you need to focus on a specific task. Use Ctrl + B z for toggling panes. You can close the current pane anytime by using the hotkey Ctrl + B x.

Closing Tmux Panes

How to Configure Tmux in Linux

You can configure almost every aspect of your tmux installation. However, we recommend you start with the basics and move on to more advanced options later.

Tmux configurations are done by tweaking the ~/.tmux.conf file. Use your favorite Linux text editor to edit this file.

        vim ~/.tmux.conf
    

Let's show some basic configurations. For example, you can change the tmux prefix from Ctrl + B to Ctrl + A by adding the below line in the tmux.conf file.

        set -g prefix C-a
unbind C-b

Unbinding the default key allows us to reassign it for a different command. So, the second line is a good practice, nothing mandatory.

Configuring Tmux in Linux

We can also change the base index of windows and panes from zero to one. It's intuitive for users who aren't comfortable with a zero-based index.

        set -g base-index 1        # starts window numbering from 1
set -g pane-base-index 1 # starts pane numbering from 1

Since tmux is a terminal-driven tool, it does not support mouse functions out of the box. However, you can easily enable mouse support for tmux by adding the below file to your configuration.

        set -g mouse on
    

Introduction to Tmux Plugins

You can choose from a number of tmux plugins developed by the community. They offer additional features for improving productivity which in turn, enhances your overall workflow. Some standalone plugin managers are also available that help users in implementing these plugins into tmux.

The Tmux Plugin Manager (TPM) is one such tool that allows us to install and configure third-party plugins. You can install it by following the instructions on TPM's official site.

We strongly recommend users to check out the tmux-resurrect package. You can use it for saving tmux sessions between reboots.

Manage Terminal Workflow Using Tmux

Tmux offers a full-fledged solution for terminal enthusiasts who have a hard time switching between windows. It's a great tool for people who take productivity seriously. Although there's some learning involved, it becomes a habit as you continue using Tmux.

The power of terminals is not only limited to Linux operating systems or computers in general. You can even use a terminal in your smartphone and perform some basic Linux computations on Android.