On Unix-like operating systems, ed is an interactive file editor.

red, short for “restricted ed”, is a version of ed which carries restrictions about what types of editing can be performed.

Description

ed is one of the oldest editing programs around. It was introduced in 1969, approximately 50 years ago, as one of the original components of Unix.

  • Description
  • Syntax
  • Options
  • Technical description
  • Line addresses
  • Referring to line addresses
  • Address ranges
  • Regular expressions
  • Commands
  • Restricted ed
  • Examples
  • Related commands
  • Linux commands help

Syntax

ed [options] [file]

red [options] [file]

Options

Technical description

ed is a line-oriented text editor with a minimal interface. It is used to create, display, modify and otherwise manipulate text files.

If ed is invoked with a file name argument, then a copy of file is read into the editor’s buffer. Changes are made to this copy and not directly to file itself. Upon quitting ed, any changes not explicitly saved with a ‘w’ command are lost.

Editing is performed in two distinct modes: command and input. When first invoked, ed is in command mode. In this mode, commands are read from the standard input and executed to manipulate the contents of the editor buffer. A typical command might look like:

,s/old/new/g

Which replaces all occurrences of the string old with the string new.

When an input command, such as ‘a’ (append), ‘i’ (insert), or ‘c’ (change) is given, ed enters input mode. This mode is the primary means of adding text to a file. In this mode, no commands are available; instead, the standard input is written directly to the editor buffer. Lines consist of text up to and including a newline character. Input mode is exited by entering a single period (".") on a line.

An important thing to remember is that all ed commands operate on whole lines or ranges of lines. For example, the ’d’ command deletes lines; the ’m’ command moves lines, etc. It is possible to modify only a portion of a line by specific replacement, as in the example above; although even then, the ’s’ command is applied to whole lines at a time.

In general, ed commands consist of zero or more line addresses, followed by a single character command and possibly additional parameters. Commands have the structure:

[address [,address]]command[parameters]

The address(es) indicate the line or range of lines to be affected by the command. If fewer addresses are given than the command accepts, then default addresses are supplied.

Line addresses

An address represents the number of a line in the buffer. ed maintains a current address that is supplied to commands as the default address if none other is specified.

When a file is first read, the current address is set to the last line of the file. In general, at any given time, the current address is set to the last line affected by a command.

Think of the address as a place marker to the last line in the file where ed has done something, and, unless told otherwise, the line where it operates next.

Referring to line addresses

A reference to a line address is constructed from one of the bases in the list below, optionally followed by a numeric offset. The offset may include any combination of digits, operators (for instance, “+” and “-”) and whitespace. Addresses are read from left to right, and their values are computed relative to the current address.

One exception to the rule that addresses represent line numbers is the special address 0 (zero). This address means “before the first line,” and can be used in any command.

Address ranges

An address range is two addresses separated either by a comma (",") or semicolon (";"). The value of the first address in a range cannot exceed the value of the second. If only one address is given in a range, then the second address is set to the given address.

Each address in a comma-delimited range is interpreted relative to the current address. In a semicolon-delimited range, the first address is used to set the current address, and the second address is interpreted relative to the first.

The following address symbols are recognized:

Regular expressions

Regular expressions are patterns used in selecting text. For example, the ed command

g/string/

Prints all lines containing string. Regular expressions are also used by the ’s’ command for selecting old text to be replaced with new. In addition to a specifying string literals, regular expressions can represent classes of strings. If it is possible for a regular expression to match several strings in a line, then the left-most longest match is the one selected.

The following symbols are used in constructing regular expressions:

The following extended operators are preceded by a backslash () to distinguish them from traditional ed syntax.

[:alnum:][:cntrl:][:lower:][:space:][:alpha:][:digit:][:print:][:upper:][:blank:][:graph:][:punct:][:xdigit:]

[.col-elm.]

[=col-elm=]

Commands

All ed commands are single characters, though some require additional parameters. If a command’s parameters extend over several lines, then each line except for the last must be terminated with a backslash ().

