ed(1)
is the truly Unix-minimalist way of plain-text editing. It dates from
the days of teletypes.[116] It has a simple, austere CLI, and there is
no screen display. In the following listing, computer output is
emphasized
.
ed sample.txt
sample.txt: No such file or directory
# This is a comment line, not a command.
# The message above warns that the sample.txt file is newly created.
a
the quick brown fox
jumped over the lazy dog
.
# That was an append command, which added text to the file.
# The dot on a line by itself terminated the append.
1s/f[a-z]x/dragon/
# On line 1, replace the first substring matching an f followed by a
# lowercase alphabetic followed by x with ‘dragon’. The
# substitute command accepts basic regular expressions.
1,$p
the quick brown dragon
jumped over the lazy dog
# Print all lines from 1 to the last.
w
51
# That wrote the file to disk. The ‘q’ command ends the
# editing session.
q
Unbelievable as it may seem to a modern reader, most of Unix's
original code was written with this editor. The reader with DOS
experience may recognize here the original on which
EDLIN was (crudely) modeled.
If one defines the job of an editor simply as enabling the user
to create and modify plain text files,
ed(1)
is entirely sufficient for the job. Importantly to the Unix view of
design correctness, it does nothing else. Many old-school Unix
programmers half-seriously maintain that all editors with more
features than ed has are simply bloated — and a few still who
seriously believe this.
Appropriately, ed was Ken Thompson's
deliberate simplification of the earlier
qed[RitchieQED] editor
— which was very similar (and the first editor to use regular
expressions in the characteristic Unix way) but had multiple-buffer
capability that Ken deliberately discarded. He judged it not worth
the additional complexity.
A notable characteristic of ed(1) and all its descendants is the
object-operation format of its commands (the session example shows an
explicit range on the ‘p’ command). There is a relatively
powerful syntax for specifying line ranges, either numerically, or by
regular-expression pattern match, or by special shorthands for the
current and last line. Most editor operations can be applied to any
range. This is a good example of orthogonality.
Nowadays,
ed(1)
is primarily used as a program-driven editing tool in scripts —
a role to which editors with more elaborate modes of interactivity are
unsuited. There is a close variant called
ex(1)
which adds a few useful interactivity features such as command
prompts; it is occasionally useful in rare cases when editing must be
done over a slow serial line, or in certain unusual crash-recovery
situations where the library support needed to run other editors is
not accessible. For these reasons, every Unix includes an
ed implementation and most include
ex as well.
The
sed(1)
stream editor mentioned in Chapter9 is also closely related to ed; many
of the basic commands are the same, though designed to be invoked
through command-line switches rather than from standard input.
Almost all Unix programmers have strayed from the path of
austerity and minimalist virtue enough to normally use editors that at
least present a roguelike, screen-oriented interface. However, the
fact that the religion of ed
persists[117] says a great deal that is worth
noting about the Unix mindset.