Master terminal multiplexing with GNU Screen and tmux. Learn session management, window splitting, customization, and automation to boost your terminal productivity for local and remote work.
Why Use Terminal Multiplexers?
Terminal multiplexers revolutionize command-line productivity by allowing multiple terminal sessions within a single window.
- Session Persistence: Detach and reattach sessions without losing work
- Multitasking: Run multiple commands in parallel
- Remote Work: Maintain sessions on remote servers
- Window Management: Organize terminal workspace efficiently
- Copy/Paste: Advanced copy mode with scrollback
- Automation: Script and automate terminal workflows
- Collaboration: Share sessions for pair programming
1. Screen vs Tmux Comparison
Screen vs Tmux Feature Comparison
| Feature | Screen | Tmux | Notes |
|---|---|---|---|
| Default Prefix | Ctrl-a |
Ctrl-b |
Both customizable |
| Configuration File | ~/.screenrc |
~/.tmux.conf |
Tmux has better defaults |
| Vertical Split | ❌ No | ✅ Yes | Tmux supports both directions |
| Status Bar | Basic | Advanced, customizable | Tmux status bar is superior |
| Copy Mode | ✅ Yes | ✅ Yes | Tmux copy mode is more intuitive |
| Session Management | Good | Excellent | Tmux has client-server model |
| Plugin Support | Limited | ✅ Yes (tpm) | Tmux has plugin manager |
| Mouse Support | ✅ Yes | ✅ Yes | Both require configuration |
| Pane Resizing | Arrow keys | Mouse or keyboard | Tmux is more flexible |
| Community | Stable | Active | Tmux has more recent development |
2. GNU Screen Mastery
Screen Configuration (~/.screenrc)
# ~/.screenrc - GNU Screen Configuration
# Basic Settings
defshell -$SHELL
defutf8 on
deflogin on
# Startup Message
startup_message off
# Visual Bell
vbell off
vbell_msg " Bell!! "
# Screen Buffer (Scrollback)
defscrollback 5000 # 5000 lines of scrollback
termcapinfo xterm* ti@:te@
# Status Bar
hardstatus alwayslastline
hardstatus string "%{= kG}%-w%{= kW}%n %t%{-}%+w %=%{= kG} %H %{= kY}%Y-%m-%d %c"
# Alternative status bar with more info
# caption always "%{= kw}%-w%{= kG}%{+b}[%n %t]%{-b}%{= kw}%+w %=%{+b}%H%{-b} %{= kG}%Y-%m-%d %c"
# Window List
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %d/%m %{W}%c %{g}]'
# Key Bindings
bind -c demo1 0 select 0
bind -c demo1 1 select 1
bind -c demo1 2 select 2
bind -c demo1 3 select 3
bind -c demo1 4 select 4
bind -c demo1 5 select 5
bind -c demo1 6 select 6
bind -c demo1 7 select 7
bind -c demo1 8 select 8
bind -c demo1 9 select 9
# Escape Character (Change from Ctrl-a to Ctrl-o)
# escape ^Oo
# Mouse Support
# mousetrack on
# Split Screen
bind s split
bind S vsplit
bind Q remove
bind tab focus
# Copy Mode
bind -c copy edit
bind -m copy -c paste .
# Logging
bind H log
# Screen Buffer
defscrollback 10000
# Visual Notification
activity "Activity in %t(%n)"
# Automatically detach on hangup
autodetach on
# Power detach (allow reattaching)
power detach
# Zombie processes
zombie az
# Lock screen after inactivity (seconds)
idle 1200 lockscreen
# Screen locking command
lockcmd xtrlock
# Set the escape key to Ctrl-a (default)
escape ^Aa
# Enable 256 colors
term screen-256color
# XTerm settings for better compatibility
termcapinfo xterm*|rxvt*|kterm*|Eterm* ti@:te@
termcapinfo xterm*|linux*|rxvt*|Eterm* OP
# Remove some delays (for faster response)
maptimeout 5
# Start with logging off
log off
# Default windows/screens
screen -t shell1 0
screen -t shell2 1
screen -t logs 2 tail -f /var/log/syslog
screen -t monitor 3 htop
3. Tmux Mastery
Tmux Key Bindings (Prefix: Ctrl-b)
Tmux Configuration (~/.tmux.conf)
# ~/.tmux.conf - Advanced Tmux Configuration
# ============================================================================
# GENERAL SETTINGS
# ============================================================================
# Set true color support
set -g default-terminal "screen-256color"
set -ga terminal-overrides ",xterm-256color:Tc"
# Enable mouse mode (tmux 2.1+)
set -g mouse on
# Set prefix to Ctrl-a (like screen)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# Reduce escape time for faster response
set -sg escape-time 0
# Increase scrollback buffer
set -g history-limit 10000
# Focus events for terminals that support it
set -g focus-events on
# ============================================================================
# KEY BINDINGS
# ============================================================================
# Reload configuration
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
# Split windows with current path
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# Easy pane navigation (vim style)
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Pane resizing with vim keys
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5
# Quick window selection
bind -r C-h select-window -t :-
bind -r C-l select-window -t :+
# Window creation with current path
bind c new-window -c "#{pane_current_path}"
# ============================================================================
# APPEARANCE
# ============================================================================
# Status bar customization
set -g status-interval 1
set -g status-justify centre
set -g status-left-length 50
set -g status-right-length 150
set -g status-style "fg=#665c54"
# Status left
set -g status-left "#[fg=#7c6f64,bg=default,bold] #S #[fg=#7c6f64,bg=default]|"
# Window status
set -g window-status-format "#[fg=#7c6f64,bg=default] #I:#W "
set -g window-status-current-format "#[fg=#d79921,bg=default,bold] #I:#W "
set -g window-status-separator ""
# Status right
set -g status-right "#[fg=#7c6f64,bg=default] %Y-%m-%d %H:%M #[fg=#d79921,bg=default,bold] #(whoami)@#h "
# Pane border
set -g pane-border-style "fg=#7c6f64"
set -g pane-active-border-style "fg=#d79921"
# Message styling
set -g message-style "fg=#d79921,bg=#3c3836"
# ============================================================================
# COPY MODE (VI STYLE)
# ============================================================================
# Use vi keybindings in copy mode
setw -g mode-keys vi
# Copy to system clipboard (requires xclip on Linux)
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-pipe "xclip -in -selection clipboard"
bind -T copy-mode-vi V send -X select-line
bind -T copy-mode-vi C-v send -X rectangle-toggle
# ============================================================================
# PLUGINS (TPM - Tmux Plugin Manager)
# ============================================================================
# Install TPM: git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'tmux-plugins/tmux-prefix-highlight'
set -g @plugin 'tmux-plugins/tmux-sidebar'
# Tmux Resurrect settings
set -g @resurrect-strategy-nvim 'session'
set -g @resurrect-capture-pane-contents 'on'
# Tmux Continuum settings
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'
# Prefix highlight
set -g @prefix_highlight_show_copy_mode 'on'
set -g @prefix_highlight_copy_mode_attr 'fg=white,bg=red'
# ============================================================================
# ADVANCED FEATURES
# ============================================================================
# Enable aggressive resize (multiple clients)
setw -g aggressive-resize on
# Allow rename even if command is running
set -g allow-rename on
# Re-number windows when one is closed
set -g renumber-windows on
# Monitor activity in windows
setw -g monitor-activity on
set -g visual-activity off
# Bell settings
set -g visual-bell off
set -g bell-action none
# ============================================================================
# INITIALIZATION
# ============================================================================
# Initialize TMUX plugin manager (keep this line at the very bottom)
run '~/.tmux/plugins/tpm/tpm'
4. Practical Workflows
Development Workflow Example
# Start tmux session for development
tmux new -s dev
# Create windows for different tasks
# Window 0: Code editor
cd ~/projects/myapp
vim
# Window 1: Server logs
Ctrl-b c # Create new window
cd ~/projects/myapp
npm run dev
# Window 2: Tests
Ctrl-b c
cd ~/projects/myapp
npm test -- --watch
# Window 3: Database
Ctrl-b c
psql myapp_db
# Window 4: Git operations
Ctrl-b c
cd ~/projects/myapp
git status
# Window 5: System monitoring
Ctrl-b c
htop
# Split windows for better workflow
# In window 0 (editor):
Ctrl-b " # Split horizontally
# Bottom pane: terminal for quick commands
cd ~/projects/myapp
# In window 1 (server):
Ctrl-b % # Split vertically
# Right pane: curl tests or API monitoring
watch -n 5 'curl -s http://localhost:3000/health'
# Navigate between panes:
Ctrl-b arrow-keys # Move between panes
Ctrl-b ; # Toggle last active pane
Ctrl-b o # Switch to next pane
Ctrl-b q # Show pane numbers, then press number
# Detach from session (go home)
Ctrl-b d
# Later, reconnect and resume
ssh dev-server
tmux ls # List sessions
tmux attach -t dev # Reattach to dev session
# All windows and processes are exactly as you left them!
System Administration Workflow
#!/bin/bash
# sysadmin-session.sh - Setup tmux session for system administration
SESSION="sysadmin"
TMUX="tmux"
# Check if session exists
$TMUX has-session -t $SESSION 2>/dev/null
if [ $? != 0 ]; then
# Create new session
$TMUX new-session -d -s $SESSION
# Window 1: System Monitoring
$TMUX rename-window -t $SESSION:1 'monitor'
$TMUX send-keys -t $SESSION:1 'htop' C-m
# Split window 1
$TMUX split-window -v -t $SESSION:1
$TMUX send-keys -t $SESSION:1.1 'watch -n 5 "df -h | grep -E \"Filesystem|/$\""' C-m
$TMUX split-window -h -t $SESSION:1.1
$TMUX send-keys -t $SESSION:1.2 'watch -n 5 "free -h"' C-m
# Window 2: Logs
$TMUX new-window -t $SESSION:2 -n 'logs'
$TMUX send-keys -t $SESSION:2 'sudo tail -f /var/log/syslog' C-m
$TMUX split-window -v -t $SESSION:2
$TMUX send-keys -t $SESSION:2.1 'sudo tail -f /var/log/auth.log' C-m
$TMUX split-window -h -t $SESSION:2.1
$TMUX send-keys -t $SESSION:2.2 'journalctl -f' C-m
# Window 3: Network
$TMUX new-window -t $SESSION:3 -n 'network'
$TMUX send-keys -t $SESSION:3 'iftop' C-m
$TMUX split-window -v -t $SESSION:3
$TMUX send-keys -t $SESSION:3.1 'watch -n 5 "netstat -tulpn | grep LISTEN"' C-m
# Window 4: Services
$TMUX new-window -t $SESSION:4 -n 'services'
$TMUX send-keys -t $SESSION:4 'systemctl list-units --type=service --state=running' C-m
$TMUX split-window -v -t $SESSION:4
$TMUX send-keys -t $SESSION:4.1 'watch -n 10 "systemctl --no-pager status nginx mysql redis"' C-m
# Window 5: Backup/Maintenance
$TMUX new-window -t $SESSION:5 -n 'backup'
$TMUX send-keys -t $SESSION:5 'cd /backup && ls -la' C-m
# Window 6: Shell
$TMUX new-window -t $SESSION:6 -n 'shell'
# Select first window
$TMUX select-window -t $SESSION:1
fi
# Attach to session
$TMUX attach -t $SESSION
5. Advanced Features
Session Sharing and Pair Programming
Scripting and Automation
#!/bin/bash
# automated-session.sh - Create automated tmux sessions
SESSION="automation"
WINDOWS=("monitor" "build" "deploy" "test")
create_session() {
tmux new-session -d -s "$SESSION" -n "${WINDOWS[0]}"
for ((i=1; i<${#WINDOWS[@]}; i++)); do
tmux new-window -t "$SESSION:$i" -n "${WINDOWS[$i]}"
done
# Configure window 1: Monitoring
tmux send-keys -t "$SESSION:1" 'watch -n 1 "date; echo; uptime; echo; who"' C-m
# Configure window 2: Build process
tmux send-keys -t "$SESSION:2" 'cd /projects/app && clear' C-m
# Configure window 3: Deployment
tmux send-keys -t "$SESSION:3" 'echo "Deployment commands will go here"' C-m
tmux split-window -v -t "$SESSION:3"
tmux send-keys -t "$SESSION:3.1" 'watch -n 30 "kubectl get pods"' C-m
# Configure window 4: Testing
tmux send-keys -t "$SESSION:4" 'cd /projects/app && npm test' C-m
# Select first window
tmux select-window -t "$SESSION:1"
}
attach_or_create() {
if tmux has-session -t "$SESSION" 2>/dev/null; then
echo "Session '$SESSION' exists. Attaching..."
tmux attach -t "$SESSION"
else
echo "Creating new session '$SESSION'..."
create_session
tmux attach -t "$SESSION"
fi
}
# Run the function
attach_or_create
6. Tips and Best Practices
1. Use descriptive session names:
tmux new -s project-dev2. Regularly save sessions: Use tmux-resurrect or screen logging
3. Customize your prefix: Choose a comfortable key combination
4. Learn key bindings: Master navigation without mouse
5. Use configuration files: Maintain consistent environment
6. Implement session scripts: Automate common setups
7. Monitor resource usage: Be aware of memory consumption
8. Clean up old sessions: Remove unused sessions regularly
9. Backup configurations: Version control your .tmux.conf/.screenrc
10. Share knowledge: Teach team members multiplexer skills
Performance Optimization
| Optimization | Screen | Tmux | Benefit |
|---|---|---|---|
| Scrollback Buffer | defscrollback 5000 |
set -g history-limit 10000 |
More terminal history |
| Escape Time | maptimeout 5 |
set -sg escape-time 0 |
Faster response |
| Mouse Support | mousetrack on |
set -g mouse on |
Easier navigation |
| 256 Colors | term screen-256color |
set -g default-terminal "screen-256color" |
Better color support |
| Aggressive Resize | N/A | setw -g aggressive-resize on |
Better multi-client |
| Focus Events | N/A | set -g focus-events on |
Better terminal integration |
| Pane Synchronization | Ctrl-a :at |
Ctrl-b :setw synchronize-panes |
Send commands to multiple panes |
Troubleshooting Common Issues
Ctrl-a d (screen) or Ctrl-b d (tmux)screen -ls or tmux lsMaster Terminal Multiplexing
Terminal multiplexers like Screen and Tmux are essential tools for anyone working extensively in the command line. They transform your terminal from a single-task interface into a powerful, persistent workspace that survives disconnections and organizes complex workflows.
Key Takeaways: Start with basic session management, then explore window splitting and pane organization. Customize your configuration to match your workflow. Implement automation scripts for common tasks. Use session sharing for collaboration. Most importantly, practice regularly to build muscle memory for key bindings.
Next Steps: Choose either Screen (for maximum compatibility) or Tmux (for modern features) and master it. Create your personalized configuration. Set up session scripts for your common workflows. Explore plugins (for Tmux) to extend functionality. Share your knowledge with team members to improve collective productivity.