Skip to content
Hands-on IT IT Drills

Day 8: tmux I – sessions, windows, panes

Goal: Work in tmux with sessions, windows and panes using only the keyboard, copy text in copy mode and build your own configuration with prefix ⌃a. · Duration: approx. 75 min · Prerequisite: Day 7

Briefing

  • tmux is a server that keeps shells alive. Your terminal is just a client. Window closed, SSH gone: the session keeps running.
  • Hierarchy: server → session (a project) → window (a tab) → pane (a split).
  • Every tmux key starts with the prefix. The default is ⌃b; we use ⌃a (easier to reach, common among senior engineers).
  • The lab uses its own socket (-L lab) and its own file (-f ~/term-lab/tmux.conf). Your normal tmux stays untouched.
tmux server  (socket: /tmp/tmux-<uid>/lab)
 ├─ Session "lab"
 │   ├─ Window 1: zsh    ┌──────────┬──────────┐
 │   │                   │ Pane 1   │ Pane 2   │
 │   │                   │          ├──────────┤
 │   │                   │          │ Pane 3   │
 │   │                   └──────────┴──────────┘
 │   └─ Window 2: logs
 └─ Session "ops"

Drill 1: Starting, leaving and returning to the lab tmux

First the configuration, then a function tl that always passes the socket and the file:

cat > ~/term-lab/tmux.conf <<'EOF'
# ~/term-lab/tmux.conf – lab configuration (Day 8, extended on Day 9)

# --- Prefix: Ctrl-a instead of Ctrl-b --------------------------------------
unbind C-b
set -g prefix C-a
bind C-a send-prefix              # ⌃a ⌃a sends ⌃a to the shell

# --- Basics -----------------------------------------------------------------
set -g default-terminal "tmux-256color"
set -as terminal-features ",xterm-256color:RGB,xterm-ghostty:RGB"
set -sg escape-time 10            # pass Esc to nvim immediately
set -g history-limit 50000
set -g base-index 1               # count windows from 1 (key 1 is on the left)
setw -g pane-base-index 1
set -g renumber-windows on
set -g focus-events on
set -g display-time 2000

# Reload the config
bind r source-file "$LAB/tmux.conf" \; display "tmux.conf neu geladen"
EOF
cat > ~/term-lab/config/zsh/tmux.zsh <<'EOF'
# tmux.zsh – lab tmux (Day 8): own socket, own config
tl() { tmux -L lab -f "$LAB/tmux.conf" "$@"; }
EOF
reload
cd ~/term-lab/projekt
tl new -s lab

You’re now in tmux. At the bottom is the green default status line with [lab] 1:zsh*. The shell inside is your lab zsh, because the tmux server inherits ZDOTDIR and LAB from the shell that starts it.

Now ⌃a d (detach). You’re back in the normal shell:

tl ls

Expected (shortened):

[detached (from session lab)]
lab: 1 windows (created Sat Oct 10 08:51:12 2026)

The session lives on. Back in:

tl attach -t lab

Tip: tl new -A -s lab means “attach, otherwise create”. That’s the one command to remember for every day.

set -sg escape-time 10 matters for Neovim: without that line, tmux waits 500 ms to see whether another key follows Esc. tmux substitutes $LAB in source-file when loading.

Save your progress? Sign up for free

Drill 2: Sessions – one project, one session

Create a second session from inside and switch back and forth:

tmux new -d -s ops -c /tmp
tmux ls

Expected (shortened):

lab: 1 windows (created Sat Oct 10 08:51:12 2026) (attached)
ops: 1 windows (created Sat Oct 10 08:51:52 2026)

Inside tmux, tmux without -L is enough: the $TMUX variable points to the right socket. Now from the keyboard:

  • ⌃a s opens the session tree. Pick with j/k, Enter jumps there, x kills.
  • ⌃a ( and ⌃a ) switch to the previous and next session.
  • ⌃a $ renames the current session.
