Using the ED or EDIT line editor


Is ED available?

On most Unix systems, there is a line editor available, called ed or edit. To see if the ed command is available, for instance, you can type:

        which ed
      
If you get a response like
        ed: Command not found
      
then ed is not available on your system, or you haven't set up your path so that it is available. If the ed command is available, then you will instead see something like
        /usr/bin/ed
      

Starting an edit session.

Assuming you are using the ed editor, then you can edit an existing file or create a new one simply by typing

        edit filename
      
You can also simply open the edit command by typing
        edit
      
In that case, the program assumes you are typing information that is to go into a new file which you haven't named yet. (You do that using the w command, and including the name of the file then.)

If you were working on a file using the editor, and your edit session was terminated unexpected, then your changes will be lost. Actually, though, the editor usually saves a copy of your commands. In that case, you may be able to recover your work by typing

        edit -r filename
      
This will try to restart the editor on the old file, reading in the saved copy of your commands from the interrupted edit session.

Finishing an edit session

When you are done making changes, you would normally terminate by "writing" the changes, (that is, saving them), and then quiting. These two commands can be combined into a single input to the editor:

        wq
      
Since the "w" command saves your changes, you can issue it at any time.
        w
      
When you write your changes, the old version of your current file is replaced. At any time, you can specify that you want the changes to go to a different file by a command like:
        w newfile
      
Once you specify a file in this way, it becomes your current file; that is, if you make some more changes inside the editor, and use the plain "w" command, the updated information will again go to newfile.

The current line

When you are editing a file, the editor numbers each line of the file, and places the initial editing position at a new line just after the last line of the input file. The editor always keeps track of the current line you are editing. This line can be referred to by its number, or by the shorthand name ".". In particular, to see the number of the current line, you can type the curious expression:

        . =
      
and the editor should respond with something like
        line 2946
      

To print out the text of the current line, you can use the "print" command

        p
      
but if you want to print a particular line, you can specify its number first:
        12 p
      
and to print out a range of lines, you specify the first and last, separated by a comma:
        10,25 p
      

Moving around in the file

To move to a particular line, you can simply type its number.

        2946
      
To move to the last line of the file, you can simply type
        $
      
because "$" is another name for the last line of the file. For obvious reasons, the name of the first line is just "1"!

Hitting RETURN will move you to the next line of the file, and should also display its content. Thus, you could actually print out the first 10 lines of text of your file by typing "1" to go to line 1, and then hitting RETURN 10 times.

You can also move by using the relative motion operators

        +5
      
moves ahead 5 lines, and
        -11
      
moves back 11 lines.

You can also move to the next line containing a particular string by typing the string preceded by a slash:

        /Fred says
      
will find the next line containing the string "Fred says". Now you often want to keep looking for the next line with that same string. To do this, you need only type the slash again!
        /
      

Other commands

While you are editing the file, there are a variety of commands that allow you to move to specific locations in the file, search for certain patterns, change or substitute items.

j
Joins the current and next lines into one line.
r filename
Inserts the text of the named file into the current file.
s/pat1/pat2
replaces the first occurrence of "pat1" by "pat2" on the current line.
s/pat1/pat2/g
replaces all occurrences of "pat1" by "pat2" on the current line.
1,$s/pat1/pat2/g
replaces all occurrences of "pat1" by "pat2" throughout the file.
s/^Bob/Joe/
replaces occurrences of "Bob" by "Joe", but only if Bob begins the line. "^" symbolizes the left margin.
s/Bob$/Bob./
replaces occurrences of "Bob" by "Bob.", but only if Bob ends the line. "$" represents the right margin.

You can return to the HTML web page.


Last revised on 12 September 2005.