In his book, Unix: A History and a Memoir, Brian Kernighan describes the origin of the first Unix system. A very brief summary is that Ken Thompson at Bell Labs was testing a disk scheduling algorithm, and realized it was only a few steps away from an operating system. Brian describes Ken’s work this way: “He needed to write three programs, one per week: an editor, so he could create code; an assembler, to turn the code into machine language that could run on the PDP-7; and a ‘kernel overlay’” that became the first Unix kernel. (p. 33)

That first editor was ed, which you can still find on Linux systems, usually implemented as GNU ed, although every implementation should work the same. ed is also called a line editor because it doesn’t use a screen display; it runs in plain text mode, scrolling text one line at a time from the bottom of the terminal. Let’s explore several of ed’s features by writing a sample C program.

A brief introduction

ed is a no-frills editor from a time when “terminals” didn’t exist as we know them today. In the 1960s and 1970s, computer operators interacted with systems using a device similar to a typewriter, printing lines of text to a long paper roll. That’s why ed doesn’t have a visual display mode like the vi editor.

Commands in ed are usually single letters, such as i to insert lines, a to append lines, l to list lines, and so on. Most commands may optionally be preceded by a number to indicate the line where the command should operate, or a pair of comma-separated numbers to indicate a range for the action. For example, 3i inserts new lines before line 3 in the file, and 6a appends new lines after line 6. The 5,9l command lists lines 5 through 9 in the file.

If you don’t provide line numbers before or after the comma, ed makes assumptions for the start and end of the range. For the sake of clarity, I’ll include line numbers in this article.

Edit a new file

Let’s write a sample program called ten to print the numbers 1 to 10, similar to the Linux seq command. We can start editing an empty source file by typing ed at the command line:

At first, you might not think anything has happened, because ed doesn’t show a prompt by default. To display an editor prompt, enter the uppercase P (Prompt) command and press return. You should see * as the default ed prompt:

Since we didn’t provide a filename on the command line, we start in an empty file. We can add new lines to the editor buffer with the i (insert) command. You won’t see a different prompt to let you know to enter text, but you can start typing away. When you’re done adding text, enter . (period) on a line by itself to go back into the ed prompt:

Making changes

For this example, I’ve entered the standard code for any new C program. However, I mis-typed the header file in the first line. We can fix that using the c (change) command. To change the first line, indicate the line number as 1c. Just as when inserting or appending new text, enter . (period) on a line by itself when you are done adding the new text:

You can also change a range of lines if you provide starting and ending line numbers. For example, enter 5,8c to replace lines 5 through 8 in a file with new text.

Let’s look at what we have so far. We can list lines in the buffer with the lowercase p (print) command. By itself, this prints the contents of the current line. Pair line numbers with the comma to show a range of lines, such as 1,3p to print the first three lines. Use $ to indicate the last line of the file, such as the 1,$p command to print the full contents of the buffer:

Another way to print the lines of the file is with the l (list) command. I think of this command as the print literal command, because it displays lines unambiguously, displaying $ at the end of every line and special characters as escape sequences. You can list a range of lines by providing starting and ending line numbers, the same way as the p (print) command.

One more way to display lines is with the n (number) command. I remember this command as print numbered because it precedes each line with a line number and a tab. Use this the same way as the p (print) or l (list) commands:

Replacing text

With this C code entered into a source file, we can add the guts of the program. Referring to our n (number) listing, we want to add new code between lines 5 and 6. We can do that with the i (insert) command, and provide the starting line number as 6i to insert new text before line 6.

Another way to add text is to put the new code after line 5. That’s the a (append) command, which can take a line number like 5a to append new lines after line 5. Don’t forget to enter . (period) on a line by itself when you are done:

If we review the source code we’ve written so far, we’ll find the code on lines 6 to 11 are not indented inside the main() function. That’s because I forgot to type extra spaces when I entered the lines. You can see how the lines lack indentation if you print the full buffer with 1,$n to print the file using line numbers:

We can do a “search and replace” of text in the file with the s (substitute) command. If you’ve used sed on the command line, ed’s s command works the same way with regular expressions. We can insert a few spaces at the start of lines 6 to 11 using a regular expression, such as ^ to match the start of each line, and specify the starting and ending lines for the substitution action:

Moving text around

Let’s add a brief comment at the top of the file, to describe what this program does. As we’ve seen, we can do this with the 1i command to insert new text before line 1:

Let’s say we wanted to move this comment, so it appears after the #include statement. We can use the m (move) command. This takes a slightly different syntax from the other commands, requiring the destination line number after the m command. For example, to move line 1 (the comment) and line 2 (a blank line) to the location after line 4, we enter 1,2m4 like this:

Moving text to the beginning of the file requires addressing line 0, which is technically “the line before line 1.” To relocate the comment and the blank line from lines 3 and 4 back to the start of the file, we would use the 3,4m0 command:

Save and quit

As with any computer system, you should save early and often. To save the file to disk, use the lowercase w (write) command, followed by a filename. ed reports back with the amount of data written to the file:

Once you’ve finished editing your file, and saved your changes, you can exit the ed editor with the q (quit) command:

Have fun with ed

ed is a great part of Unix history, and editing files in ed can be a fun experiment in retrocomputing. You can learn more about ed and how to use its commands with the GNU Info reader, using info ed. The regular manual page (man ed) shows a brief usage of the command line options. Here are two options you might try to make ed easier to use:

Change the default ed prompt with the -p string or --prompt=string option. Using --prompt='*' is the same as typing uppercase P (Prompt) after ed first starts up. Or you can set a new prompt to something you find more useful:

ed displays ? for any kind of error, including an incorrect command or invalid line numbers. For example, printing lines from an empty buffer is meaningless, so ed will show ? to indicate an error. You can get more information about the error with the h (help) command. Or start ed with the -v or --verbose option to always display this extra help.

Author

  • Jim Hall

    Jim Hall is an open source software advocate, developer, and technical writer. At work, Jim is CEO of Hallmentum, providing workshops and training to organizations. Jim is also the editor-in-chief of Technically We Write, an article-based community website about technical writing and technical communication.

Categories: Command LineLinux

Jim Hall

Jim Hall is an open source software advocate, developer, and technical writer. At work, Jim is CEO of Hallmentum, providing workshops and training to organizations. Jim is also the editor-in-chief of Technically We Write, an article-based community website about technical writing and technical communication.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *