Ed: The Standard Editor
ed was designed for real terminals, i.e. a typewriter.
You would type a command to the computer, and it would type out any errors.
It would not waste paper, ink, and time by typing out COMMAND RUN SUCCESSFULLY after each command.
A silent machine meant a happy machine.
Basic Usage
Open a file:
1ed file.md
Insert a new line.
- Press
i<Return> - Type the line.
- Finish your edit with single dot.
1i
2dear diary,
3.
Print the current line:
1p
Change the current line:
1c
2Dear diary,
3.
Delete the current line:
1d
Write the 'buffer' to disk:
1w
Quit:
1q
Working with Lines
Open that file:
1ed file.md
Add a line:
1a
2Fortune of the day:
3.
Run fortune, and place the results inside the current buffer:
1r!fortune
2?
The ? indicates an error. We can ask what the error was with the command
h:
1r!fortune
2?
3h
4Unexpected command suffix
The command r has something after it which makes no sense. It should have a
space after the r!
1r !fortune
242
That last line means ed has 42 characters in its buffer.
Read the current line, and show the line number:
1n
22 How many weeks are there in a light year?
This means we are on line 2.
Fill up the file with fortunes:
1r !for x in 1 2 3 ; do echo --- && fortune ; done
Print out which line in the file we are currently on:
1=
212
There are twelve lines. Go back up to line 3:
13
2---
So line 3 just has our delimiter: ---.
Whenever you hit enter, ed prints the current line then moves to the next
line.
1I would have promised those terrorists a trip to Disneyland if it would have
2
3gotten the hostages released. I thank God they were satisfied with the
4
5missiles and we didn't have to go to that extreme.
Go back a few lines:
1-3
Print out 'current line', using the dot:
1.
Print current line with the number of that line:
1.n
211 Possessions increase to fill the space available for their storage.
Print and number the next three lines:
1.,+3n
29 You have no real enemies.
310 ------
411 Possessions increase to fill the space available for their storage.
Skip ahead four lines:
1+4
2?
3h
4Invalid address
This is an invalid address because there are no more lines, so ed cannot print them.
All Commands
Meta Commands
| Aim | Command |
|---|---|
Explain an error (?) |
h |
Show a prompt when ed can take a command. |
P |
Change the default filename to rec.txt. |
f rec.txt |
| Save the file. | w |
| Append a line after line 8. | 8a |
| Move three lines down. | +3 |
| Move four lines back. | -4 |
| Move to the last line. | $ |
Create
| Aim | Command |
|---|---|
| Insert a line before line 5. | 5i |
| Append a line after line 8. | 8a |
Read all of ~/.profile into the current position. |
r ~/.profile |
Run dir and place the results into the current position. |
r !dir |
| Copy lines 10 to 15 to line 23. | 10,15t23 |
Read
| Aim | Command |
|---|---|
| Print current line. | . or p |
| Read lines 1 to 10. | 1,10p |
| Print and number lines 2 to 7. | 2,7n |
Find the next line ending with fi |
/fi$/n |
Show the number of the next line starting with if. |
/^if/= |
Find the line which contains HOME. |
?HOME? |
| Print the next five lines literally (showing tabs). | .,+5l |
| Print from here till the end of the file. | .,$p |
Show all lines with HOME and show their numbers. |
g/HOME/n |
Update
| Aim | Command |
|---|---|
On line 30, substitute less with less -R |
30s/less/less -R/ |
Change each line starting # to ## |
,s^# /## /g |
| Move lines 50 to 55 to line 20. | 50,55m20 |
Delete
| Aim | Command |
|---|---|
| Delete line 4. | 4d |
| Delete from here to four lines down. | `.,+4d |
Delete all lines starting with #. |
g/^#/d |