HMC Homepage      CS Home

Using ed: The Standard Editor

Before you start using ed

Read this before you read the rest of the qref. You will probably never need to use ed. You should avoid using ed at all costs. You should use vim or emacs instead of ed because it is VERY user-UNfriendly. Consider yourself warned.

Intro

Commands to ed are entered by pressing a single letter, followed by the <enter> key. Most of those commands can (optionally) be preceded by an address or address range (as explained in the "Viewing the file" section). A few commands may be followed by arguments, as explained within each command's documentation.

Starting ed

Starting ed is simple:

% ed filename

  • If the file does exist, ed will read the file, and print the number of characters in it.
  • If file does not exist, ed will complain, by saying "?filename". This is not a problem; when you tell ed to save the file (using w with no arguments), it will save it to the filename given (unless you tell it otherwise; see the "Saving Files" section for details).
  • If you start ed without a filename, you will need to give the save (w) command a file to write to.

Moving around in ed

Instead of showing you an entire screen of text, ed only keeps track of a line at a time. At any time, you are on a specific line, designated by "." (period). The last line of the file is indicated by the "$" (dollar-sign). To see the current line number, enter the command n. This will output the line number, followed by a tab, followed by the contents of the line.

When you start ed, you begin at the last line. To move to a different line, simply type the line number (to move to the top of the file, for example, type 1). When you move to a line, ed echoes the contents of the line.

You can also enter relative line numbers. For example, -2 will go back two lines, and +5 will go forward five. This also works with the "." and "$" special lines; for example, $-1 will give you the second-to-last line, and .+3 will jump forward three lines (which is the same as the +3 command).

Pressing <Enter> makes ed go to the next line and print it. If there is no next line (i.e., you're at the end of the file), ed gives you its error message, "?". "Viewing the File" gives you more ways to look at the file.

Viewing the File

To print the current line, use the command p.

To print several lines, prefix p with the start and end line numbers separated by a , (comma). For example, to list lines 10 to 20 of a file, use the command 10,20p . In addition to displaying lines 10 through 20 (inclusive), this sets the current line number to the last line displayed (20, in this case).

The special characters (. and $) also work with p (and any other command). In addition, there are two other special characters that should be introduced: , (comma) and ; (semicolon). The comma is equivalent to the address pair "1,$" (i.e., the entire file), and the semicolon is equivalent to ".,$" (i.e., from the current line to the end of the file). For example, ",p" prints the entire file, while ";p" prints the contents of the file from the current line to the end.

To view whitespace characters, use l (L, not 1). Note that the end-of-line will be denoted by a "$" symbol, and various characters (most notably the backslash (\) and the tab, but there are others; consult the man page for details) will be printed as their escaped forms (\\ and \t, respectively).

Editing

Note: many of the commands accept an arbitrary number of lines of text. After entering such a command, you may type in as many lines of text as you want. Note that there will be no prompt. To signify that you are done entering text, you must input a line that has only a single . (period) or a single <Ctrl>-d.
  • a

    Appends text after the current line (by default). To specify the place to append text, specify a line number (i.e., 2a will append text between the 2nd and 3rd lines of the file; $a will append text to the end of the file). Zero is an allowed address for this command (unlike most other commands), it indicates that the text will be placed at the beginning of the file. End with "." or <Ctrl>-d.

  • c

    Changes line(s) of text in place. This command requires the address(es) of the line(s) to be replaced. Note that the lines specified will be deleted, and the new text inserted where they used to be. End with "." or <Ctrl>-d.

  • d

    Deletes line(s) of text. This command requires the address(es) of the line(s) to be deleted.

  • e filename

    Deletes the buffer, and replaces it with the file given. The current line is set to the last line of the buffer. The number of bytes read is printed.

    Note: if the buffer has not been saved, this command will output the error message. The E command, or the e command again, will force the loading of the given file, killing any unsaved changes.

  • i

    Inserts text before the current line, by default. Like append, except it inserts before the given line, not after. End with "." or <Ctrl>-d.

  • s/old/new

    Substitutes old with new. As given, ed replaces the first occurrence of old on the current line with new. The string old can contain regular expressions, using the same syntax as grep

    Putting a trailing g at the end of the substitution (i.e. s/old/new/g) will change every occurrence of the pattern on the line.

    The s// command can be prefixed with a range. For example, 2,5s/old/new will perform s/old/new on lines 2 through 5 -- note that it still only changes the first occurrence on each line.

    Putting it all together, we see that the command ,s/old/new/g will replace all occurrences of old with new in the entire file (the "," preceding the s// indicates that is should look at all lines; the /g means that all occurrences on all lines looked at).

    Caution: Using the s// to match or insert a &, %, \, or \digit (i.e., \0 - \9), is more involved. Consult the ed man page for details as to which characters should be escaped and how.

  • u

    "undo": cancels the effect of the previous command. Note that the undo command only undoes things that actually changed the buffer, not commands that only looked at it (i.e., ",l" will be ignored by the u command, as will any command that does not alter the contents of the buffer).

Saving

To save, use the w command. If you haven't already specified a filename (either on the command line or from a previous save), ed will print its error message (?), and do nothing. In this case (or if you want to save the file under a different name), specify the name of the file to save using the command w filename

After saving, the number of bytes in the file is printed.

Quitting

  • Use q to quit only if the file has saved.
  • Use Q to quit, whether the file is saved or not.
Note: the q command twice in a row will quit whether or not the file has been saved.

Helpful (?) Hints

ed is very user-unfriendly. However, these tips may help you in your editing:
  • ed's only error message is "?". If you give it a command with an argument that it doesn't recognize, it will give you the error message "? argument".
  • Needless to say, ed's error messages are not very helpful. The command h will tell you the reason for ed's most recent "?" output.
  • The H command makes ed toggle the "Help Mode". By default, it is off. When turned on, it explains the reason behind each ?.
  • From the man page:
    Some size limitations are in effect: 512 characters in a line, 256 characters in a global command list, and 255 characters in the path name of a file (counting slashes). The limit on the number of lines depends on the amount of user memory; each line takes 1 word.
  • ed has even more commands than those listed here. Most of them are fairly esoteric; frankly, if you need to use them, you should probably be using a different editor (vi or emacs). However, ed's man page has reasonably good documentation if you insist on using it for more complicated tasks; consult it for details.
Ed, the greatest WYGIWYG editor of all.
HMC Computer Science Department
Olin Science Center
301 Platt Blvd Claremont, CA 91711-5980 USA
PH : (909) 621-8225 FX : (909) 607-8364
Info Email: CS Staff or Admission Office
Last Modified Thursday, 21-Nov-2002 15:11:28 PST