Dominic van der Zypen
Dominic van der Zypen

Reputation: 257

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

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?

Upvotes: 6

Views: 1993

Answers (3)

gnued
gnued

Reputation: 91

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.

Upvotes: 2

rafarios
rafarios

Reputation: 11

Use the following command at ed:

s/\. /\.\
/

Be aware that there are two lines.

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

Upvotes: 1

Jim Stewart
Jim Stewart

Reputation: 17323

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.

Upvotes: 8

Related Questions