Chapter 26: Learning tmux

In this chapter, we’ll explore tmux, a terminal multiplexer that allows you to manage multiple terminal sessions within a single window. By the end of this chapter, you’ll be able to create, manage, and customize tmux sessions, panes, and windows, making your workflow more efficient and organized.


1. Why Learn tmux?

tmux is a powerful tool for: 
- Multitasking: Run multiple terminal sessions in one window. 
- Session Management: Save and restore terminal sessions. 
- Remote Work: Keep processes running even if your connection drops. 
- Customization: Tailor tmux to your workflow with keybindings and plugins.


2. Getting Started with tmux

Installing tmux

Most Linux distributions include tmux in their package repositories. Install it using your package manager: - Debian/Ubuntu:
$ sudo apt install tmux 
Red Hat/Fedora: $ sudo dnf install tmux

Starting a tmux Session

To start a new tmux session:

$ tmux

This opens a new session with a single window.

Detaching and Reattaching

  • Detach from a session (leaves it running in the background): bash Ctrl-b d
  • Reattach to a session: bash $ tmux attach

3. Managing tmux Sessions

Creating Named Sessions

Create a named session for easier management:

$ tmux new -s session_name

Listing Sessions

View all active tmux sessions:

$ tmux ls

Switching Between Sessions

Switch to another session:

$ tmux switch -t session_name

Killing a Session

End a tmux session:

$ tmux kill-session -t session_name

4. Working with Windows and Panes

Creating Windows

  • New Window: bash Ctrl-b c
  • Rename Window: bash Ctrl-b ,

Navigating Windows

  • Next Window: bash Ctrl-b n
  • Previous Window: bash Ctrl-b p

Splitting Panes

  • Horizontal Split: bash Ctrl-b "
  • Vertical Split: bash Ctrl-b %

Navigating Panes

  • Switch Panes: bash Ctrl-b o
  • Resize Panes: bash Ctrl-b Alt-arrow_key

5. Customizing tmux

Configuration File

Customize tmux by editing its configuration file (~/.tmux.conf):

$ nano ~/.tmux.conf

Example Customizations

  • Change the prefix key from Ctrl-b to Ctrl-a
    $ unbind C-b set-option -g prefix C-a bind C-a send-prefix
  • Enable mouse support: $ set -g mouse on

Reloading Configuration

After editing the config file, reload it:

$ tmux source-file ~/.tmux.conf

6. Practical Examples

Running Multiple Commands

Use panes to run multiple commands simultaneously: 1. Split the window into two panes. 2. Run a command in one pane and monitor logs in the other.

Remote Work

Start a tmux session on a remote server to keep processes running even if your connection drops.

Custom Workflow

Create a custom tmux setup for your daily tasks, such as coding, monitoring, and debugging.


7. Practice Time!

Let’s put your new skills to the test: 1. Start a new tmux session and create two windows. 2. Split one window into multiple panes and run different commands in each. 3. Detach from the session and reattach to it. 4. Customize your tmux configuration to enable mouse support and change the prefix key.


tmux revolutionizes terminal usage by allowing multiple windows and panes within a single session, with the ability to detach and reattach at will. The tmux plugin manager (tpm) extends functionality, enabling features like clipboard synchronization and status bar enhancements. 

Scripting tmux sessions allows for automated setups, recreating consistent layouts with a single command. By mastering tmux and its plugins, you can efficiently manage multiple tasks and maintain a predictable terminal environment.

$ git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

8. Conclusion

tmux is a versatile tool that enhances your productivity by allowing you to manage multiple terminal sessions efficiently. By mastering tmux, you can streamline your workflow, work more effectively on remote systems, and customize your terminal environment to suit your needs.


That’s it for Chapter 26 ! You’ve now learned how to use tmux to manage multiple terminal sessions, windows, and panes. With this knowledge, you can organize your workflow, automate tasks, and work more efficiently in the terminal.


Prev: Chapter 25 | Next: Chapter 27