Text

Text: ed

Ed is a line-oriented editor. The file being edited is copied into a buffer and you enter commands to manipulate the buffer. Lines of the buffer are only displayed when explicitly requested. Ed comes from a time, long long ago, when most terminals were hard copy terminals (i.e. a printer with a keyboard) and it was important to not waste paper and ink (and reprinting the entire file after each change just did not make sense).

Of course, we don’t work like that today, but knowledge of Ed is useful for two primary reasons. The first is that tools like Vim, Grep, Sed and Awk have their genesis in Ed. Once you use Ed directly this will become obvious and you will have a better grasp of your current tools.

The second is that you may, one day, find yourself in the shell of a remote system with a minimal configuration where your favorite editor is not available. Ed to the rescue.

Ed Basics

When you first open a file, Ed displays the number of characters in the file and positions you at the last line. This is known as the current address. You can read more about line addressing below.

$ ed test.txt
34
▌

is used to indicate the cursor position, you will see whatever your cursor character is.

The cursor is placed below the number of characters display and Ed is silently waiting for input. This can be confusing if you did not grow up using a printer as a terminal, but you can tell Ed to use a prompt using the -p option when invoking Ed.

$ ed -p ">" test.txt 
34
>▌

Ed has two modes, command mode and input mode. When you start Ed you are in command mode.

In command mode, you enter commands to interact with the buffer. After you enter a command, say p for print, you submit the command by pressing enter. By default a command applies to the current line (represented by the current address). You can read more about line addressing below.

If you enter a command that Ed does not understand, a ? is displayed. If you then enter the h command Ed will display an explanation of the error. Yes, this really is from a time when ink was expensive! You can toggle this behaviour so that error messages are always displayed by using the H command.

To change the current address you enter the line number and press enter, e.g. 13<cr> will make line 13 the current line. There are also a few special symbols that represent fixed locations in the file, you can read about those below.

When you enter an input command (e.g. a, i, or c) Ed enters input mode. In this mode standard input is written to the buffer. Lines consist of entered text up to and including the newline character. You exit input mode by entering a single period . on a line.

There are various commands for moving about and manipulating the file. You can read about those below.

You have to write the contents of the buffer back to the file on disk using the w command.

You can exit Ed using the quit command, q.

Command Format

The general Ed command format is (brackets represent optionality):

[address[,address]]command[parameters]

It consists of 0-2 line addresses, followed by a command (always a single character), and possibly followed by options relevant to the specific command.

The addresses specify the range of lines affected by the command. If addresses are not supplied then defaults are used.

Line Addressing

An address represents the number of a line in the buffer.

Notes on line addresses:

Address Symbols

These are the valid address symbols:

Commands

In the following examples the parenthesis represent the specification of an address range. The paranthesis are not included when entering the command. The period indicates that the current address is used as the default if an address is not supplied.

Many of these commands will feel familiar if you are a Vim user, they are similar (if not exactly the same) to common Vim commands.

A (not comprehensive) list of Ed commands:

Substitute Command

The substitute command is a little more involved and worthy of its own section.

Note:

Global Command

The global command is another command worthy of its own section.

Apply command-list to each of the addressed lines.

Note:

Variations:

Options

Scripting

All Ed commands can be entered using a file containing the commands.

$ ed test.txt < script

These commands are in the exact format you would enter interactively. This is a useful approach for common tasks.

Resources

The ed man page
$ man ed
If you want to cut ot the chase, the man page for ed is all you need.

The GNU ed line editor
https://www.gnu.org/software/ed/manual/ed_manual.html
The GNU manual for the ed editor.

Learning the vi and Vim Editors, 8th Edition, by Robbins and Hannah
https://learning.oreilly.com/library/view/learning-the-vi/9781492078791/
This book explains the history of ed and ex leading to Vi and then Vim and the history of the associated commands.

sed & awk, 2nd Edition, by Dougherty and Robbins
https://learning.oreilly.com/library/view/learning-the-vi/9781492078791/
Great book covering the usage of sed, awk, and ed. Includes a useful explanation of the history.

Tools

Web

Languages

Data