VGEL.ME

Ed: The standard EDitor

Posted

Since some people aren't getting it, the beginning of this article is a joke. What editor you use isn't important! As long as it's ed.

##Getting started with Ed - the standard editor Many modern programmers might laugh at ed. "What a quaint editor"! It doesn't have code completion! I can't load it with thousands of shitty plugins! It's name is only 2 letters! It's not written in hipster-script! Well, they're wrong. Ed is the standard text EDitor. It's on every Unix machine, ever! It doesn't waste your time with useless effects like cursors or showing you the current contents of the file. It's efficient! It's better! It's... ed!

permalink for Getting_into_Ed Getting into Ed

Ed can seem intimidating at first, so I suggest you don't just start a blank file. Instead, create a file with an inferior editor - I refuse to use vi, and Emacs is banned from my machines, so I used echo.

echo 'ed-test\nHello world\n\tHi\nend.' > ed-test

You can cat the contents if you'd like to get a bearing on the file layout. This is important because unlike inferior editors, ed will not waste your time by showing you file contents. You have to ask for them. Otherwise, ed assumes you know what you're doing. You can now start ed. Run

ed ed-test

and you will be presented with the pinnacle of EDitor UI -

$ ed ed-test
29

Before doing anything else, press ^C. This will ensure you are in command mode.

$ ed ed-test
29
^C
?

For starters, you should attach training wheels. Like I have said, ed will not waste your time printing unneeded characters. In fact, I suggest typing a few things. Ed is in command mode at the moment, so you will not change the file. The only response you will get is ? (unless you stumble upon one of ed's single-letter commands). ? is ed telling you it doesn't understand your command. Lets make this a little more friendly. Input the commands H<newline> and P<newline>. H toggles on verbose error help messages, and P will give you a visual indicator (a

$ ed ed-test
29
^C
?
H
Interrupt
P
*

Now if you mess around, you should get better error messages, such as invalid address or invalid command suffix. This is very wasteful, so turn it off as you become more familiar with ed.

##Viewing the File Lets print the file! But first, we need an explanation of ed commands. Ed commands are single letters, however you generally will not type single letters in command mode. Instead, commands usually take 3 things: an address(range) first, then the command name, then a suffix. Both the address and suffix are optional, and the command will usually fill in useful defaults.

For example, to print the current buffer (the copy of the file in memory), type ,n

*,n
1 ed-test
2 Hello world
3  Hi
4 end.

This prints line numbers too. (,l only prints lines, though it's uglier. I recommend always using n). In this command, , was the address (range from first line to last line), and n was the command (print lines). There are many other addresses, and I won't list them all here. man ed has a list if you are curious, but some useful ones are:

You may have noticed the -- command in my command log. An address expression by itself acts as a command that moves the current line to the value of the expression and prints it.

##Editing the file Now, let's edit text! Editing text in ed is not done in command mode, but text (input) mode. There are several commands to put you in text mode, and ^C will bring you out. a is the simplest, so let's start with that. a accepts a line (not a range), and it will append text after that line.

*,n
1 ed-test
2 Hello world
3  Hi
4 end.
*2a
Appending!
^C
?
Interrupt
*,n
1 ed-test
2 Hello world
3 Appending!
4  Hi
5 end.

As you can see, we've added a line! However, our changes aren't saved yet. If you open another terminal, you will see the file is unchanged. We'll cover saving later.

Another text command is c (change). It accepts a range, deletes that range, and then appends where it used to be.

*,n
1 ed-test
2 Hello world
3 Appending!
4 Test1
5 Test2
6 Test3
7 No more testing!
8  Hi
9 end.
*4,6c
Removed tests.
^C
?
Interrupt
*,n
1 ed-test
2 Hello world
3 Appending!
4 Removed tests.
5 No more testing!
6  Hi
7 end.

c removed 4,6 and then started at 4.

d is much like c, but it doesn't allow input. It just deletes the lines, and sets the current line to either after the section if possible, or before the section if the section was at the end of the file.

*,n
1 ed-test
2 Hello world
3 Appending!
4 Removed tests.
5 No more testing!
6  Hi
7 end.
*5,6d
*,n
1 ed-test
2 Hello world
3 Appending!
4 Removed tests.
5 end.
*.
end.

Another very useful command is u (undo). It's what it sounds like. After running 5,6d, I ran u:

*u
*,n
1 ed-test
2 Hello world
3 Appending!
4 Removed tests.
5 No more testing!
6  Hi
7 end.

Like it never even happened! To redo, run u again. u only keeps one piece of history.

y and x can be used to yank and place. y takes a range to copy into the copy buffer, and x takes a line to place it at.

*,n
1 ed-test
2 Hello world
3 Appending!
4 Removed tests.
5 No more testing!
6  Hi
7 end.
*2,4y
*5x
*,n
1 ed-test
2 Hello world
3 Appending!
4 Removed tests.
5 No more testing!
6 Hello world
7 Appending!
8 Removed tests.
9  Hi
10 end.

##Other bits Ed has many other very powerful commands, such as G for editing lines matching a regular expression (and address modes that accept regular expressions). Read the man page to find out about them, sadly this tutorial doesn't have space for all the power of ed.

Saving a file is simple, simply type w. You can specify a range to w to only save part of a file. You can also suffix w with a file name to save to the non-default file (the default is either the file used in the original $ ed file command, or the first file saved to if ed was invoked without a file). w WILL CLOBBER EXISTING FILES WITHOUT ASKING! Use W to append to a file instead. If you're saving and quitting, wq can combine the two operations.

That's ed, the standard EDitor! More efficient than vi, has a decent text editor unlike Emacs! I suggest reading the man pages (man ed) to see all of ed's wonderful commands and useful shortcuts, like running shell commands within ed and using regular expressions (ed is the namesake of sed).

Good luck and godspeed with ED, the standard EDitor!