Oct 14, 2023 5 min read

Getting Started with Tmux

Learn to use tmux with our step-by-step tutorial. It is a powerful terminal multiplexer for Unix-like operating systems.

Getting Started with Tmux
Table of Contents

Introduction

Tmux is a powerful terminal multiplexer for Unix-like operating systems. It allows users to create multiple terminal sessions within a single window or manage multiple windows and panes, enabling efficient multitasking and remote session management. Tmux enhances productivity by providing features like session persistence, detaching and reattaching sessions, and session sharing.

To get you up and going, this tutorial will walk you through the installation and basic usage of Tmux.

Advantages

  1. Multiple Terminal Sessions: Tmux allows you to create and manage multiple terminal sessions within a single window. This means you can work on multiple tasks or projects simultaneously without the need for multiple terminal windows or SSH connections.
  2. Session Persistence: Tmux provides session persistence, which means you can detach from a session and reattach later without losing your work. This is especially useful when working on long-running processes or when you need to disconnect from a remote session while keeping it intact.
  3. Window and Pane Management: Tmux allows you to split windows into multiple panes, giving you the ability to view and work on different tasks within a single terminal window. You can easily switch between windows and panes, resize them, and rearrange them according to your workflow.
  4. Remote Session Sharing: Tmux offers the functionality to share terminal sessions with other users. This can be helpful for pair programming, collaboration, or remote troubleshooting, as multiple users can view and interact with the same terminal session simultaneously.
  5. Customization and Scripting: Tmux is highly customizable, allowing you to customize key bindings, colors, status bar appearance, and much more. Additionally, Tmux has a robust scripting interface, enabling you to automate and script complex workflows, customize behavior based on conditions, and create personalized shortcuts.

Installing Tmux

Tmux may be readily installed using your distro's package manager.

Installing Tmux on Ubuntu and Debian

sudo apt install tmux

Installing Tmux on CentOS and Fedora

sudo yum install tmux

Installing Tmux on macOS

brew install tmux

Starting Your First Tmux Session

Simply type tmux into your console to begin your first Tmux session:

tmux

This will start a new session, a new window, and a shell in the new window.

Tmux has a status line at the bottom of the screen that displays information about the current session.

You can now use Tmux to run your first command. For example, you may type the below command to get a list of all instructions.

Ctrl+b ?

Creating Named Tmux Sessions

Tmux sessions are named numerically by default. When running several Tmux sessions, named sessions come in handy. Run the tmux command with the following arguments to create a new named session:

tmux new -s session_name

Choosing a session name that is descriptive is always a smart idea.

Detaching from Tmux Session

You can exit the Tmux session and return to your regular shell by typing:

Ctrl+b d

After you detach from the Tmux session, the program that was executed will continue to operate.

Re-attaching to Tmux Session

To attach to a session, you must first locate the session's name. Type the below command to receive a list of the currently active sessions.

tmux ls

The first column in the output is the name of the session.

Output

0: 1 windows (created Sat Sep 15 09:38:43 2018) [158x35]
my_named_session: 1 windows (created Sat Sep 15 10:13:11 2018) [78x35]

There are two Tmux sessions going, as you can see from the output. The first is referred to as 0 and the second is referred to as my_named_session.

For instance, to join the session 0, you would type:

tmux attach-session -t 0

Working with Tmux Windows and Panes

Tmux opens a single window with a shell in it by default when you start a new session.

The first available number from the range 0...9 will be assigned to a new window created with the shell type Ctrl+b c.

The status line at the bottom of the screen displays a list of all windows.

The following are some of the most common Tmux window and pane management commands:

  • Ctrl+b c - Create a new window (with shell)
  • Ctrl+b w - Choose a window from a list
  • Ctrl+b 0 - Switch to window 0 (by number)
  • Ctrl+b , - Rename the current window
  • Ctrl+b % - Split the current pane into two panes, horizontally
  • Ctrl+b " - Split the current pane into two panes, vertically
  • Ctrl+b o - Go to the next pane
  • Ctrl+b ; - Switch back and forth between the current and previous panes
  • Ctrl+b x - Close the current pane

Customizing Tmux

If the file is present, Tmux reads its configuration parameters from ~/.tmux.conf when it is started.

Here's an example of a ~/.tmux.conf file with a customized status line and a few extra options:

# Improve colors
set -g default-terminal 'screen-256color'

# Set scrollback buffer to 10000
set -g history-limit 10000

# Customize the status line
set -g status-fg  green
set -g status-bg  black

Basic Tmux Usage

The following are the first stages in getting started with Tmux:

  1. Type tmux new -s my_session in the command prompt, then run the appropriate program.
  2. Start the program you want to run.
  3. To exit the session, press Ctrl-b + d on your keyboard.
  4. Type tmux attach-session -t my_session to reconnect to the Tmux session.

FAQs on Tmux

Why would I use Tmux?

Tmux offers various benefits, including the ability to organize and manage multiple terminal sessions, detach and reattach sessions to resume work later, split windows into multiple panes, and share sessions between users.

How do I install Tmux?

Tmux can be installed using package managers like apt, yum, or homebrew. For example, on Debian-based systems, run sudo apt-get install tmux to install Tmux.

How do I start Tmux?

Simply open a terminal and type tmux to start a new Tmux session. Alternatively, you can use the command tmux new-session to explicitly start a new session.

What are Tmux panes?

Tmux supports splitting windows into multiple panes, allowing you to view and interact with multiple terminal sessions simultaneously. You can create panes using commands like Ctrl-b % (vertical split) or Ctrl-b " (horizontal split).

How do I create a new window in Tmux?

Inside a Tmux session, press Ctrl-b c to create a new window. This will open a new terminal window within the existing Tmux session.

How do I switch between windows and panes in Tmux?

To switch between windows, use Ctrl-b n to move to the next window or Ctrl-b p to move to the previous window. To move between panes, use Ctrl-b <arrow key>.

Can I detach and reattach Tmux sessions?

Yes, you can detach a Tmux session by pressing Ctrl-b d. This will allow you to close the terminal window or switch to a different task, and later reattach to the previously detached session using the tmux attach-session command.

Conclusion

You learned how to utilize Tmux in this tutorial. You can now use the .tmux.conf file to create numerous Tmux windows in a single session, split windows by creating new panes, browse between windows, detach and resume sessions, and personalize your Tmux instance.

In your terminal, type man tmux or go to the Tmux User's Manual page to learn more about Tmux.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.