5 Tips for Effectively Recording Keystroke Macros
Keystroke macro recording has been around since the days of punch cards and Nixie tubes, yet somehow, it seems to be a well-kept secret. Quite simply, a macro is a sequence of events that is memorized once and then played back repeatedly, on-demand. When I was a kid I had a toy car that I could “program” by inserting a plastic disc that had notches around the edges (i.e. a cam). Depending on the depth of each notch, it would make the car either go left, straight, or right. In essence, each disc was a macro — a memorized sequence such as: straight, straight, left, straight, left, right, straight, right.
Most any type of editor program these days has keyboard macro recording capability. This is true for word processors such as MS Word, ASCII text editors such as TextPad, and graphics editors such as Photoshop. Unlike with my toy car, you don’t need an Xacto knife, a magnifying glass and steady hand to program your own keystroke macros. All you need to do is tell the editor to start recording, type away, and then tell it to stop recording. There are usually very few limits on the types of keystrokes that can be included in the macro. Not only can you use letters, numbers, and punctuation, but also control keys like Ctrl+V, function keys like Shift+F3, and navigation keys like Home and End. Between the control keys in the function keys, there are usually a myriad of editing functions that can be called upon in creative ways, such as capitalizing text, deleting the next word, deleting up to the next word, deleting to the end of line, and, of course, select, cut, copy, and paste.
Some of the most common uses for Macros are:
- Adding a prefix and/or suffix to every line
- “Commenting-out” lines
- Adding separators between sections
- Swapping text/fixing a transposition
- Applying formatting specifications to existing text
- Removing extraneous line breaks
- Realigning columns of text
- Entering frequently used text, such as company names and return addresses.
- Typing in boilerplate text, where you then fill in the blanks (e.g. a macro that types “http://www..com”, and then leaves the cursor between the two dots, ready for you to enter the actual domain name).
- Typing in special symbols and alphabets (e.g. a macro that types the word résumé, with the accents)
Note: I included this last section of typing shortcuts for completeness; however, there are often better ways to do these simple types of text expansion. In TextPad, the clip libraries are much better suited to this, and in MS Word you’ll want to look at using the auto-correct feature.
Tip #1: Assign Shortcut Keys to the most used macros — TextPad, for example, offers two ways to play back a macro. One is that you select the macro, by name, from the Macros drop-down menu. The other is that you assign a hot key to the macro. For this, you can use any key combination that isn’t already assigned (or even ones that have default assignments that you don’t use). For temporary macros that only need to live as long as the current project is active, I like to assign Alt+1, Alt+2, etc. For longer-lived macros, it’s a matter of finding a key combination that has meaning. For example, I use Ctrl+- (control dash) to insert a horizontal-rule separator between sections.
Tip #2: Start with the Intermission — One characteristic of keystroke macros is that they don’t usually know how to pause in the middle of a sequence and wait for user input. Typically, they just plow right through. So, what do you do if the sequence you are trying to memorize has a common beginning, and a common end, but the middle varies? One solution is to memorize two different macros, one for the beginning part, and one for the end part. Thus, you would play macro #1, type in the variable part for the first entry, play macro #2, play a macro #1, type in the variable part for the second entry, play a macro #2, etc.
Another solution is to use a single macro that picks up with the end of the first sequence and immediately continues with the start of the second sequence. When the macro stops playing you’ll be poised in the middle of the second sequence, ready a type in the variable part. Thus, you would play the macro, type in the variable part for the first entry, play the macro, type in the variable part for the second entry, etc. When you have finished typing in all of the entries, all that remains is to fix up the first and the last entries by moving the extra “part two” bit from before the first entry to after the last entry.
Tip #3: First Thing, Get to a Known Location — It’s always a good idea for the first keystroke or two of a macro to be ones that ensure the cursor starts out in a known location. For example, using the Home key or the End key to ensure that the cursor is either at the beginning of the line or the end of the line, respectively. For another example, the sequence of Home, Ctrl+Right, Ctrl+Right, will usually put the cursor at the beginning of the third word on the line.
Tip #4: End Up in a Known, Convenient Location — A good billiards player not only plans how to sink the shot, but also carefully considers where the cue ball will end up, setting up the table for the his next shot. You can do the same thing with your macro recordings. For example, say that you’re working on a task list and you want to add a prefix to each task to indicate its priority (“High: “, “Med: “, “Low: “). You can record three different macros for these, assigning them as Alt+1, Alt+2, and Alt+3. The Alt+1 macro would be programmed like this:
[Home] High: [Space] [Home] [Down]
In other words, start recording, hit the Home key, type in “High: “, hit the Home key again, hit the Down-arrow key, and stop recording. The first Home means that the cursor can be anywhere on the line to be prioritized when you hit Alt+1, but the “High: ” text will be inserted at the beginning of line regardless. The Home and Down at the end of the sequence “lines up the shot” for the next task to be prioritized. In this way, you can zip through a list by holding down the Alt key with one hand, and pressing either a 1, 2, or 3 with the other hand as appropriate. (The only time the flow would be interrupted is if a task takes up more than one line, in which case you would have to hit an extra down arrow or two before resuming.)
Tip #5: Pre-load the Find buffer, Then Record Find-Next — This is a variation of the previous tip. Say that you are converting a plain ASCII text document into a Word document. This document consists of eighteen chapters and the beginning of each chapter has a line that looks like “Chapter 2: Prerequisites”. In Word, you can write a macro that would delete the word “Chapter” and the following number and colon, and then change the rest of the line to be formatted with a style of “Heading 2″. Better yet, you can use the macro to help you find those chapter heading lines. First, open the find dialog (Ctrl+F) and search for the word “chapter”. If this doesn’t put you on the chapter heading line for chapter one, then hit Find Next, repeatedly, until you are there. Now, start recording the macro and perform the deletions and the style formatting, but don’t stop the recording quite yet. Click on Find Next first, and then finally stop the recording.
In this way, every time you execute the macro, it will automatically find the next occurrence of the word “chapter.” If this occurrence is in a chapter heading line, then you would simply play the macro again. If it is not, then you would click on Find Next instead, repeatedly, until you do arrive at the next chapter heading.
Note: When MS Word records Find Next in a macro, it specifically records the search pattern that is looking for at the time, and will always look for that exact same pattern every time the macro is played back. TextPad, on the other hand, merely records the fact that it needs to do a Find Next. So, when the TextPad macro is played back it will search for whatever was searched on last (i.e. whatever happens to be in the search buffer at playback time).