4 ms·
Macros, Macros, Macros. These are a game changer when editing large files. Use the `q` key and followed by any letter you like, I default to `a`. Perform some a
by k4ch0w 8y ago
Macros, Macros, Macros. These are a game changer when editing large files. Use the `q` key and followed by any letter you like, I default to `a`. Perform some action, like deleting a line, appending to the start of a line, or searching for a word then deleting it. Press `esc`, then type in the number of times you want to repeat the action, followed by `@` and the letter you chose. I.E `10@a` and it will perform that action 10 times.
- MaxBarraclough 8y agoVim can be great for quickly doing tedious text transformations. Far easier to get right than doing it the 'proper way' with a script in Python/Perl/whatever. To do something like scan through a JSON or XML document and split a 'name' field/tag into 'firstName'/'lastName', Vim is the tool I'd reach for. I think of it as 'cursor-oriented programming', but what I do with it isn't really programming, it's generally of the pattern of 1. Find start of pattern 2. Transform pattern 3. GOTO 1 Or at worst, the decorate/sort/undecorate pattern. But it boils down to repeatedly applying a macro.
- Pete_D 8y agoAlso fun: macros can call themselves. So if you want to repeat something across a whole file, it's sometimes easiest to write an infinitely recursive macro and let it error when it bumps up against the bottom of the file.
- Xophmeister 8y agoA useful thing to know when using macros is that the letter you press after `q` is just the register in which Vim stores the macro. Registers are also used when copying and pasting. As such, if you want to edit a macro, you can just paste it into a new buffer (e.g., `"ap` for register `a`), edit it as you wish, then copy it back into that register (`:%y a`). You can even use this method to write macros from scratch, rather than recording them first.