UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
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.
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:
Move the position of the cursor up or down.
List out the text that has been entered.
Insert and append to existing lines.
Go to specific line numbers.
These are explained in the sections that follow.
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:
1--Positions and displays the cursor to the first line
.--Displays the current line the cursor is positioned on
1,$l--List all the lines in the file
1,$n--Lists all the lines in the file with line numbers
.,$n--Lists the current line the cursor is positioned on with line numbers
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.
2i--Appends text before line 2 in the file
.a--Appends text to the current position
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.
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.
s/pattern1/pattern2--Performs a one-time search on pattern1 and replaces it with pattern2
1,$s/pattern1/pattern2/g--Searches globally for all occurrences of pattern1 and replaces it with patern2
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):
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.
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.
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!
Man pages
ed
UNIX Hints & Hacks |
||||||||||||||||||||||||||||||||
Chapter 8: Editors |
|
|||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.