| ED(1) | ED(1)

NAME

| ed – text editor

SYNOPSIS

| `ed` [ `−` ] [ `−o` ] [ file ]

DESCRIPTION

Ed is a venerable text editor.

If a file argument is given, ed simulates an `e` command (see below) on that file: it is read into ed’s buffer so that it can be edited. The options are
`−` Suppress the printing of character counts by `e`, `r`, and `w` commands and of the confirming `!` by `!` commands.
`−o` (for output piping) Write all output to the standard error file except writing by `w` commands. If no file is given, make `/dev/stdout` the remembered file; see the `e` command below.

Ed operates on a ‘buffer’, a copy of the file it is editing; changes made in the buffer have no effect on the file until a `w` (write) command is given. The copy of the text being edited resides in a temporary file called the buffer.

Commands to ed have a simple and regular structure: zero, one, or two addresses followed by a single character command, possibly followed by parameters to the command. These addresses specify one or more lines in the buffer. Missing addresses are supplied by default.

In general, only one command may appear on a line. Certain commands allow the addition of text to the buffer. While ed is accepting text, it is said to be in input mode. In this mode, no commands are recognized; all input is merely collected. Input mode is left by typing a period `.` alone at the beginning of a line.

Ed supports the regular expression notation described in regexp(7). Regular expressions are used in addresses to specify lines and in one command (see s below) to specify a portion of a line which is to be replaced. If it is desired to use one of the regular expression metacharacters as an ordinary character, that character may be preceded by ‘`\`’. This also applies to the character bounding the regular expression (often `/`) and to `\` itself.

To understand addressing in ed it is necessary to know that at any time there is a current line. Generally, the current line is the last line affected by a command; however, the exact effect on the current line is discussed under the description of each command. Addresses are constructed as follows.
1. The character `.`, customarily called ‘dot’, addresses the current line.
2. The character `$` addresses the last line of the buffer.
3. A decimal number n addresses the n-th line of the buffer.
4.`'x` addresses the line marked with the name x, which must be a lower-case letter. Lines are marked with the `k` command.
5. A regular expression enclosed in slashes ( `/`) addresses the line found by searching forward from the current line and stopping at the first line containing a string that matches the regular expression. If necessary the search wraps around to the beginning of the buffer.
6. A regular expression enclosed in queries `?` addresses the line found by searching backward from the current line and stopping at the first line containing a string that matches the regular expression. If necessary the search wraps around to the end of the buffer.
7. An address followed by a plus sign `+` or a minus sign `−` followed by a decimal number specifies that address plus (resp. minus) the indicated number of lines. The plus sign may be omitted.
8. An address followed by `+` (or `−`) followed by a regular expression enclosed in slashes specifies the first matching line following (or preceding) that address. The search wraps around if necessary. The `+` may be omitted, so `0/x/` addresses the first line in the buffer with an `x`. Enclosing the regular expression in `?` reverses the search direction. 9. If an address begins with `+` or `−` the addition or subtraction is taken with respect to the current line; e.g. `−5` is understood to mean `.−5`.
10. If an address ends with `+` or `−`, then 1 is added (resp. subtracted). As a consequence of this rule and rule 9, the address `−` refers to the line before the current line. Moreover, trailing `+` and `−` characters have cumulative effect, so `−−` refers to the current line less 2.
11. To maintain compatibility with earlier versions of the editor, the character `^` in addresses is equivalent to `−`.

Commands may require zero, one, or two addresses. Commands which require no addresses regard the presence of an address as an error. Commands which accept one or two addresses assume default addresses when insufficient are given. If more addresses are given than a command requires, the last one or two (depending on what is accepted) are used.

Addresses are separated from each other typically by a comma `,`. They may also be separated by a semicolon `;`. In this case the current line is set to the previous address before the next address is interpreted. If no address precedes a comma or semicolon, line 1 is assumed; if no address follows, the last line of the buffer is assumed. The second address of any two-address sequence must correspond to a line following the line corresponding to the first address.

In the following list of ed commands, the default addresses are shown in parentheses. The parentheses are not part of the address, but are used to show that the given addresses are the default. ‘Dot’ means the current line.
(`.`)`a
` <text>
`.` Read the given text and append it after the addressed line. Dot is left on the last line input, if there were any, otherwise at the addressed line. Address `0` is legal for this command; text is placed at the beginning of the buffer.
(`.,.`)`b`[`+−`][pagesize][`pln`]

| Browse. Print a ‘page’, normally 20 lines. The optional `+` (default) or `−` specifies whether the next or previous page is to be printed. The optional pagesize is the number of lines in a page. The optional `p`, `n`, or `l` causes printing in the specified format, initially `p`. Pagesize and format are remembered between `b` commands. Dot is left at the last line displayed.

(`.,.`)`c
` <text>
`.` Change. Delete the addressed lines, then accept input text to replace these lines. Dot is left at the last line input; if there were none, it is left at the line preceding the deleted lines.
(`.,.`)`d
`

| Delete the addressed lines from the buffer. Dot is set to the line following the last line deleted, or to the last line of the buffer if the deleted lines had no successor.

`e` filename

| Edit. Delete the entire contents of the buffer; then read the named file into the buffer. Dot is set to the last line of the buffer. The number of characters read is typed. The file name is remembered for possible use in later `e`, `r`, or `w` commands. If filename is missing, the remembered name is used.

`E` filename

| Unconditional `e`; see ‘`q`’ below.

