A Tutorial for Tmux Beginners

A Tutorial for Tmux Beginners

Tmux is a very productive and versatile terminal multiplexer released in 2007. Its design stems from GNU Screen, the prototypical terminal multiplexer first released in 1987.

Why Tmux?

A typical application of Tmux is remote work. Imagine that you ssh to your office machine and open a remote terminal to run programs. The SSH connection is suddenly broken for some reasons, e.g. poor signal, power cut, etc. As a result, all running processes are lost as the terminal session is closed.

Tmux is capable of solving such problems. It allows you to “detach” a running session from a window. In addition, it has many useful functionalities:

  • It allows multiple sessions in a single window. This is useful for running multiple command-line programs at the same time.
  • It allows new windows to “plug in” to existing sessions.
  • It allows multiple windows per session, so different people can share sessions at the same time.
  • It can split a window horizontally or vertically.

Installation

To install Tmux on Ubuntu and Debian, use:

1
sudo apt install tmux

Tmux can be installed on MacOS using Homebrew:

1
brew install tmux

Or you can build and compile Tmux from the developping head, using:

1
2
3
git clone https://github.com/tmux/tmux.git
cd tmux && sh autogen.sh
./configure && make

Basic usage

Starting a Tmux session

Simply type tmux in your console:

1
tmux

This will open a new session, create a new window, and start a shell in that window. Tmux sessions created in this way have digit session names (0, 1, 2, …) by default. To create a session with a personalized <session_name>, simply run tmux command with the -s option:

1
tmux new -s <session_name>

Detaching from a Session

Press Ctrl-b d or type tmux detach to detach from the Tmux session and return to your normal shell. The program running in the Tmux session will continue to run after you detach from the session.

Listing all running sessions

To find to a (detached) session, you need to list the name of all sessions. This is done by:

1
tmux ls

The output is something like the following:

1
2
0: 1 windows (created Thu May 25 14:40:22 2023) (attached)
my-session: 1 windows (created Thu May 25 17:19:28 2023)

in which the session names are in the first column (0 and my-session in this case). An alternative way of listing all sessions is using the hotkey combo Ctrl-b s.

Re-attaching to a Session

To attach to session my-session, for example, type:

1
tmux attach-session -t my-session

Killing a session

Use the kill-session with option -t to do the job:

1
tmux kill-session -t <session_name>

Switching between sessions

1
tmux switch -t <session_name>

Renaming a session

1
tmux rename-session -t <session_name> <new_name>

Alternatively, you can press the hotkey combo Ctrl-b $ to rename the current session.

Working with Tmux Windows and Panes

When you start a new Tmux session, by default, it creates a single window with a shell in it. To create a new window with shell type Ctrl-b c, the first available number from the range 0…9 will be assigned to it. A list of all windows is shown on the status line at the bottom of the screen.

Below are some most common commands for managing Tmux windows and panes:

  • Ctrl-b % Split current pane horizontally into two panes
  • Ctrl-b " Split current pane vertically into two panes
  • Ctrl-b o Go to the next pane
  • Ctrl-b ; Toggle between the current and previous pane
  • Ctrl-b x Close the current pane
  • Ctrl-b c Create a new window (with shell)
  • Ctrl-b w Choose window from a list
  • Ctrl-b 0 Switch to window 0 (by number )
  • Ctrl-b , Rename the current window

Other commands

1
2
3
4
5
6
7
8
9
10
11
# List all Tmux commands
tmux list-commands

# List all shortcuts and the corresponding Tmux commands
tmux list-keys

# Print info about the all Tmux sessions
tmux info

# Reload the Tmux configuration file
tmux source-file ~/.tmux.conf

And that’s all for the tutorial. Please feel free to leave comments in case of any question.

References

Comments