The ed editor

It might happen as system administrator to have to work, especially to troubleshoot something, on an old Linux distro or Unix system where you don’t have none of the following known editors: vi, Emacs, Joe, ex, Sam or sed. In this case you will have at least ed, one of the oldest Unix inline text editor. So, you have guessed, this article will focus on this less famous, by today standard, editor. We will illustrate examples not just extracts from the man pages, which is not too much useful for novices.

The ed editor, like vi, works basically in two modes, edit and command. Once you start ed you will be presented with this screen without any prompt and in command mode:

Nothing is happening yet, you need to display the prompt by pressing P followed by Enter, as you can see an asterisk will be shown:

Afterwards you can type the letter i followed by newline (insert), to start typing something:

ed

Note that you must “announce” you want to finish editing with a . in a line on its own.
The number 13 is the number of characters written to the file.
You can save and exit ed with the command w (and the name of the file) and q, like in vi.

ed

To open an existing file we just runs ed followed by the filename.
Because is a line editor we won’t see the all content of the file by default, but just how many characters we have.
Let’s say we want to see its content. In this case we simply type p and press enter:

ed

We want to add the same word but in the different language, so we switch into “insert” mode
So as before we switch into command mode and with i we edit and add the line below, then we save without specifying
the filename as we already did by opening this file:

ed

Now we want to display the whole content of the file, we can do that with the command ,p
We can also this display the last line by simply using p

ed

As you can see the last line we typed is, by mistake, on top and not in the bottom.
There is also a small typo : a missing exclamation mark; let’s fix this typo first.
We just move to the desired line by typing its number followed by Enter
Then to amend the line we will use a sed like syntax.


OK we have selected the line and now we type command
s/$/!/p
to add that specific character at the end of the line ($ means EOL).

ed

Now it’s time to move our line to the bottom: you basically specify with the command m the desired line number. We must know the total number of lines as we cannot enter a not existing line.

ed

Now we want to add a new line or better to append it, we will use the command a for that.

ed

Now, let’s say we want to remove a line, we will use d for that.

ed

Finally, let’s say we want to append the result of a command, we can use the command !r for that:

ed

Note that the ! alone will just have the aim to run some system command without exiting ed, like in vi.ed

This is it: I hope you found interesting even if you won’t be using it often.