Skip to main content

Tmux Usage

Purpose of Tmux

(1) It allows access to multiple sessions within a single window, which is useful for running multiple command-line programs simultaneously.

(2) It enables new windows to "attach" to existing sessions.

(3) It supports multiple connected windows per session, allowing real-time session sharing among multiple users.

(4) It also supports arbitrary vertical and horizontal splitting of windows.

Basic Usage of Tmux

Installation of Tmux

# Ubuntu or Debian
sudo apt-get install tmux

# CentOS or Fedora
sudo yum install tmux

# Mac
brew install tmux

Starting and Exiting

Enter the tmux command to enter the Tmux window.

tmux

The above command will start the Tmux window with a status bar at the bottom. The left side of the status bar shows window information (number and name), while the right side shows system information.

Step 1

Press Ctrl+d or explicitly type exit to exit the Tmux window.

exit

Prefix Key

All Tmux shortcuts require a prefix key to be activated. The default prefix key is Ctrl+b. For example, the help command shortcut is Ctrl+b ?. In the Tmux window, first press Ctrl+b, then press ? to display help information. Press ESC or q to exit the help.

Session Management

Creating a New Session

The first started Tmux window is numbered 0, the second is 1, and so on. These windows correspond to sessions named 0, 1, etc. Using numbers to distinguish sessions is not intuitive; a better method is to name sessions.

tmux new -s <session-name>

Step 1

Detaching from a Session

In the Tmux window, press Ctrl+b d or input tmux detach to detach the current session from the window. After executing the command, you will exit the current Tmux window, but the session and its processes will continue to run in the background.

tmux detach

Use tmux ls to view all current Tmux sessions.

tmux ls
# or
tmux list-session

Step 1

Attaching to a Session

Use tmux attach to re-attach to an existing session.

Using session number
tmux attach -t 0

Using session name
tmux attach -t <session-name>

Killing a Session

Use tmux kill-session to terminate a session.

Using session number
tmux kill-session -t 0

Using session name
tmux kill-session -t <session-name>

Switching Sessions

Use tmux switch to switch between sessions.

Using session number
tmux switch -t 0

Using session name
tmux switch -t <session-name>

Renaming a Session

Use tmux rename-session to rename a session.

tmux rename-session -t <old-name> <new-name>

Minimal Operation Flow

  1. Create a new session
tmux new -s my_session
  1. Run the required program in the Tmux window.

  2. Press the shortcut Ctrl+b d to detach the session.

  3. Reconnect to the session next time.

tmux attach-session -t my_session

Dividing Panes

Divide into top and bottom panes

tmux split-window

Divide into left and right panes

tmux split-window -h

step1