tmux Command

What is Linux tmux Command?

tmux (Terminal Multiplexer) is a powerful command-line tool that allows you to create and manage multiple terminal sessions within a single window. It enables you to run multiple programs simultaneously, switch between them, detach from sessions while keeping them running in the background, and reattach to them later from different locations.

tmux is particularly valuable for remote work, long-running processes, and organizing complex workflows. When you lose your SSH connection, tmux sessions continue running on the server, protecting your work from accidental disconnection.

From the man page:

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.

Key Features

  • Session persistence: Sessions survive network disconnections and system reboots
  • Multiple windows: Create multiple terminal windows within a single session
  • Window splitting: Split windows into multiple panes for parallel work
  • Remote accessibility: Attach to sessions from different locations
  • Scriptable: Can be automated and configured extensively
  • Resource efficient: Lightweight alternative to GUI terminal emulators

tmux Concepts

Sessions

A session is the top-level container that holds one or more windows. Sessions persist even when you disconnect from them.

Windows

Windows are like tabs in a browser - each window can contain one or more panes with different programs running.

Panes

Panes are divisions within a window that allow you to run multiple programs side-by-side in a single window.

tmux Syntax

tmux [command] [options]

Basic usage patterns:

  • tmux - Start a new session
  • tmux new -s session-name - Create named session
  • tmux attach -t session-name - Attach to existing session
  • tmux list-sessions - List all sessions

Installation

Ubuntu/Debian

sudo apt update
sudo apt install tmux

CentOS/RHEL/Fedora

sudo yum install tmux   # CentOS/RHEL
sudo dnf install tmux   # Fedora

macOS

brew install tmux

Basic tmux Usage

Starting tmux

Start a new session:

tmux

Start a named session:

tmux new -s development

Start session with specific window name:

tmux new -s project -n editor

Key Bindings Concept

tmux uses a prefix key followed by command keys. The default prefix is Ctrl+B. All tmux commands start with this prefix.

Example: Ctrl+B then D means:

  1. Press and hold Ctrl+B
  2. Release both keys
  3. Press D

Essential tmux Key Bindings

Session Management

  • Ctrl+B D - Detach from current session
  • Ctrl+B : - Enter command mode
  • Ctrl+B ? - Show help (list all key bindings)

Window Management

  • Ctrl+B C - Create new window
  • Ctrl+B N - Switch to next window
  • Ctrl+B P - Switch to previous window
  • Ctrl+B 0-9 - Switch to window number
  • Ctrl+B , - Rename current window
  • Ctrl+B & - Close current window (with confirmation)

Pane Management

  • Ctrl+B % - Split window vertically (side by side)
  • Ctrl+B " - Split window horizontally (top and bottom)
  • Ctrl+B Arrow Keys - Navigate between panes
  • Ctrl+B O - Switch to next pane
  • Ctrl+B X - Close current pane (with confirmation)
  • Ctrl+B Z - Toggle pane zoom (maximize/restore)
  • Ctrl+B Space - Toggle pane layouts

Session Management Examples

Creating and Managing Sessions

Create a named session:

tmux new -s webdev

List all sessions:

tmux list-sessions
# or shorthand
tmux ls

Attach to a session:

tmux attach -t webdev
# or shorthand
tmux a -t webdev

Attach to last session:

tmux attach

Kill a session:

tmux kill-session -t webdev

Kill all sessions:

tmux kill-server

Working with Detached Sessions

From within tmux, detach session:

# Press Ctrl+B then D

From command line, detach all clients:

tmux detach -s webdev

Window Management Examples

Creating and Organizing Windows

Create new window with specific command:

tmux new-window -n logs 'tail -f /var/log/syslog'

Create window and run multiple commands:

tmux new-window -n server 'cd /var/www && python -m http.server'

Rename current window from within tmux:

# Press Ctrl+B then ,
# Type new name and press Enter

Move window to different position:

# From command mode (Ctrl+B :)
move-window -t 3

Window Navigation

Switch windows by number:

# Ctrl+B then 0, 1, 2, etc.

Switch to last active window:

