Copy Mode and Scrollback
This chapter delivers the text that has already scrolled away: how to walk back through a window's history, mark a stretch of it, and carry it off — into another window, an editor, or a file. On the way it settles what the scrollback is, because the buffer you are reading from is a part of Screen with its own size and its own limits.
The scrollback
Every window keeps the lines that roll
off its top in a scrollback buffer.
The buffer belongs to the window, not
to your terminal program, which is why
it survives detach and reattach and is
identical from every client. Its size
is a setting — the lab machine's
system default is 1024 lines, from
/etc/screenrc — and chapter 8 raises
it, with the lab's before-and-after
measurement in transcript t05: 1500
printed lines, of which the default
kept 1048 lines in a capture (1024 of
scrollback plus the visible screen)
and the raised setting kept all 1503.
When a job prints more than the buffer holds, the oldest lines are simply gone. For output that matters, the scrollback is the wrong container — chapter 11's logging recipe records to a file instead. The scrollback is for the recent past you did not know you would need.
Entering copy mode
Press Ctrl-A [ — or Ctrl-A Escape, the same thing. The window freezes into copy mode: output pauses (really it is held; nothing is lost), and your keys now move a cursor over the window and its scrollback instead of going to the shell. The message line shows where in the buffer you are.
Movement is the vi-flavoured set:
| Key | Moves |
|---|---|
| h j k l | Left, down, up, right |
| Ctrl-U / Ctrl-D | Half a page up / down |
| Ctrl-B / Ctrl-F | A full page up / down |
| g | The top of the scrollback |
| G | The bottom — back to the present |
| 0 / $ | Start / end of the line |
| / | Search forward for text |
| n | The next match |
Escape (or q) leaves copy mode without copying anything.
Marking and copying
With the cursor where the wanted text starts, press Space: the first mark is set, and text from there highlights as you move. Move to the end of the stretch and press Enter: the marked text is copied into Screen's paste buffer, copy mode ends, and a message reports how much was taken ("Copied ..."). Pressing Space a second time instead of Enter copies from mark to mark just the same.
Then Ctrl-A ] pastes: the buffer's
contents are typed into the current
window, exactly as if you had keyed
them. Into a shell prompt, that runs a
command you harvested from old output.
Into an editor, it inserts the text.
Into cat > notes.txt, it writes the
text to a file — the poor man's export,
and often the fastest.
The lab did the whole manoeuvre with
real keystrokes through a terminal and
then read the buffer back from outside
(transcript
screen-lab/transcripts/t04-copy-mode-and-scrollback.txt):
a window filled with seq 1 40, copy
mode entered, g to the top, Space,
5j down five lines, Enter. The buffer
afterwards held exactly the first six
lines of that window — the prompt that
ran the command, and 1 to 5:
lab@htch-runtime:~$ seq 1 40
1
2
3
4
5
hardcopy: the scripted cousin
For programs and scripts there is a blunter instrument. hardcopy writes a window's visible screen to a file:
screen -S demo -p editor -X hardcopy editor.txt
and with -h, the scrollback comes
too:
screen -S demo -X hardcopy -h whole.txt
Transcript t04 measures the difference
on the same window: 24 lines without
-h (one screenful), 42 with (the
screen plus everything that had
scrolled off it). hardcopy is how the
lab looked inside detached sessions
throughout this guide, and it is the
natural tool for "keep the output of
that overnight run" — second only to
real logging, chapter 11 again.
Check
Run anything that prints fifty lines.
Ctrl-A [, then g, and you are looking
at the first of them, however long ago
it scrolled away. Mark a few lines with
Space and Enter, then Ctrl-A ] at a
shell prompt and watch your own
scrollback type itself back at you.