In general, at most one command is allowed per line. However, most commands accept a print suffix, which is any of ‘p’ (print), ’l’ (list), or ’n’ (enumerate), to print the last line affected by the command.

An interrupt (typically ^C) has the effect of aborting the current command and returning the editor to command mode.

ed recognizes the following commands. The commands are shown together with the default address or address range supplied if none is specified (in brackets).

Restricted ed

red operates like ed, except that it can only edit files in the current directory, and it cannot execute shell commands.

Examples

Getting started

The simplest way to start ed is to run it with no options at the command line:

ed

This command places your cursor at a new line, but you won’t receive any other prompt or indication you’re in ed. To quit ed and return to your shell’s command prompt, type q and press Enter:

q

You could also have quit using a capital Q, which is an “unconditional” quit: it exits regardless if you have changes that haven’t yet been written to a file.

Let’s go back into ed:

Now let’s intentionally type a command that ed won’t recognize, to cause an error. There is no o command, so let’s type that:

o

?

The “?” means that something went wrong. By default, ed has such a minimal interface that it doesn’t give you a specific error message. If an error has occurred, you can find out what happened using the h command:

h

Unknown command

And then ed is ready for another command.

To turn on error messages so they always display immediately by default, use the H command:

H

Making ed more friendly

It’s easier to use ed if you have a command prompt. Let’s quit ed…

…which returns us to our shell’s command prompt. Now let’s start ed again using the -p option, which tells ed to use a command prompt. We specify what prompt we want to use as a string immediately following -p. For instance, let’s use the simple prompt ‘> ‘. Here’s how we invoke ed:

ed -p’> '

Now when ed launches, we see our prompt:

Adding text, and viewing it

We haven’t given ed a file name, so it starts us with an empty buffer to enter text. Let’s enter some now. We use the a command to add text; in the commands below we also show the prompt before the command, for clarity.

a

Now we are given blank lines where we can type our text. We enter it like in any word processor, pressing Enter at the end of each line. When we’re done, we enter a period alone on a new line, to tell ed we’re done:

Line one, line two, line three, line four. .

We’ve entered four lines; the editor leaves off there, and gives us another ed command prompt. If we enter the p command, it prints the line where we left off editing:

p

line four.

Most ed commands can be prefixed with an address range to specify what range to apply to the text. So, if we want to print lines 1 through 4, we could use the command 1,4p:

1,4p

Line one, line two, line three, line four.

If we omit the line numbers (but leave in the comma), ed assumes maximum start and end values, so it prints the whole buffer:

,p

In case we forget what line number we left off at, we can use the = command:

=

4

We can continue to add lines using the a command again, and entering a period on a new line when we’re done:

Line five. .

Line one, line two, line three, line four. Line five.

We’re now at line 5:

5

We can change to another line by entering that line number. When we do, ed prints that line to let us know where we are:

3

line three,

Now if we entered a, we’d start adding lines after line 3. Or we can use the i command to start inserting lines before line 3:

i

This is the new line three. All lines after this are off by one! .

Let’s check to see where the editor leaves us after this insertion. This is known as where the buffer is “addressed,” and it should be the last line of the buffer:

6

Let’s look at all six lines:

Line one, line two, This is the new line three. All lines after this are off by one! line three, line four. Line five.

Let’s delete the third line using the d command. We put a 3 before the d, to specify that line 3 is the line to be deleted:

3d

…and view the changed buffer:

,p

If you want to view lines with the line number at the beginning, use the n command. For example:

n

5 Line five.

3n

3 line three,

2,4n

2 line two, 3 line three, 4 line four.

To change a line (completely replace it), you can use the c command. You can enter multiple lines of text, so you still need to put a period on a new line to indicate you’re done:

3c

line the third, .

It also works on multiple lines:

1,2c

First line, second line, .

4,5c

line four. .

And we can join lines with the j command:

4,5j

Let’s look at those changes:

First line, second line, line the third, line four.

Understanding ranges

Let’s explore ranges a little more. Let’s select line 2, then print one line before and after it:

