Using ed, the standard UNIX editor

Some time ago I wrote about the sam text editor, a UNIX text editor that incorporates many features that are cherished by UNIX users, such as regular expressions and programmability.

It turns out that many of these features were already present on the standard UNIX editor: ed. For many, it seems crazy that one might want to edit text in an editor that is not screen oriented, and is capable only of showing individual lines. However, sometimes this is not only OK, it is even desirable.

The most common case for me is when I am on a UNIX command line. Frequently there is a small change that needs to be performed to a file in order to continue. For example, this happens most of the time when I am compiling code with gcc or some other compiler that I might be using at the moment. After a compilation error happens, the compiler will say exactly what file is the culprit, and at what line the problem occurred.

The common UNIX user will then start their preferred screen-oriented editor to make the change. This can be one of vi, emacs, gedit, eclipse, or whatever editor you may be into. Then, after the editor starts (which, depending on the program, may take a few seconds — or minutes if it is eclipse), you need to locate the line reported by the compiler, and make the necessary change.

The point is that you don’t need to have access to the whole file to make this simple edit. You just need to see one line of the file. An editor such as emacs or vi will do the work, but it can only make it more confusing, by displaying all the other lines that you don’t need.

With ed, you can just type ed followed by the name of the file: ed will start immediately, since there is no UI to wait for. Finally you type the number of the line followed by enter, and the desired line is displayed. It is now just a matter of using a single command to change the line (either s or c, depending on the kind of changes you want to perform).

Another situation where ed is really useful is when you just want to add lines, either to the end or the beginning of a file. That’s another case when it doesn’t matter that you have access to a screen-oriented editor: you just need to enter text into a very specific point of the file. What you can very well do in this case is to forgo the screen editor and use ed to add as much text as you want to the beginning (using 1 followed by i) or end (using the “a” command).

So, you see that using ed has a lot to do with the way UNIX systems work. You always want to use the tool that is most efficient for the task at hand. If the task is simple, the best way to do it is using a simple tool such as ed.

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

One Response to “Using ed, the standard UNIX editor”

  1. Some of the more modern editors can be faster than ‘ED’ but “more haste, often results in less speed.”
    And the more complicated editors, such as Vim or Emacs require a massive amount of memorizing all the commands.

    I love being able to look at 4 or 5 lines of text at a time and edit those lines slowly but carefully with a small number of simple commands.

    Ed is simplicity itself – a pleasure to use.

    By d roberts on Dec 12, 2011

Post a Comment