3 ms·
My favorite newish-to-me macro trick is to use "recursive macros" -- macros that call themselves at the end, eg `qa[do something]@aq`. The idea (at least for me
by n8henrie 7y ago
My favorite newish-to-me macro trick is to use "recursive macros" -- macros that call themselves at the end, eg `qa[do something]@aq`. The idea (at least for me so far) is to record a line-wise action that goes to the next line before calling itself and will fail on the first non-matching line. For example, if you need to put some text after the first `0`, using commands like `t` or `f` to get there will fail if there is no `0`, but `/` will go to the next `0` on any line.
A macro like this will just keep calling itself until it fails, which is nice for files with thousands of lines and you want to make a change that is not conducive to a regex.
As a tip that took me a while to figure out, you can search within a visual selection using `\%V`, which may make it easier to reliably go to a specific part of the current line (compared to t or f like I mention above).
Also wanted to point out that iVim for iOS is pretty good, for those rare cases you want to do some text processing on the move that makes you miss Vim: https://itunes.apple.com/us/app/ivim/id1266544660?mt=8 https://itunes.apple.com/us/app/ivim/id1266544660?mt=8
A decent post on recursive macros:
https://jovicailic.org/2018/06/recursive-macros-in-vim/ https://jovicailic.org/2018/06/recursive-macros-in-vim/
- Izkata 7y agoA slight alternative to recursive macros, macros are repeatable actions, so this works: 100@a I use it so I can run the macro in batches a couple times and check if I made any mistakes, before picking an absurdly high number to hit the whole file.
- hhmc 7y agoAnother alternative, visually select the region you're interested in then: :'<,'>normal @q to apply the macro (in q) to that region. You might want to remap the above command to some shortcut if you find it useful often.