# Ctrl+B then L

Find window by name:

# Ctrl+B then F
# Type window name or part of it

Pane Management Examples

Creating and Arranging Panes

Split current pane vertically:

# Ctrl+B then %

Split current pane horizontally:

# Ctrl+B then "

Create complex layout:

# Start with full window
# Split vertically: Ctrl+B %
# Move to left pane: Ctrl+B Left Arrow
# Split horizontally: Ctrl+B "
# Result: Left side has two panes, right side has one

Pane Navigation and Resizing

Move between panes:

# Ctrl+B then Arrow Keys
# or Ctrl+B then O (cycle through panes)

Resize panes:

# Hold Ctrl+B and press Arrow Keys repeatedly
# or Ctrl+B : then resize-pane -L 10 (resize left by 10 units)

Swap pane positions:

# Ctrl+B then { (swap with previous pane)
# Ctrl+B then } (swap with next pane)

Practical Use Cases

Development Environment

Set up a complete development environment:

#!/bin/bash
# Create development session
tmux new-session -d -s dev -n editor
tmux send-keys -t dev:editor 'vim .' Enter

tmux new-window -t dev -n server
tmux send-keys -t dev:server 'npm start' Enter

tmux new-window -t dev -n logs
tmux send-keys -t dev:logs 'tail -f logs/app.log' Enter

tmux new-window -t dev -n terminal
tmux attach-session -t dev

System Monitoring

Create monitoring dashboard:

tmux new-session -d -s monitor -n system
tmux split-window -h -t monitor:system
tmux split-window -v -t monitor:system.0
tmux split-window -v -t monitor:system.1

tmux send-keys -t monitor:system.0 'htop' Enter
tmux send-keys -t monitor:system.1 'iotop' Enter
tmux send-keys -t monitor:system.2 'tail -f /var/log/syslog' Enter
tmux send-keys -t monitor:system.3 'df -h' Enter

tmux attach-session -t monitor

Remote Server Management

Set up server administration session:

tmux new-session -d -s admin -n main
tmux new-window -t admin -n logs 'journalctl -f'
tmux new-window -t admin -n docker 'docker stats'
tmux new-window -t admin -n backup
tmux attach-session -t admin

Advanced tmux Features

Copy Mode

Enter copy mode to scroll and copy text:

# Ctrl+B then [ (enter copy mode)
# Use arrow keys or vim keys (h,j,k,l) to navigate
# Space to start selection
# Enter to copy selection
# Ctrl+B then ] to paste

Command Mode

Enter command mode for advanced operations:

# Ctrl+B then :
# Type commands like:
list-windows
rename-session newsession
set-option status-bg blue

Synchronized Panes

Send commands to all panes simultaneously:

# Ctrl+B then :
setw synchronize-panes on
# Now typing will appear in all panes
# Turn off with: setw synchronize-panes off

tmux Configuration

Basic Configuration

Create ~/.tmux.conf:

# Change prefix key to Ctrl+A
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Enable mouse support
set -g mouse on

# Start window numbering at 1
set -g base-index 1
set -g pane-base-index 1

# Automatically renumber windows
set -g renumber-windows on

# Increase scrollback buffer
set -g history-limit 10000

# Enable vim keys in copy mode
setw -g mode-keys vi

# Fast pane switching
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Split panes with more intuitive keys
bind | split-window -h
bind - split-window -v

Status Bar Customization

Customize status bar appearance:

# Status bar colors
set -g status-bg black
set -g status-fg white

# Show session name, window list, and date
set -g status-left '#[fg=green]Session: #S #[default]'
set -g status-right '#[fg=yellow]%Y-%m-%d %H:%M#[default]'

# Highlight active window
setw -g window-status-current-bg red
setw -g window-status-current-fg white

Reload Configuration

Reload tmux configuration without restarting:

# Add to ~/.tmux.conf
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
# Then use Ctrl+B then R to reload

tmux Scripting and Automation

Session Startup Script

Create automated session setup:

#!/bin/bash
# tmux-dev-setup.sh

SESSION_NAME="development"

