Tuesday, October 27, 2009

Talk to Mr. ed













Talk to Mr. ed


The vi editor may seem like a quaint throwback to prehistoric software, but in the early days of UNIX vi didn’t even exist. In the pre-CRT era of Teletype terminals, line editors ruled, and the standard among line editors was (and still is) ed. A line editor, such as ed, is one that assigns line numbers to the lines in a file. Every time you do something, you must tell ed which line or lines to do it to. If you use the EDLIN program in DOS, ed should look familiar. The ed program has been a part of UNIX since the beginning of time. When you use it, you begin to appreciate how far software design has progressed since 1969.


If you can get another text editor to use in any way @@repeat — any way, do it. If you don’t think that ed can really be that bad, just peruse the rest of this section, and you will run screaming to your system administrator for vi , pico, or emacs (preferably pico or emacs).



�Tip��Some systems have a program called ex that is similar to but not quite as horrible as ed. Try typing ex to see what happens.


To run ed, type this line:



ed important.letter

(Type the name of your file rather than important.letter.) If no file has the name you specify, ed makes one. UNIX responds to this command with a number, which is the number of characters (letters, numbers, punctuation, and spaces) in the file, just in case you are being paid to write by the letter.


If you receive an error message when you try to run ed, talk to your system administrator. Congratulate her on getting rid of that Neanderthal text editor and find out which text editor you can use.










Hey, Wilbur, which command was that?


All ed commands are one-letter long (such as h).



�Tip��Remember not to capitalize ed commands unless we specifically say to. ed commands are almost all lowercase letters.


Relatively recent versions of ed (since, oh, about 1983) have a P command (that’s a capital P, one of the few uppercase commands) that turns on a prompt. If you type P and press Enter, ed prompts you with an asterisk when it’s in command mode and waiting for a command. Is that incredibly user-friendly or what? This P command enables you to determine when you’re in command mode! Must have snuck that one in when the lazy typists weren’t looking.


If you’re in input mode and want to give a command, type a single period on a line by itself, which switches ed to command mode.


In the remainder of this discussion about using ed, whenever we tell you to type a command, it works only if you’re in command mode. If you’re not sure, type a period and press Enter first.


If you’re in command mode and want to type some text, you switch to text-input mode. First, however, you must decide whether you’re going to append (by using the a command) after the current line the lines of text you will type or insert (by using the i command) the lines of text before the current line. More about the current line and the a and i commands in a minute.





Feeding text to Mr. ed


When you want to create a file and feed some text to it start the process by typing this line:



ed eating.peas

You can name your file something other than eating.peas, if you want. UNIX responds with a question mark, just to keep you on your toes. (This time, the question mark tells you that ed just created a new file for you.)


To add (append) new lines of text to the end of the file — in this case, the end of the file is the same as the beginning because the file is empty — type a and press Enter. UNIX responds by saying nothing, which is your indication that ed is now in input mode and waiting for you to type some text. Type some pearls of wisdom, like this:



I eat my peas with honey,
I’ve done it all my life.
It makes the peas taste funny,
but it keeps them on the knife.
.


�Remember��When you finish typing text, type a period on a line by itself to switch ed �from input mode back to command mode. Not that ed gives you a hint that this process is going on, unless you have used the P command to tell it to prompt you.


The lines of text are now in your file. Now is a good time to save the file, just in case you kick your computer’s plug from the wall in your frustration at having to use such a brainless program.





Getting Mr. ed to save your text


The following command saves your text in a file with ed. If you are in input mode, remember to type a period on a line by itself to switch to command mode before typing w (followed by Enter). That’s w for write. UNIX responds with the number of characters now in the file. Be sure to give this command before leaving ed so that your deathless prose is saved in the file, in this case, eating.peas (or whatever filename you used when you ran ed).











Show me the file, please


Now that you have text in the file, how can you see it or change it? By using the p (print) command. This command doesn’t print anything on the printer; it just displays it on-screen — another example of superb software engineering. (Well, it printed on those old Teletypes.) If you type the p command by itself, as follows, ed displays the current line. In the case of the sample eating.peas file, the current line is the last line in the file. You can also tell ed which lines to display by typing their line numbers. To display lines 1 through 4, for example, type this line:



1,4p

You can also use the symbol $ to stand for the line number of the last line in the file (in case you don’t know how many lines are in the file). The following command always displays the entire file:



1,$p




A miserable way to edit


You can change the contents of a line of text with ed, but it involves giving commands that look like this:



12,13s/wrong/right/

This command substitutes right for wrong in lines 12 through 13, inclusive. Totally primitive and painful, isn’t it? For the amount of editing you probably do in ed, it’s almost easier to delete the line with the typo and insert a new line. We recommend that you immediately ask your system administrator for a better text editor.





Undo your thing, ed!


Wait — ed has one useful, humane command, after all! The u command enables you to "undo" the last (and only the very last) change you made to the file. If you delete a line by mistake with the d command, for example, you can type u and then press Enter to undo the deletion.



�Remember��Be sure that you don’t make any other changes before using the u command. It undoes only the last thing you did.





Time to ed out


When you finish making changes and you want to leave ed (or even if you’re not finished making changes and you want to leave ed anyway), type q and then press Enter. If you are in input mode, first type a period on a line by itself to get into command mode. Then press the q key to quit.


If you haven’t saved your work by using the w command, ed just doesn’t quit. Instead, it displays a question mark to tell you that it was expecting a w command first. To save your changes, type these two commands, pressing Enter after each:



w
q

If you don’t want to save the changes to the file, press the q key again at the question mark. This time, ed believes that you really want to leave and thus exits. Not a moment too soon!


As a review, Table 10-4 lists the commands you use most commonly with ed.

















































Table 10-4: Commands in ed

Command



Description



a



Add lines after the current line and enter input mode



d



Delete line or lines



h



Display extremely terse help message right now



H



Display terse help messages whenever anything goes wrong



i



Insert lines before the current line and enter input mode



n



Display line or lines with line numbers



p



Display line or lines



P



Display an asterisk whenever ed is in command mode



q



Quit the whole thing



Q



Quit regardless of whether changes have been saved



u



Undo last change



w



Write (save) the file













No comments: