This appendix explains how to create, edit (modify), display, and save text files with ed, a line editing program. If your system has another editing program, you may want to learn how to do these tasks with that program.
A good way to learn how ed works is to try the examples in this appendix on your system. Since the examples build upon each other, it is important for you to work through them in sequence. Also, to make what you see on the screen consistent with what you see in this guide, it is important to do the examples just as they are given.
In the examples, everything you should enter is printed in boldface. When you are told in the text to enter something, you should enter all of the information for that line and then press Return.
Because ed is a line editor, you can work with the contents of a file only one line at a time. Regardless of what text is on the screen, you can edit only the current line. If you have experience with a screen editing program, you should pay careful attention to the differences between that program and ed. For example, with the ed program, you cannot use the Cursor Up and Cursor Down keys to change your current line.
A file is a collection of data stored together in the computer under an assigned name. You can think of a file as the computer equivalent of an ordinary file folder - it may contain the text of a letter, a report, or some other document, or the source code for a computer program.
The edit buffer is a temporary storage area that holds a file while you work with it - the computer equivalent of the top of your desk. When you work with a text file, you place it in the edit buffer, make your changes to the file (edit it), and then transfer (copy) the contents of the buffer to a permanent storage area.
The rest of this appendix explains how to create, display, save, and edit (modify) text files with the ed editor.
To create and save a text file, perform the following steps. The following sections describe these steps in detail.
$
ed
filename
The filename argument is the name of the file you want to create or edit.
a
w
q
To start the ed program, enter a command of the form ed filename after the shell prompt ($).
In the following example, the ed afile command starts the ed program and indicates that you want to work with a file named afile:
$
ed afile
?afile
_
The ed program responds with the message ?afile, which means that the file does not exist. You can now use the a (append) subcommand (described in the next section) to create afile and put text into it.
To put text into your file, enter a. The a subcommand tells ed to add, or append, the text you enter to the edit buffer. If your file had already contained text, the a subcommand would add the new text to the end of the file.
Type your text, pressing Return at the end of each line. When you have entered all of your text, enter a dot (.) at the start of a new line.
Note
If you do not press Return at the end of each line, the ed program automatically moves your cursor to the next line after you fill a line with characters. However, ed treats everything you enter before you press Return as one line, regardless of how many lines it takes up on the screen; that is, the line wraps around to the beginning of the next line (based upon your workstation display settings).
The following example shows how to enter text into the afile file:
a
The only way to stop
appending is to enter a
line that contains only
a dot.
.
_
If you stop adding text to the buffer and then decide to add some more, enter another a subcommand. Type the text and then enter a dot at the start of a new line to stop adding text to the buffer.
If you make errors as you enter your text, you can correct them before you press Return. Use the Backspace key to erase the incorrect character(s). Then enter the correct characters in their place.
Use the p (print) subcommand to display the contents of the edit buffer.
To display a single line, use the np subcommand, where n is the number of the line. For example:
2p
appending is to enter a
_
To display a series of lines, use the n,mp subcommand, where n is the starting line number and m is the ending line number. For example:
1,3p
The only way to stop
appending is to enter a
line that contains only
_
To display everything from a specific line to the end of the buffer, use the n,$p subcommand, where n is the starting line number and $ stands for the last line of the buffer. In the following example, 1,$p displays everything in the buffer:
1,$p
The only way to stop
appending is to enter a
line that contains only
a dot.
_
Note
Many examples in the rest of this appendix use 1,$p to display the buffer's contents. In these examples, the 1,$p subcommand is optional and convenient - it lets you verify that the subcommands in examples work as they should. Another convenient ed convention is ,p, which is equivalent to 1,$p - that is, it displays the contents of the buffer.
The w (write) subcommand writes, or copies, the contents of the buffer into a file. You can save all or part of a file under its original name or under a different name. In either case, ed replaces the original contents of the file you specify with the data copied from the buffer.
To save the contents of the buffer under the original name for the file, enter the w subcommand. For example:
w
78
_
The ed program copies the contents of the buffer into the file named afile and displays the number of characters copied into the file (78). This number includes blanks and characters such as Return (sometimes called newline), which are not visible on the screen.
The w subcommand does not affect the contents of the edit buffer. You can save a copy of the file and then continue to work with the contents of the buffer.
The stored file is not changed until the next time you use the w subcommand to copy the contents of the buffer into it. As a safeguard, it is a good practice to save a file periodically while you work on it. Then, if you make changes (or mistakes) that you do not want to save, you can start over with the most recently saved version of the file.
Note
The u (undo) subcommand restores the buffer to the state it was in before it was last modified by an ed subcommand. The subcommands that u can reverse are a, c, d, g, G, i, j, m, r, s, t, v, and V.
Often, you may need more than one copy of the same file. For example, you could have the original text of a letter in two files - one to keep as it is, and the other to be revised.
If you have followed the previous examples, you have a file named afile that contains the original text of your document. To create another copy of the file (while its contents are still in the buffer), use a subcommand of the form w filename, as the following example shows:
w bfile
78
_
At this point, afile and bfile have the same contents, since each is a copy of the same buffer contents. However, because afile and bfile are separate files, you can change the contents of one without affecting the contents of the other.
To save part of a file, use a subcommand of the form n,mw filename, where:
In the following example, the w subcommand copies lines 1 and 2 from the buffer into a new file named cfile:
1,2w cfile
44
_
Then ed displays the number of characters written into cfile (44).
Caution
The contents of the buffer are lost when you leave the ed program. To save a copy of the data in the buffer, use the w subcommand to copy the buffer into a file before you leave the ed program.
To leave the ed program, enter the q (quit) subcommand. For example:
q
$ _
The q subcommand returns you to the shell prompt ($).
If you have changed the buffer but have not saved a copy of its contents, the q subcommand responds with ?, an error message. At that point, you can either save a copy of the buffer with the w subcommand, or enter q again to leave the ed program without saving a copy of the buffer.
Before you can edit a file, you must load it into the edit buffer. You can load a file either at the time you start the ed program or while the program is running.
To load a file into the edit buffer when you start the ed program, enter the following command:
ed
filename
This command starts ed and loads the filename file into the edit buffer.
To load a file into the edit buffer while ed is running, you can enter one of the following commands:
This loads the filename file into the buffer, erasing any previous contents of the buffer.
This reads the filename file into the buffer after line n. If you do not specify n, ed adds the file to the end of the buffer.
To load a file into the edit buffer when you start the ed program, enter the name of the file after the ed command. The ed command in the following example invokes the ed program and loads the file afile into the edit buffer:
$
ed afile
78
_
The ed program displays the number of characters that it read into the edit buffer (78).
If ed cannot find the file, it displays ?filename . To create that file, use the a (append) subcommand (described in Section B.2.2) and the w (write) subcommand (described in Section B.2.4).
Once you start the ed program, you can use the e (edit) subcommand to load a file into the buffer. The e subcommand replaces the contents of the buffer with the new file. (Compare the e subcommand with the r subcommand, described next in Section B.3.3, which adds the new file to the buffer.)
Caution
When you load a new file into the buffer, the new file replaces the buffer's previous contents. Save a copy of the buffer with the w subcommand before you read a new file into the buffer.
In the following example, the e cfile subcommand reads the cfile file into the edit buffer, replacing afile. The e afile subcommand then loads afile back into the buffer, deleting cfile. The ed program returns the number of characters read into the buffer after each e subcommand (44 and 78):
e cfile
44
e afile
78
_
If ed cannot find the file, it returns ? filename. To create that file, use the a (append) subcommand, described in Section B.2.2, and the w (write) subcommand, described in Section B.2.4.
You can edit any number of files, one at a time, without leaving the ed program. Use the e subcommand to load a file into the buffer, make your changes to the file, and use the w subcommand to save a copy of the revised file. (See Section B.2.4 for information about the w subcommand.) Then use the e subcommand again to load another file into the buffer.
Once you have started the ed program, you can use the r (read) subcommand to read a file into the buffer. The r subcommand adds the contents of the file to the contents of the buffer. The r subcommand does not delete the buffer. (Compare the r subcommand with the e subcommand, described in Section B.3.2, which deletes the buffer before it reads in another file.)
With the r subcommand, you can read a file into the buffer at a particular place. For example, the 4r cfile subcommand reads the file cfile into the buffer following line 4. The ed program then renumbers all of the lines in the buffer. If you do not use a line number, the r subcommand adds the new file to the end of the buffer's contents.
The following example shows how to use the r subcommand with a line number:
1,$p
The only way to stop
appending is to enter a
line that contains only
a dot.
3r cfile
44
1,$p
The only way to stop
appending is to enter a
line that contains only
The only way to stop
appending is to enter a
a dot.
_
The 1,$p subcommand displays the four lines of afile. Next, the 3r cfile subcommand loads the contents of cfile into the buffer, following line 3, and shows that it read 44 characters into the buffer. The next 1,$p subcommand displays the buffer's contents again, letting you verify that the r subcommand read cfile into the buffer after line 3.
If you are working the examples on your system, complete the following steps before you go to the next section:
w cfile
e afile
The ed program is a line editor. This means that ed lets you work with the contents of the buffer one line at a time. The line you can work with at any given time is called the "current line", and it is represented by the dot (.). To work with different parts of a file, you must change the current line.
To display the current line, enter the following subcommand:
p
To display the line number of the current line, enter the following subcommand:
.=
Note
You cannot use the Cursor Up and Cursor Down keys to change the current line. To change the current line, use the ed subcommands described in the following sections.
To change your position in the buffer, do one of the following. These steps are described in detail in the following sections.
n
.+
n
.-
n
When you first load a file into the buffer, the last line of the file is the current line. As you work with the file, you usually change the current line many times. You can display the current line or its line number at any time.
To display the current line, enter p:
p
a dot.
_
The p subcommand displays the current line (a dot.). Because the current line has not been changed since you read afile into the buffer, the current line is the last line of the buffer.
Enter .= to display the line number of the current line:
.=
4
_
Since afile has four lines, and the current line is the last line in the buffer, the .= subcommand displays 4.
You also can use the dollar sign (the symbol that stands for the last line in the buffer) with the = subcommand to determine the number of the last line in the buffer:
$=
4
_
The $= subcommand is an easy way to find out how many lines are in the buffer. The ed $ symbol has no relationship to the shell prompt ($).
You can change your position in the buffer (change your current line) in one of two ways:
To move the current line to a specific line, enter the line number; ed displays the new current line. In the following example, the first line of afile becomes the current line:
1
The only way to stop
_
Pressing Return advances one line through the buffer and displays the new current line, as the following example shows:
appending is to enter a
line that contains only
a dot.
? _
When you try to move beyond the last line of the buffer, ed returns ?, an error message. You cannot move beyond the end of the buffer.
To set the current line to the last line of the buffer, enter $.
To move the current line backward through the buffer one line at a time, enter dashes (-) one after the other, as the following example shows:
- line that contains only - appending is to enter a - The only way to stop - ?_
When you try to move beyond the first line in the buffer, you receive the ? message. You cannot move beyond the top of the buffer.
To move the current line forward through the buffer more than one line at a time, enter .n (where n is the number of lines you want to move):
.2
line that contains only
_
Note that .2 is an abbreviation for .+2.
To move the current line backward through the buffer more than one line at a time, enter the following subcommand: .- n (where n is the number of lines you want to move):
.-2
The only way to stop
_
If you do not know the number of the line that contains a particular word or another string of characters, you can locate the line with a context search.
To search in context, do one of the following:
/string to find/
?string to find?
The following sections describe these methods of searching text in detail.
To search forward through the buffer, enter the string enclosed in slashes (//):
/only/
line that contains only
_
The context search (/only/) begins on the first line after the current line, then locates and displays the next line that contains the string "only". That line becomes the current line.
If ed does not find the string between the first line of the search and the last line of the buffer, then it continues the search at line 1 and searches to the current line. If ed searches the entire buffer without finding the string, it displays the ? error message:
/random/
?
_
Once you have searched for a string, you can search for the same string again by entering //. The following example shows one search for the string only, and then a second search for the same string:
/only/
The only way to stop
//
line that contains only
_
Searching backward through the buffer is much like searching forward, except that you enclose the string in question marks (?):
?appending?
appending is to enter a
_
The context search begins on the first line before the current line, and locates the first line that contains the string appending. That line becomes the current line. If ed searches the entire buffer without finding the string, it stops the search at the current line and displays the message ?.
Once you have searched backward for a string, you can search backward for the same string again by entering ??. This is because ed remembers search strings.
You can change the direction of a search for a particular string by using the slash (/) and question mark (?) search characters alternately:
/only/
line that contains only
??
The only way to stop
_
If you go too far while searching for a character string, it is convenient to be able to change the direction of your search.
Use the s (substitute) subcommand to replace a character string (a group of one or more characters) with another. The s subcommand works with one or more lines at a time, and is especially useful for correcting typing or spelling errors.
To make substitutions, do one of the following:
s/oldstring/newstring/
ns/oldstring/newstring/
n,ms/oldstring/newstring/
The following sections describe these methods of substitution in detail.
To make a substitution on the current line, first make sure that the line you want to change is the current line. In the following example, the /appending/ (search) subcommand locates the line to be changed. Then the s/appending/adding text/p (substitute) subcommand substitutes the string "adding text" for the string "appending" on the current line. The p (print) subcommand displays the changed line.
/appending/
appending is to enter a
s/appending/adding text/p
adding text is to enter a
_
Note
For convenience, you can add the p (print) subcommand to the s subcommand (for example, s/appending/adding text/p). This saves you from having to enter a separate p subcommand to see the result of the substitution.
The s subcommand changes only the first occurrence of the string on a given line. To learn how to change all occurrences of a string on the line, see Section B.6.4.
To make a substitution on a specific line, use a subcommand of the following form:
ns/oldstring/newstring/
Here n is the number of the line on which the substitution is to be made. In the following example, the s subcommand moves to line number 1 and replaces the string "stop" with the string "quit" and displays the new line:
1s/stop/quit/p
The only way to quit
_
The s subcommand changes only the first occurrence of the string on a given line. To learn how to change all occurrences of a string on the line, see Section B.6.4.
To make a substitution on multiple lines, use a subcommand of the following form:
n,ms/oldstring/newstring/
Here n is the first line of the group and m is the last. In the following example, the s subcommand replaces the first occurrence of the string "to" with the string "TO" on every line in the buffer:
1,$s/to/TO/
1,$p
The only way TO quit
adding text is TO enter a
line that contains only
a dot.
_
The 1,$p subcommand displays the contents of the buffer, which lets you verify that the substitutions were made.
Ordinarily, the s (substitute) subcommand changes only the first occurrence of a string on a given line. However, the g (global) operator lets you change every occurrence of a string on a line or in a group of lines.
To make a global substitution on a single line, use a subcommand of the following form:
ns/oldstring/newstring/
In the following example, 3s/on/ON/gp changes each occurrence of the string "on" to "ON" in line 3 and displays the new line:
3s/on/ON/gp
line that cONtains ONly
_
To make a global substitution on multiple lines, specify the group of lines with a subcommand of the form:
n,ms/oldstring/newstring/g
In the following example, 1,$s/TO/to/g changes the string "TO" to the string "to" in every line in the buffer:
1,$s/TO/to/g
1,$p
The only way to quit
adding text is to enter a
line that cONtains ONly
a dot.
_
You can use the s (substitute) subcommand to remove a string of characters (that is, to replace the string with nothing). To remove characters, use a subcommand of the form s/oldstring// (with no space between the last two / characters).
In the following example, ed removes the string "adding" from line number 2 and then displays the changed line:
2s/adding//
text is to enter a
_
Two special characters let you make substitutions at the beginning or end of a line:
To make a substitution at the beginning of a line, use the s/^/newstring subcommand. In the following example, one s subcommand adds the string "Remember" to the start of line number 1. Another s subcommand adds the string "adding" to the start of line 2:
1s/^/Remember,/p
Remember, The only way to quit
2s/^/adding/p
adding text is to enter a
_
To make a substitution at the end of a line, use a subcommand of the form s/$/newstring. In the following example, the s subcommand adds the string "Then press Enter." to the end of line number 4:
4s/$/ Then press Enter./p
a dot. Then press Enter.
_
Notice that the substituted string includes two blanks before the word Then to separate the two sentences.
If you do not know the number of the line you want to change, you can locate it with a context search. See Section B.5 for more information on context searches.
For convenience, you can combine a context search and a substitution into a single subcommand in the following format:
/string to find/s/oldstring/newstring/
In the following example, ed locates the line that contains the string ", The" and replaces that string with ", the":
/, The/s/, The/, the/p
Remember, the only way to quit
_
Also, you can use the search string as the string to be replaced with a subcommand of the form /string to find/s//newstring/. In the following example, ed locates the line that contains the string "cONtains ONly", replaces that string with "contains only", and prints the changed line:
/cONtains ONly/s//contains only/p
line that contains only
_
Use the d (delete) subcommand to remove one or more lines from the buffer. The general form of the d subcommand is the following:
starting line,ending lined
After you delete lines, ed sets the current line to the first line following the lines that were deleted. If you delete the last line from the buffer, the last remaining line in the buffer becomes the current line. After a deletion, ed renumbers the remaining lines in the buffer.
To delete lines from the buffer, do the following:
d
nd
n,md
The following sections describe these methods of deleting lines in detail.
If you want to delete the current line, enter d. In the following example, the 1,$p subcommand displays the entire contents of the buffer, and the $ subcommand makes the last line of the buffer the current line:
1,$p
Remember, the only way to quit
adding is to enter a
line that contains only
a dot. Then press Enter.
$
a dot. Then press Enter
d
_
The d subcommand then deletes the current line (in this case, the last line in the buffer).
If you know the number of the line you want to delete, use a subcommand of the form nd to make the deletion. In the following example, the 2d subcommand deletes line 2 from the buffer:
2d
1,$p
Remember, the only way to quit
line that contains only
_
The 1,$p subcommand displays the contents of the buffer, showing that the line was deleted.
To delete a group of lines from the buffer, use a subcommand of the form n,md, where n is the starting line number and m is the ending line number of the group to be deleted.
In the following example, the 1,2d subcommand deletes lines 1 and 2:
1,2d
1,$p
?
_
The 1,$p subcommand displays the ? message, indicating that the buffer is empty.
If you are following the examples on your system, you should restore the contents of the buffer before you move on to the next section. The following example shows you how to restore the contents of the buffer:
e afile
?
e afile
78
_
This command sequence reads a copy of the original file afile into the buffer.
Use the m (move) subcommand to move a group of lines from one place to another in the buffer. After a move, the last line moved becomes the current line.
To move text, enter a subcommand of the following form:
x,ymz
The x variable is the first line of the group to be moved. The y variable is the last line of the group to be moved. The z variable is the line the moved lines are to follow.
In the following example, the 1,2m4 subcommand moves the first two lines of the buffer to the position following line 4:
1,2m4
1,$p
line that contains only
a dot.
The only way to stop
appending is to enter a
_
The 1,$p subcommand displays the contents of the buffer, showing that the move is complete.
To move a group of lines to the top of the buffer, use zero (0) as the line number for the moved lines to follow. In the next example, the 3,4m0 subcommand moves lines 3 and 4 to the top of the buffer:
3,4m0
1,$p
The only way to stop
appending is to enter a
line that contains only
a dot.
_
The 1,$p subcommand displays the contents of the buffer, showing that the move was made.
To move a group of lines to the end of the buffer, use $ as the line number for the moved lines to follow:
1,2m$
1,$p
line that contains only
a dot.
The only way to stop
appending is to enter a
_
Use the c (change) subcommand to replace one or more lines with one or more new lines. The c subcommand first deletes the line(s) you want to replace and then lets you enter the new lines, just as if you were using the a (append) subcommand. When you have entered all of the new text, enter a dot (.) on a line by itself.
The general form of the c subcommand is:
starting line,ending linec
To change lines of text, do the following:
n,mc
The n variable specifies the number of the first line of the group to be deleted. The m variable specifies the number of the last line of the group (or the only line) to be deleted.
The following sections describe these methods of searching text in detail.
To change a single line of text, use only one line number with the c (change) subcommand. You can replace the single line with as many new lines as you like.
In the following example, the 2c subcommand deletes line 2 from the buffer, and then you can enter new text:
2c
appending new material is to
use the proper keys to create a
.
1,$p
The only way to stop
appending new material is to
use the proper keys to create a
line that contains only
a dot.
_
The dot on a line by itself stops ed from adding text to the buffer. The 1,$p subcommand displays the entire contents of the buffer, showing that the change was made.
To change more than one line of text, give the starting and ending line numbers of the group of lines to be with the c subcommand. You can replace the group of lines with one or more new lines. In the following example, the 2,3c subcommand deletes lines 2 and 3 from the buffer, and then you can enter new text:
2,3c
adding text is to enter a
.
1,$p
The only way to stop
adding text is to enter a
line that contains only
a dot.
_
The dot on a line by itself stops ed from adding text to the buffer. The 1,$p subcommand displays the entire contents of the buffer, showing that the change was made.
Use the i (insert) subcommand to insert one or more new lines of text into the buffer. To locate the place in the buffer for the lines to be inserted, you can use either a line number or a context search. The i subcommand inserts new lines before the specified line. (Compare the i subcommand with the a subcommand, explained in Section B.2.2, which inserts new lines after the specified line.) To insert text, do the following:
ni
The n variable specifies the number of the line the new lines will be inserted above.
/string/i
The string variable specifies a group of characters contained in the line the new lines will be inserted above.
The following sections describe these methods of inserting text in detail.
If you know the number of the line where you want to insert new lines, you can use an insert subcommand of the form ni (where n is a line number). The new lines you enter go into the buffer before line number n. To end the i subcommand, enter a dot (.) on a line by itself.
In the following example, the 1,$p subcommand prints the contents of the buffer. Then the 4i subcommand inserts new lines before line number 4:
1,$p
The only way to stop
adding text is to enter a
line that contains only
a dot.
4i
--repeat, only--
.
1,$p
The only way to stop
adding text is to enter a
line that contains only
--repeat, only--
a dot.
_
After 4i, you enter the new line of text and enter a dot on the next line to end the i subcommand. A second 1,$p subcommand displays the contents of the buffer again, showing that the new text was inserted.
Another way to specify where the i subcommand inserts new lines is to use a context search. With a subcommand of the form /string/i, you can locate the line that contains string and insert new lines before that line. When you finish inserting new lines, enter a dot on a line by itself.
In the following example, the /dot/i subcommand inserts new text before the line that contains the string "dot":
/dot/i
and in the first position--
.
1,$p
The only way to stop
adding text is to enter a
line that contains only
--repeat, only--
and in the first position--
a dot.
_
The 1,$p subcommand displays the entire contents of the buffer, showing the new text.
With the
t
(transfer) subcommand, you can copy lines from one
place in the buffer and insert the copies elsewhere. The
t
subcommand does not affect
the original lines.
The general form of the
t
subcommand is:
starting line,ending linetline to follow
To copy lines, enter a subcommand of the form:
n,mtx
The n variable specifies the first line of the group to be copied. The m variable specifies the last line of the group to be copied. The x variable specifies the line the copied lines are to follow.
To copy lines to the top of the buffer, use zero (0) as the line number for the copied lines to follow. To copy lines to the bottom of the buffer, use the dollar sign ($) as the line number for the copied lines to follow.
In the following example, the 1,3t4 subcommand copies lines 1 through 3, and inserts the copies after line 4:
1,3t4
1,$p
The only way to stop
adding text is to enter a
line that contains only
--repeat, only--
The only way to stop
adding text is to enter a
line that contains only
and in the first position--
a dot.
_
The 1,$p subcommand displays the entire contents of the buffer, showing that ed has made and inserted the copies, and that the original lines are not affected.
Sometimes you may find it convenient to use a system command without leaving the ed program. At these times you can use the exclamation point (!) character to leave the ed program temporarily.
To use a system command from ed, enter the following:
!command
In the following example, the !ls command temporarily suspends the ed program and runs the ls (list) system command (a command that lists the files in the current directory):
!ls
afile
bfile
cfile
!
_
The ls command displays the names of the files in the current directory (afile, bfile, and cfile), and then displays another ! character. The ls command is finished, and you can continue to use ed.
You can use any system command from within the ed program. You can even run another ed program, edit a file, and then return to the original ed program. From the second ed program, you can run a third ed program, use a system command, and so forth.
This completes the introduction to the ed program. To save your file and end the ed program, perform the following steps:
w
q
For a full discussion of the w and q subcommands, see Section B.2.4 and Section B.2.5 respectively.
For information about other features of ed, see the ed(1) reference page.
For information about printing the files you create with ed, see Chapter 3.