Linux Tmux Tutorial With Examples


Tmux is a terminal multiplexer popular in Linux world. Tmux provides multiple windows in a single session. Tmux also has the ability to detach and run after disconnect and attach after connection. This is very useful for system administrators.

What Is Tmux?

Tmux is described as terminal multiplexer. This terms means a single terminal can be used like multiple terminal. Tmux is based command line interface where there is no need to run a graphical interface. Tmux can provide multiple windows like different pages and each window may have single or multiple panes which divides current window visually.

Tmux Advantages and Usecases

Tmux is a popular tool between system administrators and geeks with its lifesaver features. Tmux provides the following advantages and uses cases that make it superior to other terminals.

  • Tmux can run multiple sessions at the same time.
  • Even SHH or remote connection drops Tmux can continue reconnecting existing sessions.
  • Tmux can be used run commands continuously even the connection lost.
  • Tmux can be managed completely with key shortcuts.
  • Tmux requires very very low system resources.

Install Tmux

Installation of the tmux is very easy on Linux distributions like Ubuntu, Debian, Mint, Kali, Fedora, CentOS, RHEL etc.

Ubuntu, Debian, Mint, Kali:

$ sudo apt install tmux
Install Tmux

Fedora, CentOS, RHEL:

$ sudo dnf install tmux

Tmux Man Page

The manpage of tmux provides a lot of detailed features. To get more information man page of tmux can be used like below.

$ man tmux
Tmux Man
Tmux Man

Start Tmux

Tmux is a terminal emulator that acts as a shell. Tmux can be started as a shell too. Just run tmux command we will get tmux screen by clearing the existing shell screen.

$ tmux
Start Tmux
Start Tmux

Bind Key

Tmux uses  CTRL+b for operation key which means to use shortcuts of tmux CTRL+b should be prefixed with the related keyboard shortcut.

CTRL+b

List Current Key Bindings

Tmux mainly used with keys with bind keys. These key bindings can be listed with the following command. As we can see from the following screenshot there are a lot of key bindings provided by tmux.

CTRL+b :list-keys
List Current Key Bindings
List Current Key Bindings

Start New Window

One of the most useful functionalities of the tmux is operating multiple windows. Existing windows are listed in the green bar below. Creating a new window will create a new shell like a new tab in terminal emulators.

CTRL+b c
New Window
New Window

Move Next Window

While working with multiple windows navigation between windows can be done in different ways. One of the most common usages is switching the next window with the following key short cut.

CTRL+b n
Move Next Window
Move Next Window

After switching the asterisk which shows the current desktop will put a new window afterward.

LEARN MORE  What is File System Block And Super Block?

Move the Previous Window

This is similar to moving the next window. Just type following keyboard shortcut.

CTRL+b p
Move Previous Window
Move the Previous Window

Move To The Specified Window Number

Another useful navigation feature is using window number to navigate. Window numbers can be seen below of the terminal before the window name.

CTRL+b <number of window>

Rename Window

Normally tmux sets current running application name as window name. If we are using a lot of windows for multiple operations and we want to rename the window name.

CTRL+b ,
Rename Window Name
Rename Window Name

What Is Pane?

The pane is used to multiplexing a single window. Using multiple commands in a single-window is very useful and popular usage. Below we can see two horizontal pane. We will look at how to create panes below.

Tmux Pane
Tmux Pane

Create New Vertical Pane

The current window can be divided into two vertical panes with the CTRL+b %.

CTRL+b %
Vertical Pane
Vertical Pane

After creating a pane current working pane will change to the new pane and active pane can be seen in the line between panes. The lower part of the line is yellow which means left pane is active.

New Horizontal Pane

Horizontal pane can be created with CTRL+b ".

CTRL+b  "
New Horizontal Pane
New Horizontal Pane

Move and Navigate Between Panes

To move between panes direction keys can be used like below.

Move Left Pane

CTRL+b <left arrow>

Move Right Pane

CTRL+b <right arrow>

Move Below Pane

CTRL+b <down arrow>

Move Upper Pane

CTRL+b <up arrow>

Show Pane Numbers

While working multiple pane it can be useful to show pane numbers. Pane numbers can be shown according to their location with the command below. While showing pane number in the upper left corner of the pane the size is also shown.

CTRL+b q
Show Pane Numbers
Show Pane Numbers

Close Pane

If we have finished our job with a pane we generally close it to make things clean. We can close the current pane with the following shortcut.

CTRL+d

OR

:exit

Copy Text

One of the most important features is copying and pasting text from the terminal. Tmux provides a bit different method to copy-paste. Normally console provided copy paste can be used but if the source is multiline text it can be impossible to copy properly. Below are the steps to copy the text.

LEARN MORE  How To Solve ssh-copy-id "ERROR: failed to open ID file" Error

Start Copy Mode

CTRL+b [
Copy Mode
Copy Mode

Set Start Point

We can move the cursor to the start of the copy range in the copy mode and after that to set the starting point following command is used.

CTRL+<space>
Copy Mode
Copy Mode

Set End Point

Move the cursor and we see can see the current selection is in orange color. To set endpoint and copy the contents to the buffer use following command.

ALT+w

Paste

Copied text will be put into the buffer. We can use this text wherever we want. We can also access this text in tmux from different windows.

CTRL+b ]
Tmux Paste
Tmux Paste

Create Session

Creating a tmux session is just starting tmux. But we can also provide some parameters like the name.

$ tmux new -s mysession
Create Session
Create Session

List Sessions

There may be more than one tmux sessions. These sessions can be listed with the following command.

CTRL+b s
List Sessions
List Sessions

Detach Tmux

The existing session can be detached like below. This will not end the session thus this will only detach session from the current shell and it will continue running in the background.

CTRL+b d
Detach Tmux
Detach Tmux

Reattach

Attaching session means reconnecting currently running session which is detached before.

$ tmux a

Configuration File

There is to path where the configuration file resides. /etc/tmux.conf and ~/.tmux.conf.

Leave a Comment