tmux Quickstart

First Post:

Last Update:

Word Count:
791

Read Time:
4 min

Page View: loading...


Getting Started with [tmux] for a Powerful Terminal

tmux is a terminal multiplexer, a powerful tool that transforms your terminal into a persistent, multi-window workspace. It’s an essential utility for anyone working on remote servers or managing multiple tasks in the command line. Think of it as adding tabs and session-saving capabilities to your terminal.


Installing [tmux]

Getting tmux is straightforward on most systems.

  1. On macOS (with Homebrew):
    1
    brew install tmux
  2. On Debian / Ubuntu:
    1
    2
    sudo apt update
    sudo apt install tmux
  3. On CentOS / RHEL / Fedora:
    1
    sudo dnf install tmux

Managing Sessions and Windows

tmux commands are triggered by a prefix key, which is Ctrl + b by default. To send a command, you press the prefix, release it, and then press the command key.

Creating and Managing Sessions

A session is a collection of windows that can be detached and re-attached later.

  • Start a new named session:
    1
    tmux new -s my_project
  • Detach from the current session:
    This leaves the session running in the background.
    Press Ctrl + b, then d.

  • List all running sessions:

    1
    tmux ls
  • Re-attach to a session:

    1
    tmux attach -t my_project

Managing Windows (like browser tabs)

Within a session, you can have multiple windows for different tasks.

  • Create a new window:
    Press Ctrl + b, then c.

  • Switch between windows:

    • Ctrl + b, then n (next window)
    • Ctrl + b, then p (previous window)
    • Ctrl + b, then 0 (switch to window 0)
  • Rename the current window:
    Press Ctrl + b, then , (comma).

  • Close the current window:
    Press Ctrl + b, then &.


Browsing Command History and Buffer

Sometimes, the output of a command is too long to fit on one screen. tmux has a special “copy mode” that lets you scroll up and view your history.

  • Enter Copy Mode (Scroll Mode):
    Press Ctrl + b, then [ (left square bracket).

  • Navigate the buffer:
    Once in copy mode, you can use your arrow keys (Up, Down, Left, Right) or Vim-style keys (k for up, j for down) to scroll freely through the entire command output history of that pane.

  • Exit Copy Mode:
    Press q to return to the normal command prompt.


Managing [tmux] from the Outside

You don’t have to be inside a tmux session to manage it. You can use command-line flags to get information or to stop sessions and windows.

  • List all sessions and their windows (detailed view):
    The ls command is your primary tool for this.

    1
    tmux ls
  • Delete a specific session from outside:
    If you want to completely shut down a session and all the processes within it, use kill-session.

    1
    tmux kill-session -t my_project

    Replace my_project with the name or ID of the session you want to delete.

  • Delete a specific window from outside:
    This is less common, but you can target a specific window to close it. You need to specify the target using the format [session_name]:[window_index].

    1
    tmux kill-window -t my_project:1

    this command will kill window 1 in the ‘my_project’ session


[tmux] Structure Relationship

To help you visualize how everything fits together, here is a simple diagram. The tmux server runs in the background. It can manage multiple sessions. Each session acts as a workspace containing one or more windows (like tabs). Finally, each window can be split into one or more panes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

[ tmux Server (background process) ]
|
|-----\> [ Session: project\_A ]
| |
| |-----\> [ Window 0: bash ] --\> (Pane 0: vim)
| |
| |-----\> [ Window 1: logs ] --\> (Pane 0: tail -f)
| | --\> (Pane 1: htop)
| |
| '-----\> [ Window 2: zsh ] --\> (Pane 0: zsh)
|
'-----\> [ Session: project\_B ]
|
|-----\> [ Window 0: main ] --\> (Pane 0: python)
|
'-----\> [ Window 1: db ] --\> (Pane 0: psql)


Key [tmux] Features Explained

What makes tmux so indispensable?

  • Session Persistence: This is the killer feature. When you detach or your SSH connection drops, the tmux server keeps your session and all its running processes alive in the background. You can reconnect later and find everything exactly as you left it.

  • Environment Variable Isolation: When you export an environment variable in one tmux pane or window, it is not automatically available in other existing or new windows/panes. Each new window starts with a clean environment, preventing variables from one task from accidentally affecting another. This ensures a predictable and isolated workspace for each command.

  • Backgrounding: The ability to detach (Ctrl + b, d) and leave tasks running is crucial for long-running processes like code compilation, data processing, or server maintenance. You can start a job, detach, close your local terminal, and re-attach hours later from a different machine to check the progress.

  • Panes and Layouts: Beyond windows, you can split a single window into multiple panes (horizontal: Ctrl + b, "; vertical: Ctrl + b, %) to view logs, edit code, and run commands all at once.


[tmux] makes your terminal persistent and powerful =w=/

If you find this helpful, please give my project a ⭐on GitHub! =w=/