Basic ed concepts

Read-evaluate-print loop

ed processes a list of commands and outputs (unless option -s is set) the result of the command. Commands consist of zero or more addresses, followed by a single command character, followed possibly by parameters. The list of commands is read from stdin, so ed-scripts can be executed as cat mscript.ed | ed myfile.

Lines

The basic unit of text is the line. A text file is considered an array of lines, numbered from 1 to n; 0 refers to the position just before the first line. One of the lines is considered the current line. Additionally, 26 named lines references a to z can be used.

Lines can be referred to in several way, including the following.

  • 1, 2, ... directly naming a line by number
  • 'a, 'b, ... using a named line reference
  • . names the current line
  • -1, -2, ..., +1, +2, ... addressing relative to the current line
  • $ refers to the last line in the text

The k command can be used to set a named line reference; it takes one address (defaulting to .), the line the reference should be set to, and one parameter, the name of the reference.

Line ranges

For some very common line ranges, there are special abbreviations.

  • % is 1,$
  • ; is .,$

Files

ed does all editing operations in a buffer (defaulting to empty); if not saved before quitting, all changes are lost. Additionally, an optional path is considered the default file. If ed is started with a file name as argument, this file becomes the default file name and the content of the specified file is read into the buffer on start up.

  • f sets the default file name (and does nothing else); if called without argument, the default file name is printed.
  • e clears the buffer, reads the file, and sets the default file name. If no file name is given, the default file name is used. The current line is set to the last line read. It does warn about unsaved edits, however.
  • E is the forceful version of e
  • w writes the addressed lines to the specified file (or the default file, if none is specified); any previous content of the file is lost unconditionally. If and only if no default file is set, the specified file becomes the default file.
  • W is an append-version of w; this a BSD extension.
  • r adds the specified file after the specified line (default is $). If and only if no default file is set, the file becomes the default file.

Substitution

The substitution command s is the main command for changing text. In a given line range (defaulting to .,.), it replaces, on each line (by default) the first occurrence of a regular expression by a replacement text.

The parameter is formed as follows: first a separation character, then the regular expression, then the separation character again, then the replacement text, optionally the separation character again, possibly followed by modifiers. Any non-whitespace character can be used as separation character if that does not clash with the second form of the substitution command. If the separation character occurs in the regular expression or the replacement text, it has to be backslash-escaped. Traditionally, / is chosen as separation character, this giving the well-known form

s/re/replacement

Leaving out the final separator is equivalent to a p modifier, which asks that the substituted line be printed.

As mentioned, by default, the first occurrence of the regular expression is substituted, i.e., the longest among the left-most matches. Providing the g modifier asks for all matches to be replaced. Providing a numeral as modifier asks the match with that number (starting from 1) to be replaced.

The replacement text may contain references \1 to \9 to the corresponding explicitly bracketed subexpressions. Additionally, and unescaped & refers to the matched expression. If the whole replacement text is a single %, the replacement expression of the last substitution command is used.

The current line is set to the last line affected.

As a BSD-ism, there is also a second form of the substitution form, again with line range defaulting to .,., asking to repeat the last substituion

s

In this form, either a numerical modifier or any set of the g, p, r modifiers are accepted. The r modifier asks that the regular expression of the last search rather than that of the last substitution be used.

Adding and viewing text

The commands a and i add text after and before the named line (which defaults to .). To do so, after the command, ed enters input mode. Lines entered in input mode are inserted verbatim into the buffer until a line consisting of a single dot is entered; this line is not copied and input mode is finished. c replaces the specified block by the newly to be entered (also in input mode) text.

Exercise. How to you add a line consisting of a single dot?

Hint. When finding a typo in the line on is currently entering in input mode, it is usually the fastest to continue entering the rest of the line, then terminate with ., fix the typo with s and continue with a.

The command p shows the specified line range. n does so, but prefixes each line by its line number.

The d command deletes the specified lines.

Moving text around

  • The m command moves the specified block of text to follow directly after the specified line (which may be 0, to indicate that the text is so tbe moved to the beginning of the buffer).
  • The t command copies the specified block of text to the position directly following the specified line (which may be 0).
  • the j joins the specified lines (defaulting to the block consisting of the current and the next line) to a single long line.

Exercise. How do you split a line into two? (Hint: in the replacement text of a substitution, backslash-quoted chars are taken literally.)

Global operations

There are 4 variants of globally changing the buffer: g, v, G, and V. The g and v variants take a regular expression and a command list. A command list is a list of commands separated by a backslash followed by a newline (i.e., the backslash indicates a continuation line for thecurrent command). In the specified range (defaulting to the whole buffer) the g command executes the command list once for each line matching the regular expression; the current line is set to the line of the current match before the command list is exeuted. An empty command list means p. The v command does the same for lines not machting the regular expression.

The G and V commands are the interactive variants of g and v, respectively. For each match, it is prompted separately for the command list. A single & repeats the last non-null command list.

Exercise. Given a text, underline all lines starting with “CHAPTER”.

Quitting

  • q quits ed; it does warn about lost edits, however.
  • Q forcefully quits.
  • wq is w followed by q.