Useful for long-running tasks on a remote server.

For more help see tmuxcheatsheet site or Getting Started with tmux.

Install

$ sudo apt  install tmux  
$ sudo snap install tmux

Usage

Help

tmux --help
usage: tmux [-2CluvV] [-c shell-command] [-f file] [-L socket-name]
            [-S socket-path] [command [flags]]

Manpage

NAME
     tmux — terminal multiplexer

SYNOPSIS
     tmux [-2CluvV] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]]

DESCRIPTION
     tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen.  tmux may be detached from a screen
     and continue running in the background, then later reattached.
...

Commands

Create an unnamed session.

$ tmux

Create a named session:

$ tmux new -s my-name

You’ll get an error if it already exists.

Then you can exit with exit or CTRL+D to end the session.

Or press this sequence to detach from the session and keep it running: CTRL+B and then D,

List sessions:

$ tmux ls

e.g. Unnamed session:

0: 1 windows (created Thu Apr 28 18:34:12 2022)

e.g. Named session:

test: 1 windows (created Thu Apr 28 18:26:26 2022)

From within Tmux, you can also list all sessions and switch between them: CTRL+B then S.

You can cycle through sessions with left and right brackets: CTRL+B then ).

You can attach to a running session:

$ tmux attach -t my-name

It seems to work the same as this, as it requires the session to exist already:

$ tmux new -t my-name

Kill a session:

$ tmux kill-ses -t my-name

Kill all sessions but the current.

$ tmux kill-session -a

Kill all sessions but my-name.

$ tmux kill-session -a -t `my-name`.