cobanov
2023-12-23

Introduction to TMUX

tmux is a terminal multiplexer that we use especially when managing long-running or multiple terminal sessions. This tool allows users to manage multiple terminal sessions within a single interface.

It’s particularly useful when you need to focus on multiple tasks simultaneously, greatly streamlining your workflow.

An important feature is that tmux sessions are preserved even if the connection is lost. This means that when working remotely with a server, if your connection drops, your tmux session continues running in the background, and when you reconnect, you can resume exactly where you left off. This is extremely useful, especially when dealing with unstable internet connections or running long-duration commands.

tmux screen

Why would I need tmux?

tmux works like a powerful, flexible, and resilient control center for your command-line tasks, especially when managing remote sessions or switching between multiple tasks in a terminal environment.

Installing tmux

Source: https://github.com/tmux/tmux/wiki/Installing

╔════════════════════════╦═════════════════════╗
║        Platform        ║   Install Command   ║
╠════════════════════════╬═════════════════════╣
║ Arch Linux             ║ pacman -S tmux      ║
║ Debian or Ubuntu       ║ apt install tmux    ║
║ Fedora                 ║ dnf install tmux    ║
║ RHEL or CentOS         ║ yum install tmux    ║
║ macOS (using Homebrew) ║ brew install tmux   ║
║ macOS (using MacPorts) ║ port install tmux   ║
║ openSUSE               ║ zypper install tmux ║
╚════════════════════════╩═════════════════════╝

Getting started with tmux

To start using tmux, type tmux in your terminal. This command starts a tmux server and creates a default session (number 0).

tmux

To detach from a tmux session, press Ctrl+B followed by D (detach). tmux uses a series of key bindings (keyboard shortcuts) that are triggered by pressing a “prefix” combination. By default, the prefix is Ctrl+B. Then, press D (detach) to detach from the current session.

~ tmux ls # 0: 1 windows (created Thu Nov 30 20:16:45 2023)

You can rename an already opened session using the following command:

# tmux rename -t <target_session> <new_name>
~ tmux rename -t 0 cobanov

At this point, you can disconnect your SSH connection and the command will continue running. You can reconnect to the existing tmux session whenever you want and continue where you left off:

# tmux a -t <session_name>
~ tmux attach -t cobanov

Voilà! Everything continues exactly where it was.

Managing Screens

Just as you have windows in a desktop environment, you have panes in tmux. Like windows, these panes allow you to interact with multiple applications and can similarly be opened, closed, resized, and moved.

Unlike a standard desktop environment, these panes are tiled across the entire terminal and are mostly managed through tmux shortcuts (although mouse functionality can be added). To create a new pane, you split the screen either horizontally or vertically.

Vertical Screen Split

Ctrl+B %

Horizontal Screen Split

Ctrl+B "

Switching Between Screens

Ctrl+B [arrow key]

To see all the shortcut keys in tmux, simply use the bind-key ? command, which in my case would be Ctrl+B ?.

Further Reading & Sources

  1. https://github.com/tmux/tmux
  2. https://gist.github.com/MohamedAlaa/2961058
  3. https://leanpub.com/the-tao-of-tmux/read
  4. https://medium.com/pragmatic-programmers/a-beginners-guide-to-tmux-7e6daa5c0154