4

I'm trying to understand the ed DSL a little bit better because versions of it show up in other tools (e.g. sed, vim).

Using ed it is possible to print the next 4 lines using .,+4n where .,+4 is an explicit range starting at the current line and extending 4 lines downward.

However, printing several lines around the current one for context seems important enough to deserve some syntactic sugar.

Is there an abbreviation for line ranges relative to the current line in ed already?

e.g. in the transcript below .,+4n seems rather long.

$ touch ~/foo
$ ed '-p* ' ~/foo
0
* 0a
1
2
3
4
5
6
7
8
9
10
11
.
* 5
5
* .,+4n
5   5
6   6
7   7
8   8
9   9
Greg Nisbet
  • 3,076
  • 2
    something shorter than .,+4n and that does more than that? e.g. you do not want -4,+4p ? – Jeff Schaller Nov 10 '18 at 19:53
  • By “around”, I meant “based on” and “near”. The specific range is less important than brevity. If there is a short way of doing either of those things, that works. – Greg Nisbet Nov 10 '18 at 19:57
  • 1
    i think that you may want the term relative to, as in line ranges relative to the current line – jsotola Nov 10 '18 at 20:34
  • 2
    If you want to know ed better, there's an ancient game called quiz (in the bsdgames package in my linux distro). The questions and answers for /usr/games/quiz function ed-command can be instructive. – Cupcake Protocol Nov 11 '18 at 01:15

1 Answers1

2

-4,+4n would print the preceding four, the current and the following four lines (numbered).

Another way to write the same thing, which is slightly shorter, is -4z8n. This would apply the z command, which is different from p in that it first moves to the addressed line (here -4) and then shows that line and (here) 8 more lines. The n makes the lines numbered. The real difference is that if you then give the command zn again (no numbers now), it will show the next eight lines (or whatever number you entered after the initial z command).

Both commands change what's considered the "current line", and you would have to manually move back with -4, or set a mark before moving (with e.g. ka) and then move back to that mark (with e.g. 'a). The ed editor does not accept multiple command to be given in one go unfortunately...

Note that the z command is not a standard ed command, but supported by both GNU ed and ed on BSD systems.

With ex, the command .z.5 would show five lines of the current buffer with the current line in the middle. Here too, you would move the cursor down to the last displayed line.

Kusalananda
  • 333,661
  • +1 If the goal is to be succinct, then you should comment that -4z8 will print 4 before and 4 after as the OP asked. And that z4 will print the next 4 lines (the first question). –  Nov 13 '18 at 23:18