How to use tmux on Linux?


Overview

Tmux is a terminal multiplexing utility for Unix systems. It provides an interface between several programs running simultaneously on one computer.

Tmux allows us to detach from any terminal sessions without killing them. We can then reattached to any of the terminal sessions later on.

We’ll learn the tmux terminal emulator in Linux. Specifically, we’ll examine some of its features and commands.

Installation

You can install tmui on Debian-based Linux systems using the apt-get package manager.

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

We can also use the yum command line tool to download and run the tmux software on Red Hat Enterprise Linux systems.

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

Basic Usage

You can start a new tmux window by typing tmux alone.

$ tmux

By default, tmux creates a new session with just one window. Also, the session is given an automatic name by the server – 0. After creating the file, we’ll immediately be dropped into the tmux session.

Key Bindings

According to the documentation, tmux supports a wide range of commands. These commands are normally used to set up the tmux session. Examples include commands for opening new windows, dividing panes in the window, and detaching these windows.

To establish keybinds for the prefix key, they should be initiated with the prefix key.

Sessions in tmux

With tmux, you can create a set of pseudoterminal sessions that are managed directly by the tmux server. These are known as groups.

Creating a Session

To open a new session in tmux, use the new-session command.

For example, we could use the -s flag to set up a session called "gamma" and then run it −

$ tmux new-session -s gamma

We can also use the -n flag to specify the session's name.

$ tmux new-session -s gamma -n observation

Detaching From a Session

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

<prefix key> + d

Attaching to a Session

We can use the "attach-session" command with the "-t" flag and the session's ID. For example, we could attach to the gamma session −

$ tmux attach-session -t gamma

Listing All the Sessions

If we want to see a list of all the current tmux servers, we can run the list-sessioins 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)

We can see from the output that there are currently two active sessions on the tmux sever: alpha and gamma.

Renaming a Session

You can use the `rename-session` commands to change the name of an existing session.

$ 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)

Terminating a Session

We must first identify the names of the sessions before killing them. Next, let’s terminate the session by running the “kill-session” command with the – t option −

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

Windows in tmux

Every tmux session contains one or more windows; similarly to sessions, these windows can be created, closed, named and changed.

Creating a New Window

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

<prefix key> + c

Each window's index starts with 0, and every window has its unique name, usually the command being executed in that window.

Listing All the Windows

You can see a list of all the open tabs in this window by typing −

<prefix key> + w

We can use the up/down arrows to move through the list. Next, we press Enter to choose which screen we want to display.

Renaming a Window

Tmux lets us change the name of our windows by typing the following commands −

<prefix key> + ,

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 −

Terminating a Window

To terminate a window, we can use the hotkeys −

<prefix key> + &

When tmux prompts for confirmation before terminating a session, it will ask if you want to save any changes made to files To confirm the termination of the command, type "y" and press "ENTER".

Panes in tmux

By using tmux, it's possible to divide windows into numerous individual panes which respectively contain their own distinct terminal session.

Splitting Window Into Panes

To divide a window horizontally into two separate windows, you can press the following keyboard shortcuts −

<prefix key> + "

When we type the command, the current terminal window will be divided into two panes – a top pane and a bottom pane.

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

<prefix key> + %

Terminating a Pane

To end a pane, we press the keyboard shortcut while we're on the pane.

<prefix key> + x

Similar to closing a terminal, tmux will ask us if we want to confirm our action. To terminate the command, type "y" followed by pressing Enter.

Copy Mode in tmux

Copy and Paste With the tmux Clipboard

Copying text in tmux is complicated than just using the Ctrl + C and Ctrl + V keys.

To explanation it easier, we’ll copy the output of date command while we’re still inside tmux −

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

<prefix key> + [

To copy the text from here, we’ll use the up arrow key to go one row above. When the cursor is in position, we’ll press the keyboard shortcut to begin selecting text.

<prefix key> + Space

We can use the LEFT ARROW KEYS to move the cursor to the beginning of the next line.

Once you have made your choice, hit ENTER to transfer the text into tmux's clipboard.

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

<prefix key> + ]

Conclusion

We've looked at the tmux CLI tool in this tutorial.

We began by using some basic functions. We then looked at the different components that make up tmux. We also learnt shortcuts for configuring tmux on each of its components.

We've finally looked at the copy command in tmux and seen some of the things we can do in copy (or paste) modes.

Updated on: 03-Jan-2023

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements