Recent Changes - Search:
 Welcome to the Cisco Academy for Vision Impaired Linux Wiki.

PmWiki

edit SideBar

GettingYourFeetWetWithEd

Get Your Feet Wet With Ed

An Easy Tutorial
Part 1 of 3

At a shell prompt clear your screen with the command
clear
then press ENTER. Now type ed test
and press ENTER again. Ed will display "test: no such file or directory"

That's ed's user-friendly way of telling you it created a new file named test. If it had opened an existing file named test, it would have displayed the number of bytes.

Your empty buffer is waiting to be filled. You are in command mode. Press ENTER. Ed will display a question mark. This is its error message. Ed was designed for printing terminals. It liked saving trees so it printed as little as possible by default.

Type the letter h followed by ENTER.

Ed will display "unknown command". The letter h in lower-case tells ed to display an explanation for the last error.

Type upper-case H followed by ENTER. This toggles on ed's help. such as it is. Ed will now display a one-line explanation for future errors instead of just a question mark. You can always have that explanation repeated by typing the lower-case h.

Type the upper-case P. Follow this with Enter. From now on, I will no longer remind you to press ENTER. But all ed commands, even those to move to a different line, should be followed by Enter.

The upper-case P turns prompting on. Ed has two modes, input and command. With the prompt on, an asterisk (*) will display when it is in command mode.

Press Enter by itself several more times. Ed should display "invalid address" followed by star on the next line. The asterisk is the prompt. The "invalid address" is its explanation of your error message. If you don't see this, toggle with upper-case H and upper-case P until you do.

It is time to input some data. You are at line zero. Zero is a mythical line. It only exists if you are positioned above the first line.

Type a. This puts you in input mode. The lower-case a stands for Append. You could have also typed i. The lower-case i stands for insert. On an empty file it doesn't matter. Input mode is sometimes called text entry mode.

You know you are inputting data in text entry mode because no star appears on the line.

Type
This is my first line.
Press ENTER.
Type
This is my second line.
Press ENTER.
Type
This is my third line.
Press ENTER.
You have now entered three lines. You are ready to type a fourth. Instead, simply type a period (.) on the fourth line and press ENTER.
An asterisk appears. You are in command mode.

Troubleshooting: not in command mode? Simply type a period again and press ENTER. You might have typed a tab or space. Even an invisible character before the period tells ed that you are typing in a real line 4.

Back in command mode, you are ready to display the lines in your new file. Type a comma followed by a lower-case l.

All lines will display. The comma is a shorthand.

Each ed command begins with a line number range. The range 1,4 indicates lines 1 through 4. The range 25,50 indicates lines 25 through 50. The range comma by itself specifies everything

After the range, an ed command, has, quite naturally, a command. The l command tells ed to list the available lines in the specified range.

So ,l says "list all lines".

Try typing 1,2l

The first two lines display.

Try typing 2,3l. The second and third lines display.

Pretend you had a large file. It would be hard to remember all those line numbers. Ed has the n command to show them to you. Type ,n.

The lower-case n displays lines preceded by their line numbers. The lower-case l displays lines without numbers.

The lower-case p also displays lines without line numbers -- p stands for print -- but it is less useful than l as you will see later.

What will 1,2n display?

Now we move around in the file. Ed has no cursor, but it is always pointing to the current line. To find out where it currently points, type a period. The period always displays the content of the current line. The period is synonymous with simply typing p, or l, as by default, these commands display just the current line.

If you tuype n by itself, you also get the current line, this time preceded by its line number. To make ed move back a line, type the minus sign. To make ed move forward a line, type the plus sign. Remember that these, like all commands, need to be followed by ENTER.

Now use the minus sign, sometimes called dash, to move up until ed says "invalid address". You are pointing again to mythical line zero.

Now try this. Press ENTER by itself.

Ed will display the next line. The ENTER by itself is the same thing as pressing the plus sign. Press ENTER several times until ed displays "invalid address" again. You are pointing beyond the last line in the file.

Let's go back to line 2. Type 2 and ENTER.

Line two displays.

You have three ways to go forward to line three. You could press ENTER by itself. You could issue the plus sign. Or you could simply type the number 3.

Before we go any further, let's save our masterpiece. You might want to use it for practicing when you get stuck with harder, longer files. Type the letter f. This doesn't actually save.

The lower-case f simply displays the name of the file under which your masterpiece will be saved.

Type the w command. Think of w meaning write.

This writes the buffer to the filename you saw displayed with f. Ed will display a number, on my system, it was 70. This indicates that it wrote 70 bytes to the file. Remember, ed is terse because it's working hard to save trees!

Now type f followed by a space followed by the word practice.

Ed changes the filename to practice. Now type w.

Ed writes the same buffer to a file called practice. It's kind of like the Save As command.

Now type q. Ed is gone.

Check out your files with
cat test
and
cat practice

They should both be the same data.

Let's go back in to ed now and work on practice. You can always load up test if later you goof up something and want to start with a file that's not messed up. So type
ed practice
and don't forget to press ENTER. Ed will load practice in to its buffer, and display 70, the number of bytes in its buffer.

Type the period by itself. Ed displays the last line in the file. This is the current line you are pointing to.

Troubleshooting: when you use a screen-oriented editor, and load in a file, your cursor is positioned at the top. But in ed, you are always on the final line of the buffer. So if after loading ed, don't be surprised when you press ENTER, it tells you this is an invalid address.

More troubleshooting: not getting error messages? Not getting the asterisk prompt. Be sure to type the upper case P to get that prompt and the upper case H to have "helpful" error messages.

Let's try inserting something. Type the lower-case i.

Type I am inserting something. Type ENTER. Press the period by itself to go back to command mode, press Enter.