`f` filename

| Print the currently remembered file name. If filename is given, the currently remembered file name is first changed to filename.

(`1,$`)`g/`regular expression`/`command list
(`1,$`)`g/`regular expression`/
` (`1,$`)`g/`regular expression

Global. First mark every line which matches the given regularexpression. Then for every such line, execute the command list with dot initially set to that line. A single command or the first of multiple commands appears on the same line with the global command. All lines of a multi-line list except the last line must end with `\`. The ‘`.`’ terminating input mode for an `a`, `i`, `c` command may be omitted if it would be on the last line of the command list. The commands `g` and `v` are not permitted in the command list. Any character other than space or newline may be used instead of `/` to delimit the regular expression. The second and third forms mean `g/`regular expression`/p`.

(`.`)`i
` <text>
`.` Insert the given text before the addressed line. Dot is left at the last line input, or, if there were none, at the line before the addressed line. This command differs from the a command only in the placement of the text.
(`.,.+1`)`j
`

| Join the addressed lines into a single line; intermediate newlines are deleted. Dot is left at the resulting line.

(`.`)`k` xMark the addressed line with name x, which must be a lower-case letter. The address form `'`x then addresses this line.
(`.,.`)`l
`

List. Print the addressed lines in an unambiguous way: a tab is printed as `\t`, a backspace as `\b`, backslashes as `\\`, and non-printing characters as a backslash, an `x`, and four hexadecimal digits. Long lines are folded, with the second and subsequent sub-lines indented one tab stop. If the last character in the line is a blank, it is followed by `\n`. An `l` may be appended, like `p`, to any non-I/O command.

(`.,.`)`m` a

| Move. Reposition the addressed lines after the line addressed by a. Dot is left at the last moved line.

(`.,.`)`n
`

| Number. Perform `p`, prefixing each line with its line number and a tab. An `n` may be appended, like `p`, to any non-I/O command.

(`.,.`)`p
`

| Print the addressed lines. Dot is left at the last line printed. A `p` appended to any non-I/O command causes the then current line to be printed after the command is executed.

(`.,.`)`P
`

| This command is a synonym for `p`.

`q` Quit the editor. No automatic write of a file is done. A `q` or `e` command is considered to be in error if the buffer has been modified since the last `w`, `q`, or `e` command.
`Q` Quit unconditionally.
(`$`)r filename

| Read in the given file after the addressed line. If no filename is given, the remembered file name is used. The file name is remembered if there were no remembered file name already. If the read is successful, the number of characters read is printed. Dot is left at the last line read from the file.

(`.,.`)`s` n`/`regular expression`/`replacement`/
` (`.,.`)`s` n`/`regular expression`/`replacement`/g
` (`.,.`)`s` n`/`regular expression`/`replacement

Substitute. Search each addressed line for an occurrence of the specified regular expression. On each line in which n matches are found (n defaults to 1 if missing), the nth matched string is replaced by the replacement specified. If the global replacement indicator `g` appears after the command, all subsequent matches on the line are also replaced. It is an error for the substitution to fail on all addressed lines. Any character other than space or newline may be used instead of `/` to delimit the regular expression and the replacement. Dot is left at the last line substituted. The third form means `s` n`/`regular expression`/`replacement`/p`. The second `/` may be omitted if the replacement is empty.
An ampersand `&` appearing in the replacement is replaced by the string matching the regular expression. The characters `\`n, where n is a digit, are replaced by the text matched by the n-th regular subexpression enclosed between `(` and `)`. When nested parenthesized subexpressions are present, n is determined by counting occurrences of `(` starting from the left.
A literal `&`, `/`, `\` or newline may be included in a replacement by prefixing it with `\`.

(`.,.`)`t` a

| Transfer. Copy the addressed lines after the line addressed by a. Dot is left at the last line of the copy.

(`.,.`)`u
`

| Undo. Restore the preceding contents of the first addressed line (sic), which must be the last line in which a substitution was made (double sic).

(`1,$`)`v/`regular expression`/`command list

| This command is the same as the global command `g` except that the command list is executed with dot initially set to every line except those matching the regular expression.

(`1,$`)`w` filename

| Write the addressed lines to the given file. If the file does not exist, it is created with mode 666 (readable and writable by everyone). If no filename is given, the remembered file name, if any, is used. The file name is remembered if there were no remembered file name already. Dot is unchanged. If the write is successful, the number of characters written is printed.

(`1,$`)`W` filename

| Perform `w`, but append to, instead of overwriting, any existing file contents.

(`$`)`=` Print the line number of the addressed line. Dot is unchanged.
`!`shell command

| Send the remainder of the line after the `!` to rc(1) to be interpreted as a command. Dot is unchanged.

(`.+1`)<newline>

An address without a command is taken as a `p` command. A terminal `/` may be omitted from the address. A blank line alone is equivalent to `.+1p`; it is useful for stepping through text.

If an interrupt signal (DEL) is sent, ed prints a `?` and returns to its command level.

When reading a file, ed discards NUL characters and all characters after the last newline.

FILES

| `/tmp/e* ed.hup` work is saved here if terminal hangs up

SOURCE

| `/usr/local/plan9/src/cmd/ed.c`

SEE ALSO

| sam(1), sed(1), regexp(7)

DIAGNOSTICS

| `?`name for inaccessible file; `?TMP` for temporary file overflow; `?` for errors in commands or other overflows.