2

second line,

-1,+1p

First line, second line, line the third,

We’ve already seen that we can specify the entire buffer using a single comma as the range (","):

We’ve been using a single comma (",") to represent the entire buffer range in our commands. A percent sign ("%") does the same thing:

%p

When specifying ranges, we can represent the last line using a dollar sign ("$"):

2,$p

second line, line the third, line four.

If you don’t specify a range, most commands assume a range of the currently-addressed line. You can also specify the currently-addressed line explicitly using a period ("."):

.

.=

2

.p

Marking lines for easier reference

You can mark a line using the k command, giving it a “marker” of any lowercase letter. You can then refer to the marked line using a single quote followed by that letter. For instance, here we mark line 3 with the letter a, then print that line by referring to it with ‘a:

3ka

‘ap

line the third,

Moving and copying lines

You can move lines from one place to another using the m command:

1,2m3

line the third, First line, second line, line four.

You can copy lines to another location using the t command:

2t4

line the third, First line, second line, line four. First line,

Writing to and reading from files

When you’re ready to write to a file, you can use the f command to specify a file name:

f myfile.txt

myfile.txt

We can now write to myfile.txt using the w command. ed tells us how many bytes were written:

w

64

If we specify a file name after w, ed writes to that file name, and tell us how many bytes:

w myfile2.txt

To open a file for editing, use the e command with the name of the file you want to open. ed reads the file into the buffer, and let you know how many bytes were read in:

e myfile.txt

If you try to open a file for editing when you have unsaved changes in your current buffer, you receive an error. To open a file with unsaved changes, losing any unwritten changes to the current buffer, use a capital E:

E myfile.txt

The same goes for exiting ed: if you try to use the q command to exit and you have unsaved changes, you receive an error. To quit without writing your changes, use a capital Q:

Q

To open a file from the command line, specify it with the last argument when invoking ed. The file is read into the buffer, and ed reports how many bytes were read and give you a prompt for a command. For example:

ed -p’> ’ myfile.txt

64

Searching for and replacing text

Let’s delete all the lines in our buffer and re-make the file before the next examples:

,d

Twinkle, twinkle, little star. How I wonder what you are. Although I suspect you’re mostly made of hydrogen. .

To search for text in your file, use the / command with the text to search for:

/twinkle

Twinkle, twinkle, little star.

To globally run a command on lines matching a regular expression, use the command form g/regular-expression/command. For instance, here we’ll globally search for lines containing the consecutive letters “re”, and print those lines:

g/re/p

How I wonder what you are. Although I suspect you’re mostly made of hydrogen.

To run a command on the lines which do not match a specific regular expression, use the command form v/regular-expression/command:

v/re/p

To perform text substitution, use the command form s/text-to-replace/replacement-text/. We specify a range before the s so that it searches more than only the currently-addressed line:

,s/hydrogen/hydrogen and helium/

Twinkle, twinkle, little star. How I wonder what you are. Although I suspect you’re mostly made of hydrogen and helium.

By default, only the first occurrence of text-to-replace in a line is substituted. To replace every occurrence in each matching line, specify g after the second slash, for a “global” substitution:

,s/wink/wonk/g

1p

Twonkle, twonkle, little star.

Conclusion

Hopefully this has given you enough familiarity with ed to be able to use it, and to make use of the references available above. ed can be very useful when working on a system with minimal system resources, or when repairing a system that cannot boot beyond a very basic runlevel. It’s the progenitor of many modern editors, and once you get used to it, its simplicity can make basic, important tasks easy to perform.

bfs — An editor that can load very large files.cat — Output the contents of a file.edit — A text editor.ex — Line-editor mode of the vi text editor.grep — Filter text which matches a regular expression.ksh — The Korn shell command interpreter.pico — A simple text editor.sed — A utility for filtering and transforming text.sh — The Bourne shell command interpreter.stty — Set options for your terminal display.umask — Get or set the file mode creation mask.vi — Text editor based on the visual mode of ex.vim — An advanced version of vi.