In Progress
Unit 1, Lesson 1
In Progress

Editor Macro

Video transcript & code

As a rule, text editing is one of the least important aspects of programming. Most of the topics on this show are about improving our tools for working with ideas and concepts, not with characters and lines.

That said, as programmers we are paid not just to model application domains in our minds, but also to work efficiently and effectively with the artifacts of code. And in Ruby, that means working efficiently with plain text.

For instance, here's a file full of tests.

We need to add temporary "skip" commands to each test. We will then un-skip them one by one as we implement code to pass each test.

Of course, we could just do this manually, one test at a time.

But this is not an effective use of our all-too-limited time. This also isn't a particularly good use case for a search-and-replace operation. However, it is a perfect application for a recorded macro.

Let's turn on macro recording, and give the editor an example of what to do. We'll search forward to the next test, insert a line, and type skip. Then we'll end the recording.

This is Emacs, and I'm using Emacs macro commands. But the specific editor and commands are not what's important right now. Chances are your preferred editor has its own keybindings for the same operation.

Now let's play back the macro once.

Now let's do it again.

This looks like it's working. Let's play it back a whole bunch of times at once.

Now let's look back and check the work. Uh-oh. Looks like it ran into a problem.

One of the test names wasn't consistent with the others in style.

The magic of recorded macros is that they can take care of the 80% or more of cases that are predictable. Then we are left to deal only with the outliers, using our creative, problem-solving brains.

This brings us to one of the most important rules about working with editor macros: strive only for an 80% solution. If we fiddle around with our macro until it's general enough to handle every possible case, we're wasting time just as much as if we were manually making each edit.

In many modern editors, it's possible to do a lot of the same type of tasks using multiple selections and cursors.

But while it's worthwhile to learn how to use this feature as well, I'll still advocate for the macros being the more broadly applicable skill. They can handle more types of operation. And on most editors, if you find yourself using the same macro repeatedly, you can capture it to a file and give it a permanent keybinding.

So why am I taking time out from programming tips to talk about an editor feature? Because I believe that what we perceive as "easy" strongly influences our unconscious decisions.

Many programming tasks, in particular many code maintenance and refactoring tasks, involve repetitive actions and text transformations. If we internalize the basic macro recording and replay commands, they become a "possibility multiplier" for us. They greatly increase our pallette of practical options when considering the "next move". They help us to look at large-scale modifications as no more effort than small-scale changes.

But in order for us to add this tool to our arsenal, it has to spring immediately to hand. We can't be looking up keybindings every time we think of using them. That's why macro recording and playback is on my short list of basic editor tools that I think every programmer ought to memorize, whatever editor they choose to use.

If your editor doesn't have a keybinding for macro recording, see if it's possible to add one. If it doesn't have a macro recording and playback feature at all: consider upgrading to a different editor!

And that's all for today. Happy hacking!

Responses