1. Overview

tmux is a terminal multiplexer tool in Linux. Essentially, it allows us to create and maintain multiple pseudo-terminal instances. Concretely, it maintains these pseudo terminals through a server process that’s started whenever a tmux command is executed.

Because these terminal processes are maintained by a server process, tmux provides us the flexibility to detach from any terminal session without killing them. Furthermore, we can reattach any of the terminal sessions at a later time to regain control. This makes it ideal for any long-running process that we can run in the background and reattach to regain control once in a while.

In this tutorial, we’ll be learning the tmux command in Linux. Particularly, we’ll look at some of the functionalities and commands offered by tmux.

2. tmux

2.1. Installation

We can install tmux on Debian-based Linux (such as Ubuntu) using the apt-get package manager:

$ sudo apt-get update -qq
$ sudo apt-get install -y tmux

Similarly, we can install tmux on RHEL-based Linux (such as CentOS) using the yum package manager:

$ sudo yum update -qq
$ sudo yum install -y tmux

2.2. Basic Usage

We can launch a new tmux session by entering the command alone:

$ tmux

Without any argument, tmux creates a new session with one window. Additionally, the session is given a default name – 0. Immediately after the creation, we’ll be dropped into the tmux session:



3. Key Bindings

We can see from the documentation that tmux supports a lot of commands. These commands are usually intended for configuring the tmux instance. For example, there are commands for creating new windows, splitting panes, and detaching.

To make these commands easier to enter, tmux defines a number of key bindings (also known as shortcuts or hotkeys) that we can utilize to boost our productivity. In the article, we’ll mostly be interacting with tmux using these key bindings.

For key bindings in the prefix key table, we’ll need to prefix them with a prefix key. By default, tmux sets the prefix key as Ctrl + b. Additionally, we’ll annotate the prefix key with <prefix key> in the rest of the article.

4. Sessions in tmux

A session in tmux groups together a number of pseudo terminals. These terminals are directly under the tmux server’s management.

4.1. Creating a Session

We can create a new session in tmux using the new-session command. The command supports several option flags that allow us to customize the session.

For example, we can create a session and give it a name using the -s flag:

$ tmux new-session -s gamma

Once entered, we’ll be dropped into the gamma session:



In addition to the session name, we can specify the window name using the -n flag:

$ tmux new-session -s gamma -n observation



Using the -d flag, we can choose not to attach to the newly created session:

$ tmux new-session -s gamma -d
$ tmux list-session
alpha: 2 windows (created Sun Jun 20 11:37:35 2021)
gamma: 1 windows (created Sun Jun 20 12:11:41 2021)

After we enter the new-session command, we’ll not be dropped into the session due to the -d flag. However, we can still confirm that the session has been created.

4.2. Detaching From a Session

To detach the client from a session, we can enter the hotkey:

<prefix key> + d

4.3. Attaching to a Session

To attach to a session, we can use the attach-session command followed by the -t flag and the name of the session. For instance, we can attach to the gamma session:

$ tmux attach-session -t gamma

4.4. Listing All the Sessions

To view a list of all the sessions currently on the tmux server, we can use the list-session command:

$ tmux list-session
alpha: 2 windows (created Sun Jun 20 11:37:35 2021)
gamma: 1 windows (created Sun Jun 20 12:11:41 2021)

From the output, we can see that there are currently two sessions running on the tmux server: alpha and gamma.

4.5. Renaming a Session

To rename an existing session, we can use the rename-session command:

$ tmux rename-session -t alpha beta
$ tmux list-session
beta: 2 windows (created Sun Jun 20 11:37:35 2021)
gamma: 1 windows (created Sun Jun 20 12:11:41 2021)

The rename-session command requires us to specify the target session to be renamed using the -t flag. In this example, we’re renaming our session alpha to beta.

4.6. Terminating a Session

To kill a session, we’ll need to first identify the name of the session. Then, we can terminate the session using the kill-session command with the -t flag:

$ tmux kill-session -t gamma
$ tmux list-session
beta: 2 windows (created Sun Jun 20 11:37:35 2021)

The -t flag specifies the session to kill, which is session gamma in our example.

5. Windows in tmux

Within each tmux session, there are one or more windows. Similar to the session, windows can be created, removed, named, and switched.

5.1. Creating a New Window

To create a new window in a session, we could use the hotkeys:

<prefix key> + c

From the status bar at the bottom of the screen, we can see that there are currently two windows in this session:



Each window is given an index starting from 0. Additionally, each window is given a name. By default, the name would be the command the window is currently executing. In our example, both of the windows are named bash because that’s the currently running process in both windows.