# Check if session exists
tmux has-session -t $SESSION_NAME 2>/dev/null

if [ $? != 0 ]; then
  # Create session
  tmux new-session -d -s $SESSION_NAME -n editor

  # Setup editor window
  tmux send-keys -t $SESSION_NAME:editor 'cd ~/project && vim' Enter

  # Create server window
  tmux new-window -t $SESSION_NAME -n server
  tmux send-keys -t $SESSION_NAME:server 'cd ~/project && npm start' Enter

  # Create testing window with split panes
  tmux new-window -t $SESSION_NAME -n test
  tmux split-window -h -t $SESSION_NAME:test
  tmux send-keys -t $SESSION_NAME:test.0 'cd ~/project && npm test' Enter
  tmux send-keys -t $SESSION_NAME:test.1 'cd ~/project' Enter

  # Go back to editor window
  tmux select-window -t $SESSION_NAME:editor
fi

# Attach to session
tmux attach-session -t $SESSION_NAME

Tmuxinator

Install tmuxinator for advanced session management:

gem install tmuxinator

# Create project configuration
tmuxinator new myproject

# Start project session
tmuxinator start myproject

Troubleshooting

Common Issues

Session not found

# List all sessions to verify name
tmux ls

# Check if tmux server is running
tmux info

Key bindings not working

# Check current prefix key
tmux show-options -g prefix

# Reset to default configuration
tmux kill-server
tmux

Configuration not loading

# Verify config file location
ls -la ~/.tmux.conf

# Test configuration
tmux source-file ~/.tmux.conf

Performance Issues

Too many sessions consuming memory

# List sessions with details
tmux list-sessions -F "#{session_name}: #{session_windows} windows (created #{session_created_string}) [#{session_width}x#{session_height}]"

# Kill unused sessions
tmux kill-session -t unused-session

Slow response

# Reduce history limit
set -g history-limit 5000

# Disable mouse support if not needed
set -g mouse off

tmux vs Other Tools

tmux vs screen

  • tmux: More modern, better pane support, active development
  • screen: Older, simpler, available on more systems by default

tmux vs terminal tabs

  • tmux: Session persistence, remote access, scriptable
  • Terminal tabs: Local only, lost on application close

tmux vs IDE

  • tmux: Lightweight, terminal-based, remote-friendly
  • IDE: Full GUI, integrated tools, resource-intensive

Best Practices

Session Organization

  1. Use descriptive session names
tmux new -s project-frontend
tmux new -s monitoring-prod
  1. Organize by project or purpose
# Development sessions
tmux new -s web-dev
tmux new -s api-dev

# Administrative sessions
tmux new -s server-admin
tmux new -s backup-tasks
  1. Use consistent window naming
# Window 0: editor
# Window 1: server
# Window 2: logs
# Window 3: terminal

Workflow Optimization

  1. Create setup scripts for complex environments
  2. Use configuration files for consistent behavior
  3. Learn essential key bindings to improve speed
  4. Use pane synchronization for multi-server management

tmux Command Manual / Help

We can use man and info command to see the manual page of tmux command. tmux command also have --help option to show list of options.

To open man page for tmux command we can use command below. To exit man or info page you can press q.

man tmux

To open info page for tmux command we can use command below.

info tmux

To open help page from tmux command we can run command below.

tmux --help

Within tmux, get help on key bindings:

# Ctrl+B then ?

References

You can find more information about tmux from the following links:

You can read tutorials of related Linux commands below:

Summary

In this comprehensive tutorial, we’ve covered the essential aspects of using tmux as a terminal multiplexer. tmux is an invaluable tool for anyone working with command-line interfaces, especially for remote work and complex development workflows.

Key takeaways:

  • tmux provides session persistence that survives network disconnections
  • Sessions, windows, and panes offer flexible workspace organization
  • Essential key bindings start with Ctrl+B prefix by default
  • Configuration files enable extensive customization
  • Perfect for remote server management and development environments
  • Scripting capabilities allow automated session setup
  • Superior to basic terminal tabs for professional workflows

Visit our Linux Commands guide to learn more about using command line interface in Linux.