Inserting a newline in the middle of a line in ed (editor)
Asked Answered
D

3

6

Suppose I have opened a text file in ed, and the current line looks like this:

This is sentence one. Here starts another one.

Now I want to put a newline after one. , such that the new sentence starting with Here starts occupies the next line.

How do I do this in ed?

Durer answered 12/8, 2017 at 19:23 Comment(0)
E
8

You use the s command to make substitutions. The format is:

s/pattern/replacement/

To include a newline in the replacement, escape it with a backslash, then press the return key:

s/one. /one.\
/

Where you literally press return, rather than include a \r or \n.

Equivalence answered 12/8, 2017 at 19:47 Comment(3)
Thanks! Yes I was aware that some substitution would do. I was thinking along the lines of <code>.s/\./\.\n/</code> but it appears that \n yields n.Durer
Sadly this doesn't work as part of a global command (g/.../s/pattern/replacement/) --- it's explicitly forbidden by the specification. I have some very old ed scripts which do this, and which don't work on modern eds, and I haven't found a workaround for this yet.Moser
@DavidGiven I don't think e.g. "All lines of a multi-line list except the last line shall be ended with a <backslash> preceding the terminating <newline>" is necessarily conflicting with such escaping backslash; and it works for me in ed of current GNU/Linux, FreeBSD and OpenBSD. (Unless you mean newline matching.) Do you have a dedicated question for that?Appledorf
O
2

You can do

t.
s/text_before/
-s/text_after/

Explanation:

  1. t. copies the line, in order to get 2 consecutive identical lines, both containing the original text.
  2. Change the 2nd line to contain only the text you want after the added newline.
  3. Do the same, for the 1st line, for text you want before the newline.

NOTE: The '-' prefix means, do it for the previous (of the current addressed) line.

Orran answered 1/2, 2020 at 9:49 Comment(1)
Wow - that's kind of an "out of the box" approach - thanks for sharing this!Durer
L
1

Use the following command at ed:

s/\. /\.\
/

Be aware that there are two lines.

Using 1,$p you will see the expected result.

Leilanileininger answered 12/8, 2017 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.