Terminal multiplexers are game-changers for developers and sysadmins, and tmux stands out as the ultimate tool for managing CLI workflows. Whether you’re SSH-ing into servers or juggling local tasks, tmux keeps sessions alive, organizes chaos, and supercharges efficiency. Here’s your definitive guide:
Why tmux? Key Benefits
- Session Persistence: Close SSH? No problem. Tmux sessions survive disconnects.
- Multitasking Nirvana: Split terminals into panes, stack windows, and switch contexts instantly.
- Workspace Orchestration: Manage projects in isolated sessions (e.g.,
backend
,monitoring
). - Mouse-Friendly: Scroll, select text, and resize panes with your mouse (yes, really!).
1. Installation
# Linux
sudo apt install tmux # Debian/Ubuntu
sudo dnf install tmux # Fedora
# macOS
brew install tmux # Homebrew required
2. Core Concepts & Basic Commands
Prefix Key: All commands start with Ctrl+b
(default). Remap it later!
Action | Command (after Ctrl+b ) |
---|---|
New Session | tmux new -s mysession |
Detach Session | d |
List Sessions | tmux ls |
Reattach Session | tmux attach -t mysession |
New Window | c |
Next Window | n |
Previous Window | p |
Vertical Split | % |
Horizontal Split | " |
Switch Panes | Arrow keys |
Kill Pane/Window | x then y |
3. Pro Configuration (~/.tmux.conf)
Unlock tmux’s true power with customization:
# ~/.tmux.conf
set -g mouse on # Enable mouse
set -g prefix C-a # Remap prefix to Ctrl+a (easier!)
unbind C-b # Remove default prefix
bind C-a send-prefix
# Vim-like pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Status bar tweaks
set -g status-interval 1
set -g status-bg colour234
set -g status-fg white
Reload config: Ctrl+a
then :source-file ~/.tmux.conf
.
4. Productivity Hacks You’ll Love
a. Session Management
- Quick Session Jump:
tmux attach -t mysession
→ Tab-complete session names! - Session Scripting:
# Launch a pre-configured dev environment tmux new -s dev -d # Create detached tmux send-keys -t dev:0 "cd ~/project" C-m tmux send-keys -t dev:0 "vim" C-m tmux attach -t dev
b. Copy Mode (Like Vim!)
- Enter copy mode:
Ctrl+a
+[
- Navigate with
hjkl
, select text withSpace
. - Paste:
Ctrl+a
+]
.
c. Plugins with TPM
Install Tmux Plugin Manager:
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Add to ~/.tmux.conf
:
# List plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect' # Save/restore sessions
set -g @plugin 'tmux-plugins/tmux-continuum' # Auto-save every 15min
# Initialize TPM
run '~/.tmux/plugins/tpm/tpm'
Press Ctrl+a
+ I
(capital i) to install plugins.
5. Advanced: Workflow Automation
- Start tmux on SSH Login:
Add this to~/.bashrc
:if [[ -z "$TMUX" ]] && [ "$SSH_CONNECTION" != "" ]; then tmux attach || tmux new fi
- Pair Programming: Share a session:
tmux new -s pair # Host tmux -S /tmp/pair ls # Guest attaches via: tmux -S /tmp/pair attach
Final Tip: Cheat Sheet
Keep this handy:
Ctrl+a c → New window
Ctrl+a | → Vertical split
Ctrl+a - → Horizontal split
Ctrl+a z → Toggle pane zoom
Ctrl+a & → Kill window
Embrace tmux—it’s more than a tool; it’s a productivity revolution. Start small, tweak your config, and watch your terminal chaos transform into zen-like efficiency. 🚀
> Pro Challenge: Create a session that auto-launches your daily tools (logs, editor, tests) using send-keys
!