Home > Environment & Utility > tmux

tmux: Terminal Multiplexer

tmux is a terminal multiplexer that allows you to manage terminal sessions and use multiple terminal windows on a single screen. It is particularly useful for remote work as sessions persist even if the SSH connection is lost, and it helps you perform multiple tasks concurrently within a single terminal.

Overview

tmux enables users to create, detach, and reattach terminal sessions, ensuring that work is not interrupted even if the network connection drops. It also significantly enhances productivity by allowing management of multiple windows and split panes within a single terminal window.

Key Features

  • Session Persistence and Reattachment: Your work sessions remain on the server even if the network connection is lost.
  • Multiple Windows and Panes: Concurrent work is possible through multiple virtual windows and screen splits within a single terminal.
  • Collaboration Facilitation: Multiple users can connect to the same tmux session to collaborate.
  • Customizable: Various settings such as keybindings and status bars can be modified via the `.tmux.conf` file.

Key Options

These are the main options used with the tmux command.

Session Management

Naming Sessions/Windows

Generated command:

Try combining the commands.

Description:

`tmux` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various examples of managing terminal sessions using tmux.

Start a New tmux Session

tmux

If no name is specified, a name is automatically assigned.

Start a New Session with a Name

tmux new -s my_session

Starts a new tmux session named `my_session`.

Attach to an Existing Session

tmux attach -t my_session

Reattaches to the session named `my_session`.

Detach from the Current Session

tmux detach

This has the same effect as pressing `Ctrl+b d` within a tmux session. (Not a command to be executed directly in the terminal)

View All tmux Session List

tmux ls

Displays all currently running tmux sessions and their status.

Kill a Specific Session

tmux kill-session -t my_session

Terminates the session named `my_session`.

Installation

tmux is not included by default in most Linux distributions and needs to be installed using the following commands.

Debian/Ubuntu

sudo apt update && sudo apt install tmux

Installs tmux using the APT package manager.

CentOS/RHEL/Fedora

sudo dnf install tmux
# or sudo yum install tmux (older versions)

Installs tmux using the YUM or DNF package manager.

Arch Linux

sudo pacman -S tmux

Installs tmux using the Pacman package manager.

Tips & Notes

Tips and notes for using tmux more efficiently.

Default Prefix Key

All tmux commands are executed by first pressing the `Ctrl+b` prefix key, followed by another key. This prefix key can be changed in the `.tmux.conf` file.

  • Ctrl+b d: Detach from the current session
  • Ctrl+b %: Split the current pane vertically
  • Ctrl+b ": Split the current pane horizontally
  • Ctrl+b c: Create a new window
  • Ctrl+b n: Move to the next window
  • Ctrl+b p: Move to the previous window
  • Ctrl+b x: Close the current pane (with confirmation)
  • Ctrl+b [: Enter copy mode (scroll up)
  • Ctrl+b ]: Exit copy mode

`.tmux.conf` File

You can customize tmux settings by editing the `.tmux.conf` file in your home directory. For example, you can change the prefix key to `Ctrl+a` or configure the status bar's colors and displayed information. To apply changes, run `tmux source-file ~/.tmux.conf` or restart your tmux session.

Simple `.tmux.conf` Example

# Change prefix key to Ctrl+a
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Enable mouse scrolling
set -g mouse on

An example that changes the prefix key to `Ctrl+a` and enables mouse scrolling.


Same category commands