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:
- Ed keeps track of the current address
- The current address is used as the default for commands when no address is supplied
- When a file is opened the current address is set to the last line of the file.
- The current address is typically set to the last line affected by a command.
Address Symbols
These are the valid address symbols:
.
the current line$
the last linen
the nth line-
or^
the previous line-n
or^n
the nth previous line+
the next line+n
the nth next line,
or%
the first through last lines, same as1,$
;
the current through last lines, same as.,$
/re/
the next line with the regex re, wraps at bottom//
repeats search?re?
the firt previous line with regex re, wraps at top??
repeats search
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:
(.)a
append text after the addressed line, the current address is set to the last line entered.(.,.)c
change lines. Addressed lines are deleted from buffer and replaced by the input text. Current address is set to the last line entered.(.,.)d
addressed lines are deleted from buffer. Current address is set to the line following the last deleted line (if it exists) or the line before the deleted lines otherwise.e *file*
edit file and sets the default filename. If file is not supplied then the default filename is used. The current address is set to the last line of the file.E *file*
unconditionally edit file. A useful way to wipe out current changes and start fresh.f *file*
set the default filename. If the filename is not supplied then the default filename is printed.h
print an explanation of the last error(.)i
insert text before the addressed line, the current address is set to the last line entered.(.,.)m(.)
move the addressed lines to after the destination address. The current address is set to the last line moved.(.,.)n
print the addressed lines with line numbers(.,.)p
print the addressed linesP
toggle the command prompt on and offq
quitQ
quit unconditionally(.,.)t(.)
copy (think of t for transfer) the addressed lines to after the destination address. The current address is set to the last line copied.u
undo last change and restore the current address.w
write buffer to file on disk
Substitute Command
The substitute command is a little more involved and worthy of its own section.
(.,.)s/re/replacement/
the first text matching the regexre
on each addressed line is replaced withreplacement
(.,.)s/re/replacement/g
the/g
options replaces all matches on each addressed line(.,.)s/re/replacement/n
the/n
option replaces the nth match on each addressed line
Note:
re
andreplacement
can be delimited by any character except for space and newline- An
&
inreplacement
is replaced by the currently matched text \m
where m is a number in the range [1-9] is rpelaced by by the mth back reference- Newlines may be embedded in
replacement
- The current address is set to the last line affected
Global Command
The global command is another command worthy of its own section.
(1,$)g/re/command-list
Apply command-list
to each of the addressed lines.
Note:
- The default for the addressed lines is the entire file, starting at
1
and ending at$
, which is why this command is known as the global command - The current address is set to the line currently matched before
command-list
is executed - At the end of the
g
command the current address is set to the last line affected bycommand-list
- Each command in
command-list
must be on a seperate line and every line except for the last line must be terminated by a\
- Any commands are allowed except for
g
,G
,v
, andV
Variations:
G
interactively edit teh matched lines (enter commands for each match), use&
to repeat the last command-listv
same asg
by works on the addressed lines that do not matchV
interactive version ofv
Options
-p
specify a prompt to use (there is no prompt by default), use theP
command to toggle ths prompt
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.