C.W.K.
Stream
Lesson 03 of 03 · published

Detach, Reattach, and Copy Mode

~13 min · tmux, detach, copy-mode, scrollback

Level 0Trapped
0 XP0/35 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

The detach/reattach loop is tmux's reason to exist

Internalize this rhythm and tmux pays for itself within a week:

  1. tmux new -s work — start a named session.
  2. Set up your panes, run your processes, leave them running.
  3. prefix + d — detach. Close your terminal. Walk away. Sleep. Travel.
  4. tmux attach -t work — reattach. Everything is exactly as you left it: running processes, scrollback, cursor positions.

Over SSH this is even more powerful. Start tmux on the remote box; SSH from your laptop, run a long task, detach, disconnect the SSH session — task keeps running on the server. Reconnect from anywhere later, reattach, see the result.

Copy mode — scrolling, searching, and copying

Inside tmux, the mouse wheel doesn't scroll the shell's output by default — you're in a multiplexer, not a terminal. To navigate scrollback, you enter copy mode with prefix + [. Now arrow keys (or vi keys, if configured) scroll. q exits copy mode.

Inside copy mode

With setw -g mode-keys vi (Track 6), copy mode uses vi-style navigation:

  • Movement: h j k l, w b e, 0 $, gg G — same as Vim.
  • /pattern search forward, ?pattern backward, n/N next/previous match.
  • Space start a selection, motions extend it, Enter copy the selection (and exit copy mode).
  • v toggles rectangle selection.
  • q exit copy mode.

Pasting

prefix + ] pastes the most recent tmux copy. With the right config (Track 6), copies in tmux also go to your system clipboard so Cmd-V / Ctrl-V works in other apps.

Copy mode is tmux's Vim. If you've done Tracks 1–2, you already know how to navigate and search; copy mode just borrows those keys. Most tmux users feel "oh, this is just Vim for the scrollback" within a few minutes — and it is.

Code

Detach / reattach over SSH — the canonical remote workflow·bash
# On your laptop:
ssh user@remote-box

# Once on the remote box:
tmux new -s deploy

# Inside tmux on the remote:
./long-running-deploy.sh
# (it's chugging along)

# Detach without stopping anything:
Ctrl-b d

# You're back on the remote shell.  Disconnect SSH if you want:
exit

# Hours later, from anywhere with SSH access:
ssh user@remote-box
tmux attach -t deploy
# Deploy is still running (or finished — you'll see).  Nothing was lost.
Copy mode walkthrough (with vi-keys configured)·text
prefix + [        enter copy mode

Now you're navigating scrollback with vi keys:
k         scroll up one line
j         scroll down one line
Ctrl-u    half page up
Ctrl-d    half page down
g         top of scrollback
G         bottom (back to live shell)
/error    search forward for 'error'
n         next match
N         previous match

Selecting text:
Space     begin selection
(motions) extend the selection
v         toggle rectangle selection
Enter     copy + exit copy mode

Cancel:
q         exit copy mode without copying
Paste·text
prefix + ]        paste the most recent tmux copy
prefix + =        list the paste buffer history

# With clipboard integration (Track 6) the copy also lives
# in the system clipboard, so Cmd+V / Ctrl+V works in other apps.

External links

Exercise

Run tmux new -s scrollback. In the session, run cat /var/log/syslog (or any long file) so the buffer fills with text. Press Ctrl-b [ to enter copy mode. Use k / j or Ctrl-u / Ctrl-d to scroll up. Press /error + Enter to search forward for the word "error." Use Space + motions to select a few lines, then Enter to copy. Press q to leave copy mode, then Ctrl-b ] to paste at the prompt.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.