Thursday, May 15, 2025

Tmux Getting Started Guide

Tmux Getting Started Guide

Tmux (Terminal Multiplexer) is a powerful tool that allows you to create multiple terminal sessions within a single window, detach from sessions, and reattach to them later - even if your connection drops.

Basic Concepts

  • Session: A collection of windows that can be detached and reattached
  • Window: Like a tab in your terminal (full screen)
  • Pane: Splits within a window (horizontal or vertical divisions)
  • Prefix Key: Default is Ctrl+b, pressed before most tmux commands

Tip: The most common command you'll use is detaching from a session: Ctrl + b, then d

Installation

# Ubuntu/Debian
sudo apt install tmux

# CentOS/RHEL
sudo yum install tmux

# macOS with Homebrew
brew install tmux

Essential Commands

Session Management

Command Description
tmux new -s <name> Create a new named session
tmux new -t <name> Create a new named session (alternate syntax)
tmux attach or tmux a Attach to the last session
tmux attach -t <name> Attach to a specific session
tmux a -t <name> Attach to a specific session (shorthand)
tmux ls List all sessions
tmux kill-session -t <name> Kill a specific session
tmux kill-server Kill the tmux server and all sessions
Prefix + d or Ctrl + b, then d Detach from current session

Window Management

Command Description
Prefix + c Create a new window
Prefix + , Rename the current window
Prefix + n Move to the next window
Prefix + p Move to the previous window
Prefix + <number> Switch to window by number
Prefix + w List all windows
Prefix + & Kill the current window

Pane Management

Command Description
Prefix + % Split pane vertically
Prefix + " Split pane horizontally
Prefix + <arrow key> Move between panes
Prefix + z Toggle pane zoom (make a pane full-screen)
Prefix + x Kill the current pane
Prefix + { Move the current pane left
Prefix + } Move the current pane right
Prefix + Ctrl+<arrow> Resize pane
Prefix + q Show pane numbers

Copy Mode

Command Description
Prefix + [ Enter copy mode
Space Start selection (in copy mode)
Enter Copy selection (in copy mode)
Prefix + ] Paste copied text
q Quit copy mode

Saving Output

# Capture entire pane history to a file
tmux capture-pane -S - \; save-buffer filename.log \; delete-buffer

# Save to a specific log file (e.g., a.log)
tmux capture-pane -S - \; save-buffer a.log \; delete-buffer

Note: The backslash before semicolons is important when running the command directly in the shell

Advanced Usage

Synchronize Panes

# Type the same commands in all panes
Prefix + :
setw synchronize-panes on  # or off to disable

Custom Prefix Key

To change the prefix key (e.g., to Ctrl+a), add to your ~/.tmux.conf:

unbind C-b
set -g prefix C-a
bind C-a send-prefix

Custom Session Layout

To create a custom development layout:

# Create a new session with a window for editing
tmux new -s dev -n editor

# Split the window for terminal commands
tmux split-window -v -p 30

# Create another window for running servers
tmux new-window -n server

# Switch back to the first window
tmux select-window -t dev:1

Troubleshooting

  • Lost connection? Simply run tmux attach to reconnect
  • Session not found? Use tmux ls to verify the session name
  • Command not working? Make sure you're pressing the prefix key first

Additional Resources

No comments:

Post a Comment