(0) + lab: 1 windows (attached)
(1) + ops: 1 windows
┌ lab (sort: index) (view: preview) ─────────────────────────────────┐
│ [lab] projekt %                                                    │

Clean up without the keyboard:

tmux kill-session -t ops
tmux ls

Expected:

lab: 1 windows (created Sat Oct 10 08:51:12 2026) (attached)

Save your progress? Sign up for free

Drill 3: Windows – tabs in the terminal

Key Effect
⌃a c new window (in the current path, see Drill 4)
⌃a , rename window
⌃a 1 … 9 to window number n
⌃a n / ⌃a p next / previous window
⌃a w tree of all windows of all sessions
⌃a & close window (with confirmation)

Exercise: ⌃a c, then ⌃a ,, delete the name with ⌃U, type logs, Enter. In the new window:

tail -f logs/app.log

Then ⌃a c for a third window and ⌃a 1 back to the first. Check:

tmux list-windows \
  -F '#{window_index}:#{window_name} panes=#{window_panes} #{?window_active,*,}'

Expected:

1:zsh panes=1 *
2:logs panes=1
3:zsh panes=1

The status line shows the same: [lab] 1:zsh* 2:logs 3:zsh-. * is the active, - the last used window.

Save your progress? Sign up for free

Drill 4: Panes – splits with | and -

Nobody can remember the default keys % and ". | looks like a vertical cut, - like a horizontal one. New panes open in the current path:

cat >> ~/term-lab/tmux.conf <<'EOF'

# --- Windows and panes ------------------------------------------------------
unbind '"'
unbind %
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"

# vim-style pane switching, Shift resizes (repeatable)
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
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
bind -r C-h previous-window
bind -r C-l next-window
bind Tab last-window
EOF
tmux source-file ~/term-lab/tmux.conf
tmux list-keys -T prefix | rg 'split-window|select-pane -L'

Expected:

bind-key    -T prefix -       split-window -v -c "#{pane_current_path}"
bind-key    -T prefix h       select-pane -L
bind-key    -T prefix |       split-window -h -c "#{pane_current_path}"

Now the finger exercise, all in the first window:

  1. ⌃a | splits vertically, ⌃a - splits the right pane horizontally.
  2. ⌃a h jumps left, ⌃a l right, ⌃a j/⌃a k down and up.
  3. ⌃a H and then H several times (without the prefix, -r = repeat) makes the pane wider.
  4. ⌃a z zooms the active pane to full size and back.
  5. ⌃a q shows pane numbers; the digit jumps there.
  6. ⌃a x closes the pane (with confirmation), ⌃a ! turns it into its own window.
tmux list-panes -F '#{pane_index} #{pane_width}x#{pane_height} active=#{pane_active}'

Expected (after steps 1 and 2, sizes depend on the terminal):

1 60x39 active=1
2 59x19 active=0
3 59x19 active=0

Warning: bind -r allows repetition without the prefix for 500 ms (repeat-time). That’s why navigation with h/j/k/l is deliberately bound without -r: otherwise a quickly typed l lands in the wrong pane instead of in the shell.

Save your progress? Sign up for free

Drill 5: Controlling tmux via commands

Everything a key can do, the tmux command can do too. That’s the basis for project scripts on Day 9. Open a fresh window (⌃a c) and type:

tmux split-window -h -c ~/term-lab/projekt
tmux send-keys -t '{right}' 'git log --oneline -3' Enter
tmux select-pane -t '{left}'
sleep 1; tmux capture-pane -p -t '{right}' | head -4

Expected:

[lab] projekt % git log --oneline -3
2e4e6aa (HEAD -> main) chore: Config, Manifeste, Report
a57983c feat: Warenkorb und HTTP-Server
1dc0abc init: Projektgerüst
  • send-keys types into a pane as if you were sitting in front of it. Enter is the key name.
  • capture-pane -p reads a pane’s screen content. Ideal for tests and scripts.
  • {left}, {right}, {last} are targets relative to the active pane. Otherwise session:fenster.pane applies, e.g. lab:2.1.