Finally, tmux indicates the currently active window by placing an asterisk beside the window’s name. From the screenshot, we can tell that we’re looking at the window index number 1.

5.2. Listing All the Windows

We can display a list of all the windows in this session by typing:

<prefix key> + w



While on the list, we can navigate the list using the arrow keys UP and DOWN. Then, we press ENTER to select the window we want to display.

5.3. Renaming a Window

To make identification easier, tmux allows us to change the window name using the command:

<prefix key> + ,

Notice that the status bar at the bottom of the screen turns yellow. This indicates that tmux is expecting our input.

Let’s change the name of our window to window1:



Once we change the name and press ENTER, we can see that the name change reflects on the status bar:



5.4. Terminating a Window

To terminate a window, we can use the hotkeys:

<prefix key> + &

Before a window is terminated, tmux will prompt for confirmation. To confirm the termination, we enter y and press ENTER.

6. Panes in tmux

The window in tmux can be further split into multiple panes. Furthermore, each of the panes runs exactly one standalone terminal instance.

6.1. Splitting Window Into Panes

To split a window along the horizontal axis into two different panes, we can use the hotkeys:

<prefix key> + "

Once we enter the command, the current window will be split into two panes – one at the top and one at the bottom:



Alternatively, we can split a window into two panes vertically using the hotkeys:

<prefix key> + %



6.2. Terminating a Pane

To terminate a pane, we enter the shortcut while we’re on the pane:

<prefix key> + x

Similar to terminating a window, tmux will prompt us for confirmation. To confirm the termination, we key in y and then press ENTER.

6.3. Breaking a Pane out to Window

We could break a pane out and make it a standalone window. For example, if we want to turn a window with two panes into two windows, we can use the shortcut:

<prefix key> + !

Once entered, the effect will take place immediately and we’ll see an additional new window that is made up of the previous panes.

6.4. Switching to Different Pane

On a window with several panes, we can easily switch to a different pane using the hotkey:

<prefix key> + <arrow key>

For instance, to switch to the right pane from the left pane, we’ll simply press the prefix key followed by the RIGHT arrow key.

Similarly, to switch to the bottom pane, we’ll enter the prefix key and then the DOWN arrow key.

7. Copy Mode in tmux

7.1. Copy and Paste With the tmux Clipboard

Copying text in tmux is a bit more involved than a simple Ctrl + C and Ctrl + V.

Generally, we’ll need to first go into copy mode. Then, we’ll select a portion of text to copy. Finally, we can paste the text from the tmux clipboard. Let’s look at the exact steps to copy and paste in tmux.

To make the explanation easier, we’ll try to copy the output from the date command while we’re still inside tmux:



First, we’ll use the shortcut to go into copy mode:

<prefix key> + [



When we’re in copy mode, there’ll be an annotation with two numbers at the top right. The first number from the left represents the line number our cursor is on. Then, the number at the right represents the total number of lines we have in the standard output.

To copy the text, we’ll move our cursor 1 line above using the UP arrow key. Once the cursor is in place, we’ll hit the shortcut to start the selection:

<prefix key> + Space

When we begin the selection, we can see that the background color of our cursor turns yellow. This indicates that our cursor is now in selection mode. To select the line, we can move the cursor to the left using the LEFT arrow key:



Once we’ve confirmed the selection, simply hit ENTER to copy the text into tmux‘s clipboard.

Finally, we’ll use the shortcut to paste the text from tmux‘s clipboard:

<prefix key> + ]



7.2. Scrolling Standard Output

To see the standard output that’s beyond the display in tmux, we’ll need to first go into copy mode. Then, we can navigate the cursor using the arrow keys. Alternatively, we can press Page Up or Page Down to scroll by pages instead of lines.

7.3. Other Commands in Copy Mode

In copy mode, tmux supports a myriad of functions akin to the ones we usually use in text editors.

For instance, we can move to the next word by pressing the w key while in copy mode. Similarly, we can move to the start of the line using the key 0. Furthermore, we can jump straight to the line by pressing the colon key and then entering the line number.

The commands we’ve mentioned thus far are just a small subset of what’s possible while in copy mode. For a full list of supported commands and their shortcuts, we can refer to the tmux documentation.

8. Conclusion

In this tutorial, we’ve taken a good look at the tmux command-line tool.

We started off with some basic usage. Then, we looked at the different components of tmux. On each component, we also learned some of the shortcut keys we can use to configure tmux.

Finally, we explored the copy mode in tmux and some of the commands we can issue in copy mode.

Comments are closed on this article!