tmux is a terminal multiplexer that lets you run multiple terminal sessions within a single window, detach from them, and reattach later.
Here’s how to get it set up and running:
Installation
On most Linux and macOS systems, you can install tmux using your system’s package manager.
Debian/Ubuntu:
sudo apt update
sudo apt install tmux
Fedora/CentOS/RHEL:
sudo dnf install tmux
# or
sudo yum install tmux
macOS (using Homebrew):
brew install tmux
Once installed, you can launch tmux by simply typing tmux in your terminal and pressing Enter. You’ll see a status bar appear at the bottom of your screen, indicating you’re now inside a tmux session.
Basic Navigation and Commands
tmux commands are typically prefixed by a "prefix key" followed by another key. The default prefix key is Ctrl+b.
Ctrl+bthenc: Create a new window (like a new tab).Ctrl+bthenn: Switch to the next window.Ctrl+bthenp: Switch to the previous window.Ctrl+bthen0to9: Switch to a specific window by its number.Ctrl+bthen&: Kill the current window.Ctrl+bthen%: Split the current pane vertically (creating two side-by-side panes).Ctrl+bthen": Split the current pane horizontally (creating two stacked panes).Ctrl+bthenArrow Key: Navigate between panes.Ctrl+bthenx: Kill the current pane.Ctrl+bthend: Detach from the currenttmuxsession. The session continues running in the background.
To reattach to a detached session, type tmux attach or tmux a. If you have multiple sessions, you can list them with tmux ls and attach to a specific one using tmux attach -t <session_name>.
Configuration: .tmux.conf
The real power of tmux comes from customization. You can create a configuration file at ~/.tmux.conf to change keybindings, colors, and behavior.
Let’s set up a more convenient prefix and some common settings.
Create or edit ~/.tmux.conf and add the following:
# Change the prefix key to Ctrl+a
set -g prefix C-a
unbind C-b
# Set the window and pane base index to 1
set -g base-index 1
setw -g pane-base-index 1
# Enable mouse mode for easier pane selection and scrolling
set -g mouse on
# Set up keybindings for window splitting and pane navigation
# Split pane horizontally
bind '-' split-window -v
# Split pane vertically
bind '|' split-window -h
# Navigate panes with arrow keys
bind -r h select-pane -L
bind -r j select-pane -D
bind -r k select-pane -U
bind -r l select-pane -R
# Resize panes with Ctrl+Arrow Keys (using the new prefix C-a)
bind C-h resize-pane -L 5
bind C-j resize-pane -D 5
bind C-k resize-pane -U 5
bind C-l resize-pane -R 5
# Reload the config file
bind r source-file ~/.tmux.conf \; display "Reloaded!"
# Set status bar colors (optional, for a nicer look)
set -g status-bg 'colour235'
set -g status-off on
After saving ~/.tmux.conf, you need to reload the tmux configuration. If you’re already in a tmux session, press your new prefix (Ctrl+a) then r. If you’re not in a session, you can start a new one with tmux and then press Ctrl+a then r.
With this configuration:
- Your prefix is now
Ctrl+a. - You can split panes with
Ctrl+athen-(horizontal) orCtrl+athen|(vertical). - You can navigate between panes using
Ctrl+athenh,j,k, orl. - You can resize panes using
Ctrl+athenCtrl+h,j,k, orl. - Mouse mode is enabled, allowing you to click on panes to select them and scroll with your mouse wheel.
This setup provides a solid foundation for using tmux efficiently for managing multiple terminal tasks.
The next step is to explore session management and more advanced pane/window manipulation.