3 ms·
This mapping makes my life much nicer: nnoremap Q @q It turns 'Q' into a sort of enhanced '.' command that can repeat a sequence of multiple commands. To u
by adamtj 11y ago
This mapping makes my life much nicer:
nnoremap Q @q
It turns 'Q' into a sort of enhanced '.' command that can repeat a sequence of multiple commands. To use it, first start recording a macro to the 'q' register. That's the easiest register because 'q' is also the command to start and stop recording a macro: to start recording you just simply press 'qq'. Then you make your changes and press 'q' to stop. Now you can repeat that sequence of commands with 'Q'.
It turns out that macros are really useful, but I would rarely use them because it's just so awkward to press '@' each time I want to apply one. Unfortunately, '.' doesn't repeat the '@' command to apply a macro. I would often find myself mechanically repeating things by hand, or else wasting time writing complicated sequences of :s/substitution/commands/. Now I use quick ad-hoc macros all the time.
The speed and convenience are the real key for me. You might call it synergy, or a tipping point, or the straw that breaks the camel's back. For me, 'qq' is just a little bit faster and easier than any other register. Repeating 'Q' is just a little bit nicer than typing '@q' followed by repeating '@@'. (It's also kinda nice that it's all the same key.) Macros are nice, but two carefully made choices that each are only slightly better than the alternatives combine to make something vastly more useful. In my mind, 'Q' is almost its own thing, separate from the idea of other macros.
Now that I use macros, I've discovered some useful patterns. One technique I often use is to include a final motion in my macro to move to the next place I want to change. For example, maybe I want to edit several lines in a row. I press 'qq', make my changes to the current line, then press 'j' to move down a line. Only then do I stop recording. After that, I can just hold down the shift key and press 'Q' repeatedly until I'm done. Even if my changes are simple enough that '.' would work, it saves me from making mistakes when bouncing back and forth: 'j.j.j.j.jj.'. Oops.
You can do that with any motion(s), and I often use 'n'. Sometimes finding some simple marker text on each problem line is easy, but using find-replace to make the actual changes requires a hideous regex. In those cases, I'll do the simple search, record a macro as I make the first change, and finally press 'n' to move to the next match. Then I just lean on 'Q' until I'm done.
Sometimes I'll also throw a 'zz' after the last motion to center the current line in the window so I don't have to look around to locate the cursor when a change scrolls the window.
Normally 'Q' enters Ex mode, but so far I haven't found much use for that. If that ever changes, I'll probably use a <leader> mapping to get Ex mode back. Using something even a little bit slower than 'Q' would destroy the effect. I forget where I originally learned this mapping.