tmux display -p '#{session_name}:#{window_index}.#{pane_index} #{pane_current_path}'

Expected:

lab:4.1 /Users/du/term-lab/projekt

Formats like #{pane_current_path} give you any information from tmux. The list is in man tmux under FORMATS.

Save your progress? Sign up for free

Drill 6: Copy mode with vi keys and pbcopy

Scrolling, searching and copying work in tmux without a mouse. Copy mode speaks vim (Day 11):

cat >> ~/term-lab/tmux.conf <<'EOF'

# --- Copy mode with vi keys ---------------------------------------------------
setw -g mode-keys vi
set -g set-clipboard on           # OSC 52: copying works over SSH too
bind Enter copy-mode
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi C-v send -X rectangle-toggle
bind -T copy-mode-vi y send -X copy-pipe-and-cancel "pbcopy"
bind -T copy-mode-vi Escape send -X cancel
bind P paste-buffer
EOF
tmux source-file ~/term-lab/tmux.conf
clear; grep -m3 ERROR logs/app.log

Expected:

2026-10-05T08:06:42 checkout ERROR request_id=r0006 user=u06
2026-10-05T08:16:52 checkout ERROR request_id=r0016 user=u16
2026-10-05T08:26:02 checkout ERROR request_id=r0026 user=u03

Now the key sequence:

  1. ⌃a Enter starts copy mode ([0/…] appears in the top right).
  2. ?request_id and Enter searches backwards, n jumps to the next match.
  3. v starts the selection, E extends it to the end of the WORD.
  4. y copies to tmux and, via pbcopy, to the macOS clipboard.
pbpaste; echo
tmux show-buffer; echo

Expected:

request_id=r0026
request_id=r0026

⌃a P (uppercase) pastes the tmux buffer – the lowercase ⌃a p remains “previous window” – and ⌘V pastes the macOS clipboard. Both now have the same content.

More movements in copy mode: ⌃u/⌃d half a page, g/G start/end of the history, w/b words, 0/$ start/end of line, V whole line, ⌃v block. q or Esc exits.

Tip: set-clipboard on additionally sends the copied text to the terminal app via OSC 52 (Day 1). That’s the way to go when tmux runs on a server where pbcopy doesn’t exist.

Save your progress? Sign up for free

Drill 7: Mouse, status line, reloading

The mouse stays off. Clicking distracts, and text selected with the mouse lands in the tmux buffer instead of the terminal. For presentations or pairing sessions, you turn it on with a key. Add a calm status line that shows whether the prefix is pressed:

cat >> ~/term-lab/tmux.conf <<'EOF'

# Mouse off: keyboard first. Toggle with prefix m when needed.
set -g mouse off
bind m set -g mouse \; display "Maus: #{?mouse,an,aus}"

# --- Status line ---------------------------------------------------------------
set -g status-position bottom
set -g status-style "bg=colour236,fg=colour250"
set -g status-left "#[fg=colour16,bg=colour214,bold] #S #[default] "
set -g status-left-length 30
set -g status-right \
  "#{?client_prefix,#[fg=colour16 bg=colour214] PREFIX #[default] ,}%H:%M "
setw -g window-status-current-style "fg=colour214,bold"
setw -g window-status-format " #I:#W "
setw -g window-status-current-format " #I:#W#{?window_zoomed_flag, [Z],} "
EOF

Now ⌃a r (reload). For two seconds the status line shows tmux.conf neu geladen (“tmux.conf reloaded”), then the session name on an orange background on the left:

 lab   1:zsh   2:logs   3:zsh   4:zsh                                        08:52

Press ⌃a and wait: PREFIX appears on the right. ⌃a m reports Maus: an (“mouse: on”), pressing it again Maus: aus (“mouse: off”). A zoomed pane (⌃a z) gets [Z] next to the window name.

