LinuxCommandLibrary

tmux

Maintain terminal sessions persistently

TLDR

Start a new session

$ tmux
copy

Start a new named [s]ession
$ tmux [[new|new-session]] -s [name]
copy

List existing sessions
$ tmux [[ls|list-sessions]]
copy

Attach to the most recently used session
$ tmux [[a|attach]]
copy

Detach from the current session (inside a tmux session)
$ <Ctrl b><d>
copy

Create a new window (inside a tmux session)
$ <Ctrl b><c>
copy

Switch between sessions and windows (inside a tmux session)
$ <Ctrl b><w>
copy

Kill a session by [t]arget name
$ tmux kill-session -t [name]
copy

SYNOPSIS

tmux [options] [command [flags]]

PARAMETERS

-2
    Force 256-colour terminal mode, overriding any terminfo capabilities to ensure richer color support.

-8
    Force 88-colour terminal mode, overriding any terminfo capabilities. Less common than -2.

-c shell-command
    Specify an alternative shell command to be used for new windows and panes, overriding the default shell.

-f file
    Specify an alternative configuration file to load instead of the default ~/.tmux.conf. Useful for custom setups.

-L socket-name
    Specify a different socket name for the server. This allows running multiple independent tmux servers concurrently.

-S socket-path
    Specify a full path to the server socket. This option overrides -L for precise socket control.

-l
    Disable the creation of a login shell for new windows and panes, affecting environment variable loading.

-r
    Force a refresh of the client. Useful for resolving display issues or redrawing the terminal content.

-u
    Unbind all key bindings when starting tmux. Primarily used for debugging configuration issues.

-v
    Enable verbose logging to standard error. Provides detailed output for troubleshooting.

DESCRIPTION

tmux (Terminal MUltipleXer) is a powerful utility that allows users to create, manage, and switch between multiple independent terminal sessions from a single window. It operates on a client-server model, enabling sessions to persist even after the user disconnects or the network connection drops. This persistence makes it invaluable for long-running processes on remote servers or during unstable network conditions, as you can detach from a session and reattach later from the same or a different terminal.

Beyond persistence, tmux facilitates an efficient workflow by allowing the division of a single terminal into multiple 'windows' and 'panes'. Windows can host different applications or tasks, while panes further subdivide a window for simultaneous viewing of command outputs, logs, or code. Sessions can also be shared, allowing multiple users to attach to and interact with the same terminal environment, making it a great tool for collaboration and pair programming.

CAVEATS

Mastering tmux typically involves a learning curve, particularly for its default keybindings and the syntax of its configuration file (~/.tmux.conf). While highly flexible, complex configurations can sometimes interact unexpectedly with specific terminal emulators or other applications. It's crucial to remember that detaching from a tmux session does not terminate the processes running within it; they continue in the background until the session is explicitly killed or the tmux server stops.

KEY CONCEPTS

tmux operates with a hierarchical structure: a server hosts multiple sessions. Each session can contain multiple windows, and each window can be further divided into multiple rectangular panes. Users interact with these components using tmux commands.

PREFIX KEY

Most tmux commands are initiated by pressing a 'prefix key' combination, followed by another key. The default prefix key is C-b (Control + b). For example, to create a new window, you would type C-b c; to split a pane vertically, C-b %.

CONFIGURATION FILE

tmux is highly customizable through its configuration file, which is typically located at ~/.tmux.conf. This file allows users to remap keybindings, set options for sessions, windows, and panes, and automate common tasks using shell commands or scripts.

COMMON SUBCOMMANDS

Beyond the top-level options, tmux provides numerous subcommands for daily use:
new-session (or new): Create and attach to a new session.
attach-session (or attach): Attach to an existing session.
list-sessions (or ls): List all active sessions.
kill-session: Terminate a specific session.
kill-server: Terminate all tmux sessions and the server.

HISTORY

tmux was created by Nicholas Marriott and first released in 2007. It rapidly gained popularity as a more modern, open-source alternative to GNU Screen, aiming to provide a cleaner codebase, a more robust client-server architecture, and enhanced extensibility. Over the years, tmux has benefited from active development, focusing on performance, flexibility, and a rich set of features, cementing its position as a preferred terminal multiplexer for developers and system administrators.

SEE ALSO

screen(1), byobu(1), ssh(1), bash(1), zsh(1)

Copied to clipboard