Published on

What is Tmux? | How to Use Tmux?

what-is-tmux

What is Tmux?

Tmux is a terminal multiplexer. In other words, it means that you can start a Tmux session and then open multiple windows inside that session. Each window occupies the entire screen and can be split into multiple pane.

A common use-case for Tmux is on a remote server where you have a common layout that you always use, and want a way to quickly jump into and out of that layout. An example would be if you're connecting through a jump server and have other remote SSH session you would like to be connected to simultaneously.

With Tmux you can easily switch between multiple programs in one terminal, detach them and reattach them to a different terminal.

Tmux Installation on Different Linux Distros

Tmux install on Ubuntu and Debian

sudo apt install tmux

Tmux install on Fedora and CentOS

sudo yum install tmux

Tmux install on macOS

brew install tmux

Note: Confirm that it installed by checking the version.

tmux -v

Start a Tmux Session

tmux

This will start a new Tmux session and create a new window. Tmux will automatically login to you default shell your user account.

tmux-session

Once you are in Tmux you'll notice a status line at the bottom of the screen which shows information about the current session.

You can now run your first Tmux command. For example, to get a list of all the available commands you can type:

ctrl+b ?

tmux-command-list

Start a Tmux Session with a Custom Name

By default, Tmux session are named automatically. Named sessions are useful when you run multiple sessions. To create a new session with a custom name you can use the tmux command with the following arguments:

tmux new -s <session-name>
tmux-session-name

It's always a good idea to choose a descriptive name for your session.

Deteching from a Tmux Session

You can detach from the Tmux session and return to your normal shell by typing: ctrl+b d

tmux-detach

The program running in the Tmux session will continue to run after you detach from the session.

Re-attaching to Tmux Session

to a seesion first, you need to find name of session. To get a list of the currenly running sessions you can type:

tmux ls

The output will be a list of all the previous sessions and first column will be the session name.

tmux-session-list

As you can see, there are two sessions running. The first one is the default session is named 0 and the second one is named development.

If you want to re-attach to session development you can type:

tmux attach-session -t development

Working with Tmux Windows and Panes

When you start a new Tmux session, default it will create a single window with your default shell.

If you want to create new window with shell type ctrl+b c, the first available number from the range 0...9 will be assigned to the new window.

tmux-new-window

A list of all the windows in the current session shown on the status line at the bottom of the screen.

There are some most commonly used commands to work with Tmux windows and panes.

  • ctrl+b c - Create a new window (with shell).
  • ctrl+b x - Close the current pane.
  • ctrl+b 0 - Switch to window 0 (by number).
  • ctrl+b , - Rename the current window.
  • ctrl+b % - Split current pane horizontally into two panes.
  • ctrl+b " - Split current pane vertically into two panes.
  • ctrl+b w - Choose a window to switch to.
  • ctrl+b o - Go to the next window.
  • ctrl+b ; - Toggle between the current and the previous window.
  • ctrl+b & - Kill the current window.

Customizing Tmux

When Tmux is started, it will automatically load the configuration file ~/.tmux.conf.

Here is an example of a custom configuration file:

~/tmux.conf
# Improve colors
set -g default-terminal "screen-256color"

# Set scrollback buffer to 10000
set -g history-limit 10000

# Customize the status line
set -g status-fg green
set -g status-bg black
tmux-custom-config