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.
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.
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.
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.
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.
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.
Exercise. How do you split a line into two? (Hint: in the replacement text of a substitution, backslash-quoted chars are taken literally.)
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”.