1 Overview ¶
GNU ed is a line-oriented text editor. It is used to create, display, modify and otherwise manipulate text files, both interactively and via shell scripts. A restricted version of ed, red, can only edit files in the current directory and cannot execute shell commands. Ed is the ’standard’ text editor in the sense that it is the original editor for Unix, and thus widely available. For most purposes, however, it is superseded by full-screen editors.
GNU ed is based on the editor algorithm described in Brian W. Kernighan and P. J. Plauger’s book "Software Tools in Pascal", Addison-Wesley, 1981.
If invoked with a file argument, then a copy of file is read
into the editor’s buffer. Changes are made to this copy and not directly to
file itself. Upon quitting ed, any changes not explicitly
saved with a ‘w’ command are lost. In interactive mode, a non-existing
file is reported but does not alter the exit status.
Editing is done in two distinct modes: command and input. When
first invoked, ed is in command mode. In this mode commands are
read from the standard input and executed to manipulate the contents of the
editor buffer. A typical command might look like:
,s/old/new/g
which replaces all occurences of the string old with new.
When an input command, such as ‘a’ (append), ‘i’ (insert) or
‘c’ (change), is given, ed enters input mode. This is the
primary means of adding text to a file. In this mode, no commands are
available; instead, the standard input is written directly to the editor
buffer. A line consists of the text up to and including a
newline character. Input mode is terminated by entering a single
period (‘.’) on a line.
Any change made to the buffer sets the modified flag even if the change does not cause a change in the contents of the buffer; for example ‘s/a/a/’. Reading an empty file (‘r file’) or terminating a command before it makes any change to the buffer (‘anewline.newline’) does not change the buffer, and therefore does not set the modified flag.
All ed commands operate on whole lines or ranges of lines; e.g.,
the ‘d’ command deletes lines; the ‘m’ command moves lines, and so
on. It is possible to modify only a portion of a line by means of
replacement, as in the example above. However even here, the ‘s’
command is applied to whole lines at a time.
In general, ed commands consist of zero or more line addresses,
followed by a single character command and possibly additional parameters;
i.e., commands have the structure:
[address[,address]]command[parameters]
The addresses indicate the line or range of lines to be affected by the command. If fewer addresses are given than the command accepts, then default addresses are supplied.