back
Ed The Line Editor
Ed is a very basic line-oriented text editor. When other CLI editors like nano, vim, ne, emacs or tilde are not available on a UNIX-like system, it is very likely ed will be so it is a good idea to learn to use this basic program.
Below is an example of me doing some simple editing with ed.
# Create file
touch textfile.txt
# Edit file with the prompt '->'. It will output current size of file (0 bytes).
ed -p '->' textfile.txt
# Useful error messages
->H
# Enter insert mode
->a
# Writing a line
First line of text.
# Exit insert mode
.
# Print contents (shows what I just wrote)
->,p
# Adding second line
->a
Another one!
.
# Print contents with line number preceding each.
->,n
# What line am I currently on?
->n
# Adding third line
->a
This makes me feel like a caveman.
.
# Adding fourth and fifth line
->a
fourth
and fifth
.
# Printing lines 1 through 4
->1,4p
# Add line before the one we we are currently on
->i
I broke fourth and fifth apart :D
.
# I made a mistake. As I wrote two in the last one and my current line number was on the fourth, I have written it in the wrong location so the file currently looks like this.
First line of text.
Another one!
This makes me feel like a caveman.
I broke fourth and fifth apart :D
fourth
and fifth
# Delete line 4
->4d
# Changing second line of text
->2c
Second one!
.
# I accidentally deleted lines 1 through 3!
->1,3d
# Undo
->u
# Write a new line
->a
This is line 0.
.
# Copy line to position 0
->6t0
# Move line 5 to second position
->5m2
# Delete line 7
->7d
# The file currently looks like this
->,n
1 This is line 0.
2 First line of text.
3 fourth
4 Second one!
5 This makes me feel like a caveman.
6 and fifth
# Search for lines beginning with these characters
->/^This
This is line 0.
->/^This
This makes me feel like a caveman.
# Using global command between first and last line to print all lines that contain "line" character.
->1,$g/line/p
This is line 0.
First line of text.
# Write current date to line
->r !date
# Write to file and quit
->wq
Ed is such an old, basic program that, by default, it doesn’t do a lot of the things we have gotten so used to. For example, with other editors you are given a useful output to help you understand what went wrong when an error occurs. In ed, you only get a question mark. You can enable useful error messages by doing H
when editing. Things like this will take some getting used to. Make sure to read the manual to learn more.