Using ed, the Unix line editor

From time to time I enjoy going back in time and drink some inspiration from old tools and software.

One of the reasons I use Vim is because it’s readily available on most machines. If that’s the case, then I should also learn how to use ed!

Ed is a line editor for the Unix operating system, developed in 1969 by Ken Thompson. To understand ed and how it works we should first understand what a line editor is.

A line editor is a text editor in which each editing command applies to one or more complete lines of text. Line editors predate screen based editors like vi, and originated in an era where a computer operator interacted with a teleprinter (a printer with a keyboard), with no video display, and no ability to move a cursor within a document.

An old teleprinter.

This explains why ed focus on lines instead of screens. This also explains why ed is not so intuitive and not natural to someone coming from the world of screen based editors. To add insult to injury, ed is from a time where memory was very very precious, so using abbreviated commands and very succinct error messages made a lot of sense, but contributed to the overall confusion.

Want to see what I’m talking about?

First of all be sure that you have ed installed on your machine. It should be, but in case it’s not try apt-get install ed.

Fire up your terminal and enter the command:

$ ed

This will start ed. Notice that the only thing that happens is the cursor moving to a new line. Don’t worry, ed started. Now let’s do something wrong on purpose.


By pressing ‘b’ and ‘enter’ we get a not so friendly message ‘?’. But if we press ‘h’, we can see what went wrong.

Unknown command

Armed with this information we can now delve a bit deeper. (we can also press ‘H’ if we want ed to print the error messages every time, so let’s do it).

Unknown command

We can exit ed by pressing ‘q’ or ‘Q’ to exit unconditionally. So let’s do it right now.

Let’s launch ed again but this time lets specify a command prompt. This way we can differentiate when we are in command entering mode or when we are in text entering mode. For that we use the -p option.

$ ed -p\#

And now we’re ready for some basic text input. Note that we started ed without a filename, so, just like Vim, it will start an empty buffer for us.

To start entering line we press ‘a’.

This is line number 1.
Followed by line number 2.
Just next we have line number 3.
And we end with line number 4.

We exit the line entering mode by pressing ‘.’ by itself. We are back into command mode because we can see the prompt (#).

We just added 4 lines to the buffer. We can check that by pressing ‘p’ (which stands for print), which prints the fourth line, or by pressing ’n’, which will print both the line and the line number.

This happens because these commands operate on the current line, which in this case happens to be the fourth one. We can always refer to the current line by pressing ‘.’ or ‘.n’if we also want to know the line number.

4 And we end with line number 4.

We can move to a different line just by pressing its number.

Just next we have line number 3.
Just next we have line number 3.

Knowing which one is the current line is essential because with that knowledge we can add line after the current one by pressing ‘a’ and add lines before the current one by pressing ‘i’.

We can also replace a line with ‘c’ and delete a line with ‘d’.

I like this line 3 better.

Or deleting:


This cover most of our basic text entering needs. what we need now is a way to see our progress, a way to see our buffer or selected parts of it. For that we use ranges.

We can select the entire buffer by pressing ‘1,$p’ or just ‘,p’. For any range we can we the (from,to p) notation like ‘1,3p’.

This is line number 1.
Just next we have line number 3.
And we end with line number 4.

Also available is the ability to search the entire buffer for a particular occurrence. Just like in Vim we use ‘/string’ or ‘?string’ for a backward search.

Just next we have line number 3.

And finally, we might want to write and read files (saving and opening).

We can write the current buffer to a file using ‘w’. This will also print the number of bytes written:

#w test.txt

Like most commands in ed, ‘w’ also accepts a range, so we can write only a subset of our buffer.

#1,3w test.txt

We can also use ‘W’ to append to a file rather than replace it.

#2,3W test.txt

To read the contents of another file after the current line we use ‘r’.

#r test.txt

With this set of commands we can do some quick text work using ed. We can also understand the principles behind a line editor.

We only looked at a very basic set of tools available in ed. To know more, be sure to read the man page by going ‘man ed’ in your terminal.

Wrapping up

Of course ed is outdated, terse and unintuitive; but it’s a great exercise and always good to know. We never know when the machine we ssh into only has ed available ;)

And when that happens, it will be a piece of cake!