This article by no means aims to give comprehensive information about ed, instead it is meant to be a jumping point from where people can take up things on their own.

An average programmer spends more than half his time working on a text editor. The text editors used may vary from an ultra complex GUI text editor like notepad++ with some really snazzy features or perhaps the simple, humble, venerable vi.

The editors mentioned in the previous statement have one thing in common they are screen editors. They show text as it is being manipulated on the screen in real time.

But then there is a another class of editors which have almost faded out of existence. These are line editors of yore. These editors date back to the earliest computers, to a time when men were men and used teletypes instead of CRTs. Examples of such editors would include the ed editor in UNIX and edlin editor in dos. This article aims to educate the reader about line editors. This article uses ed for its examples.

Quite a few people question, as to why does such an editor like ed exist in modern UNIX distributions and who would use them to do anything?

One of the most important use of ed is to manipulate text in a file through a set of instructions. This feature ed a very important tool in any UNIX power users tool kit. For example, if you want to make an entry into your profile to update your path variable with another directory, you could either do this manually by opening the profile files, making an entry and exporting the variables. Or write a script that makes an entry into the file for you. Its in the second case where ed shows its utility, we will see how this can be done later in the article

Getting started

In this section I want you to get a feel of what it is to interact directy with ed. We will look into the nitty gritties later.

Ed like vi (actually vi like ed) has two different modes

Command Mode: Used to tell ed what you it want to do Input Mode: Used to enter text

By default when you start ed, you are put into the command mode. A set of commands (such as "a", "c") move you from command mode into the input mode. For starters lets use ed to create a very simple file

The set of command above are used to create a file called testfile. Lets take apart the sequence of commands above. First on line 2 you see "a". This tells ed to append text after the current line. Once you hit "a" and then enter, you are put into input mode. Here you can type text as if it were a normal editor the only restriction being that once you hit an enter on a line you cannot go back to that line. One line 7 you see a period. This tells ed to move you back into the command mode. Now that you have typed your text you would like to save it. The "w" on Line 8 does just that and the "q" quits ed.

Lets try another example

In the example above we are opening the file and looking into its contents. Notice when we open the file with the command on line 1 ed prints out the number of bytes in the file. On line 3 we give a command to print all the lines in the file(More on this in the addressing section. Finally on line 9 we quit the editor.

From the example above(line 3) we can observe a very important point. That is how ed takes in its instructions. Almost all instructions given to ed is in the format

(Address)(Command)

Since ed unlike other editors does not allow users to visually modify text. It provides an alternative means of doing so this is by means of using addresses. More about this in the addresses section.

ed provides a set of commands that can be used to perform almost any sort of modification that a person would do on a visual editor. If you're familiar with vi this should be a breeze. Actually from a historical perspective vi is a visual version of ed.

Finally lets try out one more example before we immerse ourselves into the nitty gritties of ed.

In the above example we try to access a line that does not exist...and ed replies with a mysterious question mark...to figure out the meaning of the question mark hit H and immediately you get the cause of the problem. This example was meant to show how to deal with ed when you encounter an error.

Addressing

Unlike visual editors a user cannot use arrow keys to move around lines in a buffer in ed. Instead ed provides us with a mechanism by which we specify the address of the line that line we want to edit and ed modifies that line for us based on the command. There are two types of addressing

Line Based Addressing: e user specifies the line mber of the line that he wants to edit and ed uses that line to ply its commands. The example below shows you how to move about a le using line based addressing. We will use the test file created ove for all the examples.

In the example above we see that on line 3 that the user hits 1 and ed replies by spitting out the first line in the text file. Now to move to the next line the user can hit 2 and go on or he could simply press a series of +'s and navigate the file. For example We can also jump a number of line relative to a given line. This is illustrated in the example below . ed uses . to represent the current line its editing. So if type in whats on line 5 what you are essentially doing is that you are telling ed to move two lines from your current position. Once ed does this it responds by displaying the text thats on that line.

An important thing to remember is that when ed loads a file into memory the current line defaults to the last line in the file. The dollar($) symbol represents the last line in any file. Therefore sending a dollar to ed takes it to the last line.

A more concise way of refering to the idiom 1,$ is to use the % symbol. Then instead of writing 1,$p you can simply write %p

Context Based Addressing

ed also provides an alternate means of getting about a file, this is done by specifying a pattern(regular expression) that must be present in the line. Regular expressions are out of the scope of this tutorial, more information and a good tutorial about these can be found at this site. The example below illustrates a sample usage

In the example in line 3 we specify that we want a line that contains lots and ed jumps to that line and prints it on the screen.

I almost missed another point about addresses, you can specify a range of addresses over which you want your commands to work on, these ranges could consist completly of line number based addressing or context based addresses. For example if you want to print all the lines in a file you could possibly type the following.

Commands

ed provides a number of commands using which you can edit your text file. I have listed only the ones I have used the most in all my scripts. As mentioned earlier, please look into the manual page for a more comprehensive listing.

Printing Lines

We have seen enough of printing lines I will not explain this any further. Honestly I'm fed up of just printing lines.

Appending Lines

The "a" command appends lines after the current line. The example below illustrates this.

The change in the buffer is obvious but notice that I have used "Q" instead of "q". The capital q tells ed to quit with out saving.

Inserting Lines

The code section below shows the usage and effect of the "i" command.

Changing Lines

The code section below illustrates the effects of using "c"

Deleting Lines

The code section below illustrates how lines can be deleted from a file. Please note the usage of address ranges.

Writing to files

The code section below illustrates how files are written in ed.

ed also allows you to write a section of a buffer to another file the example below illustrates the same. The example below also illustrates the usage of ranges.

Substitution

This would be one of the most important reasons why you would use ed. Its syntax is as follows.

s/pattern/replacement/option

pattern: is a regular expression pattern that needs to be substituted

replacement: is the text that would be replaceing the pattern.

option: is used to modify the behaviour of the substitute command.

The most commonly used option is g. This tells substitute to replace every occurence of the regular expression in the text. Otherwise by default ed replaces only the first expression. The "p" option can be used to look at the result of the substitution. The example below illustrates just that.

Examples

Now for the interesting part. I have put in two examples of what you could do with ed.

Example 1

The first example is trivial, it shows how you can remove the first line from a file, this seems easy but this is the most common thing that people when scripting come around asking for help. The tiny script below does just that.

Example 2

The example below is a actual script I use at home if I want to add a new directory to my existing path variable.

In the example above, we first delete all lines that export the PATH variable, we then go to the end of the file (line 10 and 19) and append our addition into the .bashrc and .bash_profile.

Please do send me comments about this tutorial, it would help me improve this as well as the ones I write in the future. Cheers Reuben

Further reading

The UNIX Programming Environment - Kernighan and Pike

This book shows some really amazing things that you can do with simple NIX utilties. Highly recommend this book to everyone interested in UNIX