UNIX Hints & Hacks

ContentsIndex

Chapter 8: Editors

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

8.1 The Anatomy of ed & vi

 

8.5 Abbreviating vi Commands

 

 

8.2 The Six Steps to ed

 

8.6 Creating Macros

 

8.10 Edit, Run, and Edit Again

 

8.3 Six Simple Steps to vi

 

8.7 Search and Replace

 

8.11 Reading STDOUT into vi

 

8.4 Configuring vi Parameters

 

8.8 Other Places to Use vi

 

8.12 Using vi when tmp Is Full

 

 

8.2 The Six Steps to ed

8.2.1 Description

8.2.1 Description

ed has a more limited command structure than vi, and it can be more difficult to handle. After you get the hang of it, you shouldn't have any problems, however. Before you start editing, make a backup copy of the file you are working with. When you're inside the ed editor, it is very easy to corrupt the file if you get lost or confused at any point while editing.

Syntax:

ed [filename]

To bring the ed editor up, use the command ed followed by an optional filename.

Step One: Command Mode

When ed is started, you are put into the command mode. At first, you might not realize it because the cursor sits on an empty line without notifying you of what's happening. To verify that you are in the command mode, type the Enter key; you should see a question mark ( ?).

% ed foobar
?

In the command mode, there are certain options that you need to be aware of. They are all basic commands that provide the function of moving around in the editor:

These are explained in the sections that follow.

Step Two: Moving the Cursor

There are only two ways to move the cursor position in the ed editor: up and down. Enter the line number you want and you are there. You should familiarize yourself with certain commands that that will help in moving around:

Step Three: Switching to Insert Mode

There are two ways to insert text: the insert command and the append command. Whether you choose to insert or append the text is up to you. The only difference is where the text gets written, above or below the current line.

To leave insert mode, enter a period ( .) on a line by itself. Always keep a mental note that you cannot insert above line 1. In ed, there is no line 0. If you are at the top of the file you must start by appending text.

Step Four: Quitting the vi Editor

Because there is no way to move from side to side across a line, the only way to make changes to the line is to use a find/replace command.

Step Five: Deleting Characters, Words, and Lines

The concept of deletion is the same as insert mode or moving up and down through the file. You pass the line number and the command to delete ( d):

Step Six: Write to a File and Quit

Unlike vi, there is no way to perform a write and a quit in one command. It is a two-step process. You have to first write the file out, and then quit the editor.

Reason

There are times when you won't have a choice but to use the ed editor. You usually never run this program except in extreme emergencies. There are those who do actually prefer this editor over others for the simplicity that it provides. I'll still take vi any day.

Real World Experience

In several cases, I have seen administrators scrambling in a pinch after a hard crash searching for their administration books because they forgot how to use ed. There really isn't a lot to the editor. The odds are against you, however, that you will remember the commands. Practice occasionally; you will not have to use it on a regular basis, but you will need it!

Other Resources

Man pages

ed

UNIX Hints & Hacks

ContentsIndex

Chapter 8: Editors

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

8.1 The Anatomy of ed & vi

 

8.5 Abbreviating vi Commands

 

 

8.2 The Six Steps to ed

 

8.6 Creating Macros

 

8.10 Edit, Run, and Edit Again

 

8.3 Six Simple Steps to vi

 

8.7 Search and Replace

 

8.11 Reading STDOUT into vi

 

8.4 Configuring vi Parameters

 

8.8 Other Places to Use vi

 

8.12 Using vi when tmp Is Full

 

 

© Copyright Macmillan USA. All rights reserved.