Tmux (Terminal Multiplexer) allows you to run multiple terminal sessions inside a single window. It also keeps processes running in the background even if you disconnect from SSH.
Installation
# Debian/Ubuntu
sudo apt install tmux
# RHEL / CentOS / Fedora
sudo dnf install tmux
# Arch Linux
sudo pacman -S tmux
# macOS
brew install tmuxSession Management
Run these commands in your regular terminal.
tmux # Start a new session
tmux new -s mysession # Start a new session with name "mysession"
tmux ls # List active sessions
tmux a # Attach to the last active session
tmux a -t mysession # Attach to "mysession"
tmux kill-session -t mysession # Kill "mysession"
tmux kill-server # Kill all tmux sessionsPrefix Key
Inside Tmux, all shortcuts start with a prefix key.
By default, the prefix is Ctrl + b.
Window Management (Tabs)
After pressing Ctrl + b:
c: Create a new windoww: List all windows and choose onen: Go to next windowp: Go to previous window0to9: Go to a specific window number,: Rename current window&: Kill current window
Pane Management (Splits)
After pressing Ctrl + b:
%: Split pane vertically (left/right)": Split pane horizontally (top/bottom)Arrow Keys: Switch between panesx: Kill current panez: Toggle pane zoom (fullscreen)Space: Cycle through pane layouts{/}: Swap pane with the next/previous pane
Copy Mode (Scrollback)
Copy mode lets you scroll through terminal history and copy text.
After pressing Ctrl + b:
[: Enter copy mode (use arrow keys orPgUp/PgDnto scroll)q: Exit copy modeSpace: Start selection (in copy mode)Enter: Copy selection and exit copy mode]: Paste the copied text
With set -g mode-keys vi in your config, you can use Vim-style keys (hjkl, /search) in copy mode.
Scripting and Automation
# Send a command to a running tmux session from outside
tmux send-keys -t mysession "echo hello" Enter
# Create a new session with pre-configured windows
tmux new-session -d -s dev -n editor
tmux send-keys -t dev:editor "nvim" Enter
tmux new-window -t dev -n server
tmux send-keys -t dev:server "npm run dev" Enter
tmux attach -t devConfiguration (~/.tmux.conf)
You can customize the prefix and appearance in ~/.tmux.conf. After changing it, reload with tmux source-file ~/.tmux.conf.
# Change prefix to Ctrl + a (like GNU Screen)
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# Enable mouse support (scroll, click, resize panes)
set -g mouse on
# Use Vim-style keys in copy mode
set -g mode-keys vi
# Start window numbering from 1 instead of 0
set -g base-index 1
setw -g pane-base-index 1