The question of “why bother using ed?” demands an answer, but I opt to defer that answer — it’s a worthy discussion on its own.
Ed has a limited featureset and only a few commands to memorize, but using those commands efficiently can be confusing. Following are a few methods I’ve picked up for making the best use of my keystrokes.
Let’s say I have a line of code like the following:
var item = Data.create();
I don’t like the vagueness of the term item so I want to replace it.
I could find out the line number and change it, which would look something like this:
var dataItem = Data.create();
And then I check my work in context: 145,150p.
But I could also perform a substitution given the line number of the search in one step, like this:
Then I still might want to check my work, requiring another line. I can consolidate that further in one line, like this:
With the p at the end of the line running a print on the line number on which I operated.
Depending on the term in your regex, you might not be sure you’re hitting the right line. You could always make the expression itself more specific, or instead, you could check your place in the document.
For instance if you do a search and want to know where it turns up, try this:
You could always print the section around the text:
30 var item = ...
Repeating this sort of navigation pattern does “break” the purpose of ed just a bit after all, but that doesn’t mean it’s a poor workflow.
If you know where you are roughly, you can use a line number:
var item = ...
Just make sure you don’t reference a line number that comes before the searched line, or you’ll confuse ed. You can always check with .=.
If you’ve marked your line with k, you have it easy, but let’s say you don’t know where you are at all contextually. In such a case, you can use a relative address:
var item = ...
Or even another search term:
Bear in mind that these all rely on the internal reckoning of the address. Running a search command will change the line you’re on, so pay attention to the sequence of commands you run!
For instance, if you run a .,+5n you’ll get something like this:
38 current line!
However .= will now tell you that you’re at line 43. Ed can be confusing, but it sure helps you focus.
Another intriguing if slightly hacky way to navigate is by injecting unix search commands:
!grep -n 1 item file
Search what you want, then discard the rest, and you’re right back in your editor. Of course, to a purist, this feels very wrong.
One other nifty way of easing the editing process uses a combination of w- d-r. Personally I find this useful if I have lots of different ideas and bits of text to keep track of.
Here’s how: Save some lines out into a file, delete them from the buffer, and ignore them until you need them later at which point you can move your address to the location of interest and read them back in using r.
If you just have one piece of text to move and you know where it goes, you can use m to move the lines, but there are a lot of cases in which you have snippets of text you want to hang on to — and writing the buffer to temp files works great for this situation. You can always delete them right from within ed using an injection like !rm ./tmp* without ever leaving your ed session.
Through a combination of regular expressions, line addressing, command injection, and the occasional manual edit, cruising your way around a file (or files) in ed can be a breeze.
(I still haven’t addressed the why. That’s for another time).