How to Use tmux in linux with Examples

How to Use tmux in linux with Examples

Tmux (Terminal Multiplexer) in Linux is a powerful tool that allows you to manage multiple terminal sessions, windows, and panes within a single terminal. It is particularly useful for developers and system administrators who need to work with multiple terminal sessions simultaneously.

In this article, we will cover the basics of Tmux, including its installation, creating and managing sessions, working with windows and panes, and customizing Tmux to suit your workflow.


What is tmux?

Tmux, short for Terminal Multiplexer, is a command-line tool that allows users to manage multiple terminal sessions within a single terminal window. It is designed to enhance the productivity and workflow of developers and system administrators by providing a powerful set of features to manage and manipulate multiple terminal windows and panes.

Tmux works by creating multiple sessions, each of which can contain multiple windows, and each window can contain multiple panes. This allows users to switch between different sessions, windows, and panes within a single terminal window, without having to open multiple terminal windows or tabs.

Some of the key features of Tmux include:

  • Multiple sessions: Tmux allows users to create and manage multiple sessions within a single terminal window, making it easy to switch between different tasks or projects.
  • Multiple windows: Each Tmux session can contain multiple windows, which can be thought of as tabs in a web browser or text editor. This allows users to keep multiple tasks or projects open and organized within a single terminal window.
  • Multiple panes: Each Tmux window can contain multiple panes, which can be thought of as split screens within a window. This allows users to view and interact with multiple terminal sessions or commands within a single window.
  • Detachable and re-attachable sessions: Tmux sessions can be detached from and re-attached to as needed, making it easy to keep terminal sessions running even when disconnected from a server or machine.
  • Customizable status bar: Tmux includes a customizable status bar that can be used to display information about the current session, window, or pane.

Overall, Tmux is a powerful tool that can greatly enhance the productivity and workflow of developers and system administrators, by allowing them to manage and manipulate multiple terminal sessions within a single terminal window.


How to Install Tmux in Linux

Before we get started with Tmux, we need to install it on our system. Tmux is available in most Linux distributions’ package repositories and can be installed using the package manager.

Installing Tmux on Ubuntu and Debian

To install Tmux on Ubuntu or Debian, use the following command:

sudo apt install tmux

Installing Tmux on CentOS and Fedora

To install Tmux on CentOS or Fedora, use the following command:

sudo yum install tmux

Installing Tmux on macOS

To install Tmux on macOS, you can use the Homebrew package manager:

brew install tmux

Basic Tmux Commands

Basic Tmux Commands:

Once Tmux is installed, you can start using it by running the tmux command. This will create a new session with a single window. Here are some basic commands to get you started:

  • tmux new-session: Create a new Tmux session.
  • tmux attach-session: Attach to an existing Tmux session.
  • tmux list-sessions: List all Tmux sessions.
  • tmux detach: Detach from the current Tmux session.
  • tmux kill-session: Kill the current Tmux session.

How to Start Your First Tmux Session in Linux

To start a new Tmux session, simply type tmux in your terminal:

tmux

This will create a new Tmux session with a single window.


How to Create Named Tmux Sessions In Linux

Naming your Tmux sessions can be helpful when you have multiple sessions open at the same time. To create a new named Tmux session, use the -s option followed by the name of your session:

tmux new-session -s mysession

How to Detach from Tmux Session

To detach from a Tmux session, use the Ctrl-b d shortcut or the tmux detach command:

Ctrl-b d

Or

tmux detach

How to Re-attach to Tmux Session

To re-attach to a Tmux session, use the tmux attach-session command followed by the session ID or name:

tmux attach-session -t mysession

How to Work with Tmux Windows and Panes

Tmux allows you to create multiple windows within a session, each with its own set of panes. To create a new window, use the Ctrl-b c shortcut:

Ctrl-b c

To move between windows, use the Ctrl-b n shortcut to move to the next window and the Ctrl-b p shortcut to move to the previous window:

Ctrl-b n
Ctrl-b p

To split the current pane vertically, use the Ctrl-b % shortcut, and to split it horizontally, use the Ctrl-b " shortcut:

Ctrl-b %
Ctrl-b "

To move between panes, use the Ctrl-b arrow keys shortcut:

Ctrl-b Up Arrow
Ctrl-b Down Arrow
Ctrl-b Left Arrow
Ctrl-b Right Arrow

How to Customize Tmux

Tmux can be customized to suit your workflow. The Tmux configuration file is located at ~/.tmux.conf. Here are some basic configurations that you can add to your Tmux configuration file:

To change the prefix key from Ctrl-b to Ctrl-a, add the following line to your ~/.tmux.conf file:

set-option -g prefix C-a

This line tells Tmux to use Ctrl-a as the prefix key instead of Ctrl-b. Once this line has been added to the ~/.tmux.conf file and the file has been saved, users can reload Tmux with the new prefix key by running the following command:

tmux source-file ~/.tmux.conf

After this command is run, the prefix key for Tmux should be changed to Ctrl-a, and users can start using this key combination to enter Tmux commands and shortcuts.

To enable mouse support, add the following line to your ~/.tmux.conf file:

set -g mouse on

To set the default window index to 1 instead of 0, add the following line to your ~/.tmux.conf file:

set -g base-index 1

To set the default pane index to 1 instead of 0, add the following line to your ~/.tmux.conf file:

set -g pane-base-index 1

To customize the Tmux status bar, add the following line to your ~/.tmux.conf file:

set -g status-right "Session: #S"

This will display the session name on the right side of the Tmux status bar.


Basic Tmux Commands and Shortcuts

Here are some basic Tmux commands and shortcuts that you can use to get started:

  • tmux new-session: Start a new Tmux session
  • tmux new-session -s mysession: Start a new Tmux session with a name
  • tmux attach-session: Attach to the last active Tmux session
  • tmux attach-session -t mysession: Attach to a named Tmux session
  • tmux detach: Detach from the current Tmux session
  • Ctrl-b d: Detach from the current Tmux session
  • Ctrl-b c: Create a new Tmux window
  • Ctrl-b n: Move to the next Tmux window
  • Ctrl-b p: Move to the previous Tmux window
  • Ctrl-b %: Split the current Tmux pane vertically
  • Ctrl-b ": Split the current Tmux pane horizontally
  • Ctrl-b Up Arrow: Move to the pane above the current pane
  • Ctrl-b Down Arrow: Move to the pane below the current pane
  • Ctrl-b Left Arrow: Move to the pane on the left of the current pane
  • Ctrl-b Right Arrow: Move to the pane on the right of the current pane

Conclusion

Tmux is a powerful tool that can help you manage multiple terminal sessions, windows, and panes within a single terminal. By learning the basics of Tmux, you can improve your productivity and workflow as a developer or system administrator. With the commands and shortcuts covered in this article, you should have a good understanding of how to get started with Tmux and customize it to suit your needs.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top