Now back in command mode, list the entire file.

Notice that your newly inserted line was placed between your second and third line. When you go in to input mode with lower-case i for insert, you are always inserting a line before your current line.

What happens if you insert before the first line in the file?

Try it. Type 1. and see how the first line is displayed. Now type i, press ENTER, type something about how you are inserting a new line, press ENTER, type a period by itself to terminate input mode, press ENTER once more, and you are back in command mode.

When you type comma followed immediately by lower-case l to display the entire document, you'll see that you inserted your new line before your first line, at our mythical line zero.

Each time you insert and delete, you actually renumber lines.

Let's try deleting. Type 3. Ed should display the text "This is my second line". This happens because your second line is now actually the third line, on account of us inserting a line in front of all the others.

Type d and press ENTER.

The line you are pointing to is gone. This is very important. When you type a command by itself, without a range, that command executes on the current line.

Now, because this file is practice, you should do so. Insert lines. Move backwards and forwards. Delete lines. Really mess up this file.

Is this file a huge disaster now? Good. Let's get out of ed without saving.

Type the upper-case Q. Press ENTER. Ed is gone and your disaster is history as well.

use the cat command to examine both practice and test. You'll see none of your recent changes actually happened.

Fire up ed once more using either test or practice.

You've learned that in command mode, typing a single period shows you the current line. You've also learned to use the line number to get to a specific line, the plus to go forward, the minus to go back and the insert to place a new line before the current line. You've also learned that the n command displays the current line with its line number.

The append command also takes you in to input mode, but it puts you after the current line.

Once again, please fool around with your file, until you are comfortable with both insert and append.

Both insert and append put you in to text entry mode. In both cases, you get back to command mode by simply typing a single period at the start of a new input line. There's only one diference: insert INSERTS BEFORE and APPEND appends AFTER the current line.

Now that you've added data it is time to delete. Remember when we said that each command starts with a range, and if the range is omitted, ed works on the current line. That's why simply typing d for delete deletes only the current line.

But what if you typed 1,2d. That would delete the first two lines. Or what if you typed 3,4d. That would delete the third and fourth line.

Try deleting a few ranges. You can even try comma followed by d if you are daring. Before typing it though, think about it some. If ,l lists all lines in the file, what do you think the comma followed by d will do?

Exit ed without saving. Next, get back in to ed with your longest file so far and do some inserting and appending until you have at least 10 lines. Remember you can type the comma followed by the lower-case n to check those line numbers!

With ten lines or more in your buffer, and a solid knowledge of deleting, inserting, appending and displaying lines under your belt, let's revisit ranges for working with longer files.

Have you noticed that if you list lines with l, a dollar sign appears at the end of each line? In ed, the dollar sign signifies the end of things. If a line has invisible characters like tabs and spaces, the dollar sign makes the end of line visible so you can see what's actually there. Try it by inserting a few lines with spaces at the end and a few lines without. You will see that the dollar sign is flush with the last text character on some lines and farther away from that text with others.

This is why the p for print is less useful; it does not make the end of the line visible.

Now you should have more than ten lines in your buffer, and you are ready to use the dollar sign in command mode. In a range, the dollar sign signifies the end of the file, the last line in the buffer. For example typing 8,$ will list from line 8 through to the end of the file.

What would happen if you typed 10,$d? Try it.

You can also use the dollar sign for input mode. If you knew you wanted to put in one line right before the last line of a file, you could type $i to get in to insert mode before that last line. Or to append after that final line you could get in to input mode with $a.

Remember that the most useful reason for the dollar sign is to be able to reference the end of the file, or near its end without having to remember line numbers.

Let's think again about a long file and what a pain line numbers could be to deal with there. The beauty of ed is that you need not worry about line numbers that much.

Start by returning to the beginning of your file by typing 1 and Enter.

Line 1 is displayed. Now, for your range, type a slash, followed by a few characters to search for, followed by a slash. For example, typing /this/ and pressing Enter, will position you to the first line containing the word this.

To repeat your search, simply type two slashes. If something is between the slashes, ed searches for the first occurrance. If nothing is between the slashes, ed searches for whatever you last told it to seek.

But remember I said this was a range. A range can be followed by a command. So if you wanted to insert, append or even delete using a search string, the procedure with ed is trivial. Type /this/d and you'll delete the first line containing the word this. If you were positioned at line 50 when you started this command, you'd delete the first line that came after line 50 containing the word this.

If you just want to look at lines, you can continue to type two slashes together as in // and ed will point to and display the next line that matches your search. Then if you see one that needs deleting, you can go ahead and type d. Or if you see one that you need to insert before or append after, you are already properly positioned to do so.

If you take some time to master this you will find it much quicker as a screen reader user to search for lines and then perform some action on them. You never need to worry that you've arrowed your cursor in to the middle of a line by mistake, or that you've unexpectedly hit some function key that did something strange to your file.

Suppose for example that you needed to delete all lines in a configuration file that refered to grendel because grendel was no longer part of your configuration. This is just as easy as if you were using a screen-oriented editor, easier in fact for a screen access user, because you simply type /grendel/ and after each line is displayed type d and press ENTER. And you can keep typing // to go to the next grendel line.

Try searching for words in your file, inserting and appending and of course deleting some lines that contain the found word. Try deleting without displaying the line first, and try displaying and then deleting.

In my next installment, I'll cover advanced searching, global replacing, moving, copying and marking lines, and I'll show how to change part of a line's content without needing to retype it. In my last installment I'll focus on skills for competently editing the numerous configuration files that system administrators are tasked with having to alter on a regular basis.

Edit - History - Print - Recent Changes - Search
Page last modified on June 14, 2012, at 05:00 AM