0

BSD ed and GNU ed have the z command for scrolling. However, repeatedly using the z command only seems to scroll forward. Is there something similar to z that can scroll backward instead of forward?

Flux
  • 2,938
  • You can address a previous line, cumulatively, e.g., -----p to go back 5 lines. – Thomas Dickey Nov 15 '21 at 08:56
  • ed can count, too. -40n works fine, and my term pastes it with middle-button too. -40;.,.+5n goes back 35 lines at a time and shows you 5 numbered lines each time. – Paul_Pedant Nov 15 '21 at 09:30

1 Answers1

2

I'm assuming "scrolling" in the question means "showing the next few lines from the buffer in the terminal", which is what the non-standard z command is doing. Moving to some other line is done by addressing a line by its line number, by a regular expression, or by a relative address (like -4, for the 4th line previous), but this is not what this question is about, I think.

You can't scroll backwards with z directly in the same neat way that you scroll forward (starting by e.g. .z and then just z, repeatedly, to show the next few lines in the buffer), but you could use relative addressing to jump back twice the scroll window size and then scroll forward from that point.

Assuming you use a scroll window of 25 lines and that you want to scroll through the document from the very end, backwards.

$z25
-50z
-50z

(etc., use zn in place of z to get numbered lines.)

This starts by showing the last line with $z25 (this also sets the scroll window for subsequent z commands to 25 lines). Each -50z would then move 50 lines back in the buffer and then show the 25 lines onward from there.

The issue is that you generally don't know the default scroll window size (by default, the number of lines in the terminal), so you would need to give an explicit number of lines to scroll, as done above.

Entering -50z to see the next (previous) set of lines is also a bit cumbersome, but might not be too much of an issue if one is using ed with rlwrap, which allows you to use Up-arrow to recall the previous command.

Another issue is that towards the start of the editing buffer, entering a relative address that takes you past the start of the buffer will give you an error.


The z command is a shorthand for something like .,+24p, except that the 24 is automatically replaced by the number of lines available in the terminal window, minus 1 (unless an explicit number is given as in z25). The -50z command shown above could therefore be rewritten as (something like)

-50;.,+24p

which means "jump 50 lines back in the buffer, then print the current line and the following 24 lines". (Use n in place of p to get numbered lines.)

Kusalananda
  • 333,661