SSH into a server, start a long-running process, lose connection, lose your work. tmux solves this. It creates persistent terminal sessions that survive disconnects and lets you manage multiple terminals from one window.
Basic Usage# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Start new session
tmux
# Start named session
tmux new -s mysession
# List sessions
tmux ls
# Attach to session
tmux attach -t mysession
tmux a -t mysession # short form
# Attach to last session
tmux attach
tmux a
# Detach from session (inside tmux)
Ctrl-b d
# Kill session
tmux kill-session -t mysession
The Prefix Key# All tmux commands start with a prefix key (default: Ctrl-b), then another key:
C C C C C C C t t t t t t t r r r r r r r l l l l l l l - - - - - - - b b b b b b b d c n p " % ? # # # # # # # D N N P S S S e e e r p p h t w x e l l o a t v i i w c w i t t h i w o a n i u h v l d n s o e l o d r r w o w i t b w i z i i n o c n d n a d o t l i w a n l g s
Windows (Tabs)# C C C C C C C t t t t t t t r r r r r r r l l l l l l l - - - - - - - b b b b b b b c , n p 0 w & - 9 # # # # # # # C R N P G L K r e e r o i i e n x e s l a a t v t t l t m i o e e w o w c i u w i u n c n s i n r e u d n d r w r o w d o e r w i o w n w e n w s t i n d n t o n ( w d w u i i o w m n n w i b t d n e e o d r r w o a w c t i v e )
Panes (Splits)# C C C C C C C C C C t t t t t t t t t t r r r r r r r r r r l l l l l l l l l l - - - - - - - - - - b b b b b b b b b b " % ; q q z x { } 0 - 9 # # # # # # # # # # S S C T S G T K M M p p y o h o o i o o l l c g o g l v v i i l g w t g l e e t t e l o l e p e c p p h v t a p u a a o e h l n a p r n n r r r a e n a r e e i t o s e n e z i u t n e n l r o c g u n t e i n a h a m u z f g t l c b m o p t h a l p t e b o a t l y a i r e m n l n v s r e y e e ( s f p u a l n l e s c r e e n )
Resize Panes# C C C C # C t t t t t r r r r O r l l l l r l - - - - - b b b b h b o : : : : l C r r r r d t e e e e r s s s s C l i i i i t - z z z z r ↑ e e e e l / - - - - - ↓ p p p p b / a a a a ← n n n n a / e e e e n → d - - - - D U L R p r 1 1 1 1 e 0 0 0 0 s s a # # # # r r D U L R o o p e i w w f g n 1 t h k 0 t e 1 1 y 0 l 0 1 s i 0 l n c i e o c n s l o e u l s m u n m s n s
Copy Mode# tmux has a built-in copy/paste system:
C q # S E ? n N C t p n t r I a t r l n c e l - e r - b c b o [ p ] y m o d # # e # # # # # # # E E S C S S N P P n x v t o e e e r a t i i a p a a x e s e t r y r r t v t r b t c c i e c i s h h s o c o n s e e u b o p d e l f b a s u p y i l e o a r f y n e c r c c s f m g c t w k h e e m o s t i a w a r o d ) i o r a r r d e : o n d r e c e n d s h u l r t e s u l t C P g G q t a r g l e - U b p / [ D o w n # # # # # E S G G E n c o o x t r i e o t t t r l o o l c t b o o o p p t y t o m m o d e t o s c r o l l
Configuration (~/.tmux.conf)# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Change prefix to Ctrl-a (like screen)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# Start windows at 1, not 0
set -g base-index 1
setw -g pane-base-index 1
# Enable mouse
set -g mouse on
# More scrollback
set -g history-limit 50000
# Faster escape
set -sg escape-time 0
# Better splits (current path)
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
# Easy reload
bind r source-file ~/.tmux.conf \; display "Reloaded!"
# Vi mode
setw -g mode-keys vi
# Better copy mode
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-selection-and-cancel
# Status bar
set -g status-style 'bg=#333333 fg=#ffffff'
set -g status-left '[#S] '
set -g status-right '%H:%M %d-%b'
Reload config without restart:
C t r l - b : s o u r c e - f i l e ~ / . t m u x . c o n f
Session Management# From Outside tmux# 1
2
3
4
5
6
7
8
9
10
11
# New session with window name
tmux new -s work -n editor
# Attach or create
tmux new -A -s work
# Send command to session
tmux send-keys -t mysession 'npm start' Enter
# Run command in new window
tmux new-window -t mysession 'htop'
From Inside tmux# C C C C t t t t r r r r l l l l - - - - b b b b s $ ( ) # # # # L R P N i e r e s n e x t a v t m i s e o s e u e s s s s s e s i s s i o s e o n i s n s o s n i ( o i n n t e r a c t i v e )
Practical Workflows# Development Setup# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# dev-session.sh
SESSION = "dev"
tmux new-session -d -s $SESSION -n "editor"
tmux send-keys -t $SESSION :editor "cd ~/project && vim" Enter
tmux new-window -t $SESSION -n "server"
tmux send-keys -t $SESSION :server "cd ~/project && npm run dev" Enter
tmux new-window -t $SESSION -n "shell"
tmux send-keys -t $SESSION :shell "cd ~/project" Enter
tmux select -window -t $SESSION :editor
tmux attach -t $SESSION
Split Layout Script# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# monitoring.sh
tmux new-session -d -s monitoring
# Main pane - htop
tmux send-keys 'htop' Enter
# Right pane - logs
tmux split-window -h
tmux send-keys 'tail -f /var/log/syslog' Enter
# Bottom right - disk
tmux split-window -v
tmux send-keys 'watch df -h' Enter
# Select first pane
tmux select -pane -t 0
tmux attach -t monitoring
Remote Pair Programming# 1
2
3
4
5
6
7
# Host creates session
tmux new -s pair
# Guest attaches (same server)
tmux attach -t pair
# Both see the same session in real-time
Useful Commands# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# List all key bindings
tmux list-keys
# Show all options
tmux show-options -g
# Capture pane contents
tmux capture-pane -p > output.txt
# Clear history
tmux clear-history
# Swap panes
Ctrl-b :swap-pane -U # With pane above
Ctrl-b :swap-pane -D # With pane below
# Move window
Ctrl-b :move-window -t 3
# Join pane from another window
Ctrl-b :join-pane -s :2 # From window 2
# Break pane into new window
Ctrl-b !
tmux + SSH# Automatic Attach# Add to .bashrc on remote server:
1
2
3
if [[ -z " $TMUX " ]] && [[ -n " $SSH_CONNECTION " ]] ; then
tmux attach -t main || tmux new -s main
fi
Persistent Remote Session# 1
2
# SSH and attach to tmux
ssh server -t 'tmux attach || tmux new'
Plugins (TPM)# Install tmux plugin manager:
1
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
Add to ~/.tmux.conf:
1
2
3
4
5
6
7
8
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect' # Save/restore sessions
set -g @plugin 'tmux-plugins/tmux-continuum' # Auto-save
# Initialize TPM (keep at bottom)
run '~/.tmux/plugins/tpm/tpm'
Install plugins: Ctrl-b I
Quick Reference# Action Keys Detach Ctrl-b dNew window Ctrl-b cNext window Ctrl-b nSplit horizontal Ctrl-b "Split vertical Ctrl-b %Switch pane Ctrl-b oZoom pane Ctrl-b zCopy mode Ctrl-b [Paste Ctrl-b ]List sessions Ctrl-b sRename window Ctrl-b ,
tmux transforms how you work with remote servers. No more lost sessions, no more juggling terminal windows. Start with tmux, learn the prefix pattern, and gradually customize. Once you’re used to it, you’ll wonder how you worked without it.