← Back to context

Comment by cryptonector

6 days ago

Most often when I run a macro it's to change lines matching searches, so I just start the macro with the search (or `n` if the macro doesn't do additional searches) then I end the macro with `@q` (or whatever register), then execute the macro. I don't think I've ever had occasion to run a macro on every line, though I've had occasion to run macros over line ranges (but still, all matching a specific pattern).

To run a macro on the start of each line matching your search, you can use:

    :g//norm @q

Here, g// repeats the most recent search, and norm @q runs the q macro on each matched line. This is not quite the same as starting the macro with a search, since the cursor is at the start of the line and not at the start of the match, but it's often good enough.

You can also restrict it to just the matches inside a range of lines: First select the lines in visual mode, then type :g//norm @q, which will cause Vim to insert a range before g, as in: :'<,'>g//norm @q, which means "between line '< and '>, find lines containing a match of the most recent search, and run @q on each such line".