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
A decent post on recursive macros: https://jovicailic.org/2018/06/recursive-macros-in-vim/
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.
:'<,'>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.
Unless I use a feature regularly I find it's a higher cognative load. I use bash, sed and perl regexps on a near daily basis. Macros are very rare.
:%s/.*/"&",/
The & special character gets replaced with the whole matched pattern.:s/x/\&\/\\
Macros and other solutions are great, but for this very simple and surprisingly common task, multiple cursors are perfect.
But this has prompted me to look this up and it turns out that you can use @@ to do this. This will make my life a lot easier, I can just smash <Shift 2> until all my lines are updated.
:'<,'>norm @x :g/pattern/y A
you need to use an uppercase register ("A" in this case) because it executes `y` once per match. With `y a` instead of `y A`, you would end up having only the last match in the register.edit I see someone else answered this - use @@ instead of .
qq0i"<Esc>A",<Esc>jq@@
i agree this example is silly. i'd do something like :%s/.*/"&",/Macros really shine when you need to do a bit more complex stuff, there is certainly a cutoff between ease of regex "from the top of my head" vs. spending time formulating it.
They may seem a bit complex but are quite simple. Basically you just record your editing movements and play it back.
How macros work: press q x to record and q to end. Then repeat with @ x, where x is the name of the macro you want to use (you can have several macros). Anything you do will be played back. That's it... Conceptually simple.
A nice trick is to record a macro for a single line and place the cursor on the start of the next line at the end of the recording. This way it's possible to operate upon large blocks of text in a similar way by just repeating the macro. The key then is to use generic movements like "go to the beginning of line" (^), "go to the end of line" ($), "skip word" (w), etc.
As a silly toy example, if you have a list of strings in a text file, you can turn it into a list of quotes with (from the top of my head):
(Record macro, go to beginning of line, insert mode, write a quote, exit insert mode, go to end of line, insert mode, move right, write a quote and a comma, move down to next line, move to the beginning of line, exit insert mode, end macro recording)Then to process 100 lines, you'd do:
100@x
That turns these:
into If you have hundreds of lines of that you'll start to appreciate the easy automation.I use macros to do drive-by mass-edits without resorting to sed, scripting, regular expressions etc. since the cognitive load of those is higher for me.
Edit: formatting