9

How may I insert a lone dot on a line with the ed editor? Normally, a dot on a line by itself ends the insertion; can it somehow be escaped?

Kusalananda
  • 333,661

1 Answers1

8

No, you cannot insert a period on a line by itself, because that's ed's instruction to stop inserting text; see one of the early ed man pages (page 7):

Input mode is left by typing a period (.) alone at the beginning of a line.

You can work around it by not entering a period by itself; for example: Space.Enter followed by .Enter followed by s/^ //. You could use any other character than Space as the token, as long as you pair it in the subsequent s// command.

A period cannot be inserted by itself; when appending, changing, or inserting a line, ed ends up calling the append_lines function; that function, in buffer.c, reads input delimited by a newline (via get_stdin_line). As part of that, it checks to see if a period is the only thing (besides the trailing newline) in the result; below, "ibufpp" is the pointer to the inputted text and "size" is the length of that buffer:

if( size == 2 && **ibufpp == '.' ) { *ibufpp += size; return true; }

You can download compressed tar bundles at: http://download.savannah.gnu.org/releases/ed/ or view Debian's upstream page for ed or OpenBSD's page for ed where the corresponding code is in main.c.

Another option, discovered in an ed(1) Conference twitter post, is to enter any other single character, then replace that character with a period:

  1. xEnter
  2. .Enter
  3. s/./.
Kusalananda
  • 333,661
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 1
    So just to be clear, there's no way to actually enter it in insert mode ? – Brennan Vincent Nov 08 '19 at 20:20
  • Depending on your keyboard layout, you could pick for a token any key that's quick to type adjacent to both period and /, then use an unanchored s/k// (with k as an example), if you find that faster to type. – Jeff Schaller Nov 08 '19 at 20:21
  • @Brennan, see the recent edit – Jeff Schaller Nov 08 '19 at 20:39
  • @JeffSchaller https://salsa.debian.org/debian/ed/blob/upstream/1.15/buffer.c#L127 – A.B Nov 09 '19 at 09:11
  • Thank you, A.B -- I've incorporated that into the most recent edit. – Jeff Schaller Nov 09 '19 at 12:00
  • On OpenBSD (which does not use GNU ed by default), the relevant code is line 1046 in main.c: https://github.com/openbsd/src/blob/master/bin/ed/main.c#L1046 Other BSDs are likely using this variant of ed too. – Kusalananda Nov 09 '19 at 12:18
  • So, just to be clear, there's no way to do it with just the "insert" command. – ilkkachu Nov 09 '19 at 12:28
  • Thank you to Kusalananda - I've incorporated that link. Also thanks to ilkkachu for the nudge to have a direct answer (at the top). – Jeff Schaller Nov 09 '19 at 12:31
  • 1
    Because, there could be. In SMTP, a line with just a dot ends the data part of the message, but you can still send an email with a single dot on a line by itself: it gets sent as .. on the protocol level (and any other lines that start with a dot get that dot removed). – ilkkachu Nov 09 '19 at 12:33
  • @Kusalananda Thanks, edited to remove reference to "insert mode" terminology which I had (I guess wrongly) assumed was similar between ed and vi. – Brennan Vincent Nov 09 '19 at 21:28
  • @BrennanVincent My apologies. There are in fact references to "input mode" even in the POSIX specification for ed. I was honestly not aware that this was called "input mode", and I was obviously wrong in saying there was no such thing. ed (a line editor) being very different from vi (a screen editor), the modes "feel" very different though. The current phasing in the question does not feel odd or wrong at all, but feel free to change back if you wish. – Kusalananda Nov 09 '19 at 22:30