puflogh300X74.gif

Polarwave's OpenBSD
Tips and Tricks for Newbies

| Ed Tutorial |

| Home | Tips and Tricks | Links | News Feeds | Fun | Contact | Blog (Opens in New Page) |


Editing With ed

What better name for an editor would you give in the UNIX environment
but ed?

The first two characters of the most common activity with a computer for
programmers. Programmers are all the time editing source code. And they
need a powerful editor to do this job. Hence the flame wars between the
emacs and vi factions. But we have nothing to do with either. We are going
to be talking about ed(1), the powerful line editor.

The reason ed is known as ed and not editor is that UNIX guys like to type
less so they could get really lazy. Hence the plethora of two line and three
line commands in UNIX.

Instead of netcat, they want to type nc.
Anyway now coming back to ed, why do you need a line editor when you can
have screen editors?
Good question. The answer is that sometimes the terminal gets messed up.
There are many serial terminal control sequences understood by different
terminal emulation software on various UNICes. Usually TERM can be set
to vt100 and you should not have much trouble but ed can help in
situations where things get really messed up.

For a line editor, the terminal settings do not matter. Moreover in
constrained embedded environments or when you boot single user in
OpenBSD, you may love ed.

Let us cut to the chase now.

$ ed /etc/fstab

opens the file /etc/fstab in ed for reading and writing.

Once you are inside ed, you can view the file under edit
with the keystrokes:

1,$n

with line numbers or

1,$p

without the line numbers.

Good. You can read the file now. How to write to it?

You do that with a funny sequence of commands and input text.

To start writing you have to go to the line which you want to
change, add or delete.

This is easily done by simply typing the line number in UNIX spirit.

Say little, but say enough.

So assume that you have a file of 1000 lines,

saying

444

so ed will know that you want to be at line 444.

You can inspect the line number with the command.

.=

Now you wish to add a line at 444.

a

The new line added

.

will complete the action.

Now you have a line "The new line added" at line number 445.

You can verify the veracity of my statement by typing

1,$n

The other thing to do is when you wish to delete a line or range
of lines.

You can do that easily with ed.

1,10d

will delete lines 1 to 10.

Remember, when we added a line, we prefixed the activity with
a command:

a

and we suffixed with a

.

on a line all by itself. This is similar to the command line mail
program which knows that you stopped typing the mail when
you typed

.

on a line by itself.

You can also move and copy lines easily with ed.

1,5m$

will move the lines 1 to 5 to the end of the text file.

You can copy with the t operator.

1,5t20

will copy the lines 1 to 5 to line 20.

You can read shell commands into the text file with

r !ls

and you can also read and write to multiple files with

r /tmp/readfile.txt

and

w /tmp/newfile.txt

Once you are done with editing, kindly save and exit

wq

or quit without saving with just

q

on a line.

ed is a very powerful editor as it does all its actions in an internal
buffer in memory. All your changes remain in main memory and they
don't get written to the file until you explicitly write using a

w

command.

Now you may think that one of my opening statements on ed being

independent of vi or emacs is actually false.

Yes, ed is related to vi.

The

wq

is very similar to the

:wq

command in vi or vim.

And you can also search for patterns using

/pat

operator in ed.

ed is very powerful and this article should get you started with
this cryptic but very handy and powerful editing tool.

Have fun!

Back to Tips and Tricks
Home


Delicious Bookmark this on Delicious

No affiliation between this site and the OpenBSD project exists or is implied.

valid-html401.png