Adding Text to a Line in Ed(1)

If you're used to visual or full-screen text editors, switching to ed can be a challenge. One of the challenges for an ed novice is adding text to a line after that line has already been entered. Thankfully, it can be done with a simple regular expression (regex) and the "s" (substitute) command. The format is "s/regex/replacement/" where "regex" is the regular expression to match and "replacement" is the replacement text you want. Matching the entire line is matching the line start ("^"), any number of characters in between (".*"), and the line end ("$"). The entire line needs to be matched by the regex because it needs to be included in the replacement text. In ed, an unescaped ampersand ("&") in the replacement string is replaced with the string currently matched by the regex. Putting this all together, the command to add on text would be "s/^.*$/&replacement/".

For example, if you added one part of the Fox Test, thus:

  • a

  • The quick brown fox

  • .

Then completing the Fox Test on the same line would be accomplished thus:

  • s/^.*$/& jumps over the lazy dog./

Note: the space after the "&" in this example is necessary because no space was left after the word "fox". If there was a space and you didn't want to add another, you would run "jumps" up against the "&" without a space.

All Cards on This Page



2110 medium
Full list of journal entries