next up previous contents
Next: Using vi Up: Editing Files Previous: What exactly is

Using ed

ed is a line-oriented text editor. The reason we learn about it is not only that it is available on nearly all systems, but also that it works on most devices from teleprinters to high end graphics stations. Most workstations only have a line oriented text console in their basic state when no graphics interface is running, so full-screen text editors cannot be used. Line editing means that a whole line has to be typed at a time. This is important to bear in mind when using ed.

ed has two distinct modes: command and input. When first started ed is in command mode. If it is given a file name on the command line it will load that file into its text buffer. All changes made to the text buffer will only be saved to the file when the 'w' command is issued, otherwise all changes are lost. If no file name is given on the command line, the text buffer is empty initially.

In command mode commands are read from standard input and executed, manipulating the contents of the text buffer.

When ed is in command mode an input command like 'a' (append), 'i' (insert) or 'c' (change) will switch ed into input mode. In this mode no commands are available and the standard input is written directly into the text buffer. Input mode is ended by typing a single '.' on a line by itself followed by a return.

When started without a file name ed just sits in command mode without any prompt, waiting for user input; when ed is started with a filename it displays the number of characters in the file and then waits for user input. If we issue the 'P' command, ed will display a prompt whenever it is in command mode. This is useful to distinguish the two modes, even if the prompt is just a '*'.

All ed commands take the same format. They start with an optional range of lines, followed by a command to be applied to that range of lines, which in some cases is followed by a set of parameters. If the range of lines is left out before a command, the command applies to the current line.

To append text after a line we use the 'a' command. To insert text into an empty file we type '0a' at the prompt:

 
$ed
P
*0a
Hello world.
.
*

To insert some text before a given line we use the 'i' command:

*1i
This is before Hello world.
.
*

To list what we have done so far we can use the 'l' command. The first line is always 1 and the last line $:

*1,$l
This is before Hello world.$
Hello world.$
*

If we want to change a line we use the 'c' command:

*1c
Listen everybody:
.
*1,2l
Listen everybody:$
Hello world.$
*

To delete lines we use the d command:

*1d
*1,$l
Hello world.$
*

To write our work to disk we use the w command. If we gave a file on the command line, we can just type w; if we started a new file we have to follow w with the file name to save the text as:

*w a
13
*

To quit from ed we just use the 'q' command:

*q
$

ed has a whole range of searching and replacing commands that can be run in very flexible ways, but we will not discuss them here as ed is usually only needed in very extreme cases. For further information see appendix gif.



next up previous contents
Next: Using vi Up: Editing Files Previous: What exactly is



Mark O. Stitson
Wed Sep 25 10:45:32 BST 1996