Main Page | See live article | Alphabetical index

Ed

This article is about the UNIX text editor. There is another article about the television program and a list of people whose names start with Ed as well.

The text editor ed was the original standard on the UNIX operating system. It was influenced by an earlier editor known as qed, and went on to influence ex, which itself spawned vi.

In current practice it is almost never used interactively but does find use in some shell scripts. For interactive use, ed was subsumed by vi and emacs editors in the 1980s. ed can be found on virtually every version of UNIX and Linux available; people who have to work with multiple versions of UNIX often know at least the basic ed commands.

The syntax of ed influenced many UNIX tools such as sed, awk, and grep, and through them the Perl programming language.

Famous for its terseness, ed has almost no visual feedback. For example, the message that ed will produce in case of error, or when it wants to make sure you want to quit without saving is "?". It does not report the current filename or line number, unless requested.

This terseness was appropriate in the early versions of UNIX, when consoles were printers. modems were slow, and hard disk and memory were precious. These advantages ceased to apply when more interactive editors became the standards.

Example session

Here is an example transcript of an ed session:

a
ed is the standard UNIX text editor.
This is line number two.
.
2i

.
1,$l
ed is the standard UNIX text editor.$
$
This is line number two.$
3s/two/three/
1,$l
ed is the standard UNIX text editor.$
$
This is line number three.$
w text
65
q

Explanation of the example

Here we started with an empty file, and used a to append text (all ed commands are single letters). That put us into insert mode, which is terminated by a singular dot on a line. The two lines that we entered before the dot end up in the file buffer. 2i goes into insert mode, and will insert the entered text (a single empty line in our case) before line two. All commands may be prefixed by a line number and will operate on that line.

In 1,$l the l stands for the list command. This time we prefixed the command by a range, two lines separated by a comma ($ means the last line). In return, ed is listing all lines, from first to last. These lines are ended with dollar signs, so that white space at the end of lines is clearly visible.

We will correct the error in line 3 with 3s/two/three/, a substitution command. The 3 will apply it to the right line, following the command is the text to be replaced, and then the replacement. Listing all lines again with 1,$l we see that the line is correct now.

w text writes the buffer to the file "text". q will end our ed session.