tmux show -g mouse; tmux show -g prefix; tmux show -g mode-keys

Expected:

mouse off
prefix C-a
mode-keys vi

The complete file is at $BUCH/tmux/tmux.conf, already including the extensions from Day 9.

Save your progress? Sign up for free

Shortcut table

Key Effect Mnemonic
⌃a d leave the session (keeps running) detach
⌃a s / ⌃a w sessions / windows as a tree sessions, windows
⌃a ( / ⌃a ) previous / next session parenthesis = session
⌃a $ / ⌃a , rename session / window $ like a variable, comma like a name
⌃a c new window create
⌃a 1–9, ⌃a Tab window n, last window Tab like Cmd-Tab
⌃a | / ⌃a - split vertically / horizontally looks like the cut
⌃a h j k l switch pane vim directions
⌃a H J K L resize pane (repeatable) uppercase = bigger
⌃a z zoom pane zoom
⌃a x / ⌃a ! close pane / turn into window x = gone, ! = out
⌃a q show pane numbers quick numbers
⌃a Enter copy mode, then v … y Enter = go in
⌃a P paste tmux buffer Paste
⌃a r / ⌃a m reload config / toggle mouse reload, mouse
⌃a ⌃a ⌃a to the shell (start of line) double = pass through
⌃a ? all key bindings help

Checkpoint

Runs inside or outside tmux, as long as the lab session exists:

c() { eval "$2" >/dev/null 2>&1 && echo "✔ $1" || echo "✘ $1"; }
c "Lab-Session läuft"    'tl has-session -t lab'
c "Prefix ⌃a"            '[[ $(tl show -gv prefix) == C-a ]]'
c "Split mit |"          'tl list-keys -T prefix "|" | rg -q split-window'
c "Pane-Wechsel mit h"   'tl list-keys -T prefix h | rg -q "select-pane -L"'
c "Fenster ab 1"         '[[ $(tl show -gv base-index) == 1 ]]'
c "Copy-Mode vi"         '[[ $(tl show -gv mode-keys) == vi ]]'
c "y kopiert per pbcopy" 'tl list-keys -T copy-mode-vi y | rg -q pbcopy'
c "Maus aus"             '[[ $(tl show -gv mouse) == off ]]'
c "Lab-zsh in tmux"      '[[ $(tl show-environment -g ZDOTDIR) == *term-lab/zsh ]]'

Expected: nine lines with ✔.

Challenge

Set up ⌃a g so that a new window named git starts lazygit in the path of the current pane. When you quit lazygit with q, the window disappears by itself.

Hint: new-window takes a command as its last argument. A window lives only as long as its command.

Solution

cat >> ~/term-lab/tmux.conf <<'EOF'

# Challenge Day 8: lazygit in its own window
bind g new-window -n git -c "#{pane_current_path}" lazygit
EOF
tmux source-file ~/term-lab/tmux.conf

Press ⌃a g in the project, then in another pane:

tmux list-windows -F '#{window_index}:#{window_name} #{pane_current_command}'

Expected (shortened):

1:zsh zsh
2:logs tail
5:git lazygit

After q in lazygit, window 5 is gone. On Day 9 you’ll open lazygit as a popup instead.

Save your progress? Sign up for free

Enterprise Note

  • On servers, the default prefix ⌃b often applies. If tmux runs there inside your local tmux, you send to your local tmux with ⌃a and to the remote tmux with ⌃b (Day 9).
  • tmux keeps the scrollback in memory (history-limit). If you print secrets, they’re in there. Run tmux clear-history after such commands.
  • The socket lives under /tmp/tmux-<uid>/ with permissions only for you. On shared jump hosts, never open sockets to others (chmod, -S), except for deliberate pairing.
  • A long-running migration belongs in tmux, not in a bare SSH window. If the connection drops, it keeps running.
  • The tmux.conf belongs in your dotfiles (Day 13). Keep it runnable without plugins so it works on fresh servers too.