Linux tmux Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the tmux command-line tool to manage and control multiple terminal sessions on your Linux system. Tmux, a powerful terminal multiplexer, allows you to create, access, and switch between multiple terminal sessions within a single window, making it particularly useful when working on complex tasks that require multiple terminal windows. You will start by learning about the basics of tmux, including how to install it and start a new session. Then, you will explore how to navigate and manage tmux sessions, including creating new sessions, switching between sessions, and closing sessions. Finally, you will learn how to customize tmux by modifying its configuration files.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("`Secure Connecting`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") subgraph Lab Skills linux/source -.-> lab-422960{{"`Linux tmux Command with Practical Examples`"}} linux/ssh -.-> lab-422960{{"`Linux tmux Command with Practical Examples`"}} linux/nano -.-> lab-422960{{"`Linux tmux Command with Practical Examples`"}} linux/set -.-> lab-422960{{"`Linux tmux Command with Practical Examples`"}} end

Introduction to tmux

In this step, you will learn about the tmux command-line tool and how it can help you manage and control multiple terminal sessions on your Linux system.

Tmux, short for "Terminal Multiplexer", is a powerful tool that allows you to create, access, and switch between multiple terminal sessions within a single window. This can be particularly useful when working on complex tasks that require you to have multiple terminal windows open simultaneously.

To install tmux, run the following command:

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

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libevent-2.1-7 libncurses6 libncursesw6 libx11-6 libxcb1 libxdmcp6 libxext6 libxmuu1
Suggested packages:
  xdg-utils
The following NEW packages will be installed:
  libevent-2.1-7 libncurses6 libncursesw6 libx11-6 libxcb1 libxdmcp6 libxext6 libxmuu1 tmux
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.

Now that you have tmux installed, let's explore some basic commands to get started.

To start a new tmux session, simply run the tmux command:

tmux

This will create a new tmux session and you will see a status bar at the bottom of the terminal window.

Example output:

[No output]

To detach from the current tmux session, press Ctrl+b followed by d. This will leave the session running in the background, allowing you to return to it later.

To list all the running tmux sessions, use the following command:

tmux ls

Example output:

0: 1 windows (created Tue Apr 18 15:45:49 2023) [80x24]

This shows that you have one running tmux session.

In this step, you will learn how to navigate and manage tmux sessions, including creating new sessions, switching between sessions, and closing sessions.

First, let's create a new tmux session:

tmux new -s my-session

This will create a new tmux session named "my-session". You can now see the new session when you run tmux ls:

my-session: 1 windows (created Tue Apr 18 16:01:23 2023) [80x24]

To switch between tmux sessions, use the following commands:

  • tmux switch -t my-session: Switch to the "my-session" session.
  • tmux a -t my-session: Attach to the "my-session" session.

To create a new window within the current tmux session, press Ctrl+b followed by c.

To switch between windows, use the following commands:

  • Ctrl+b followed by p: Switch to the previous window.
  • Ctrl+b followed by n: Switch to the next window.
  • Ctrl+b followed by w: See a list of all windows and select one.

To close the current tmux session, press Ctrl+b followed by d to detach from the session. You can then reattach to the session later using tmux a -t my-session.

To kill a tmux session, use the following command:

tmux kill-session -t my-session

This will terminate the "my-session" session.

Customizing tmux with Configuration Files

In this step, you will learn how to customize your tmux environment by creating and modifying a tmux configuration file.

Tmux allows you to customize various aspects of its behavior, such as key bindings, window and pane management, and more, through a configuration file.

First, let's create the tmux configuration file:

nano ~/.tmux.conf

This will open the nano text editor and create a new file named .tmux.conf in your home directory.

In the configuration file, you can add the following lines to customize the tmux behavior:

## Set the prefix key to Ctrl+a instead of the default Ctrl+b
set -g prefix C-a
unbind C-b
bind-key C-a send-prefix

## Start window and pane numbering at 1 instead of 0
set -g base-index 1
setw -g pane-base-index 1

## Enable mouse support
set -g mouse on

## Set the default terminal mode to 256color mode
set -g default-terminal "screen-256color"

These settings will change the prefix key to Ctrl+a, start window and pane numbering at 1, enable mouse support, and set the default terminal mode to 256-color.

Save the file and exit the nano editor.

Now, to apply the changes, you need to reload the tmux configuration:

tmux source-file ~/.tmux.conf

You can now test the new settings by creating a new tmux session and trying out the new key bindings and configurations.

Summary

In this lab, you learned about the tmux command-line tool and how it can help you manage and control multiple terminal sessions on your Linux system. You installed tmux and explored basic commands to start, detach, and list running tmux sessions. You also learned how to navigate and manage tmux sessions, including creating new sessions, switching between sessions, and closing sessions. Finally, you customized your tmux configuration by modifying the configuration file to personalize your tmux experience.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like