5

I know that I can record macros with q<char> and run them with @<char>. I also know that I can run the last-used macro with @@.

But @@ is two whole keystrokes! That's one too many! Especially if I'm running that macro a lot of times. I know about <num>@<char> but what if I don't know <num> in advance?

What I'm looking for, really, is something like an interactive search-and-replace (see Interactive search/replace regex in Vim?) that runs macros. Can this be done?

Community
  • 1
  • 1
Samizdis
  • 1,591
  • 1
  • 17
  • 33
  • With `@@`, you can utilize keyboard repeat by just keeping the keys down. The speed is not quite optimal, but at least you will not get muscle cramps. – Jussi Hirvi May 31 '22 at 10:43

2 Answers2

3

Apply macro a in normal mode with one keystroke:

:nnoremap , @a

You can then use "," when you want to apply the macro, or "n" when you want to skip to the next search occurrence

To interactively apply macro to search matches:

  1. define macro with "qa"
  2. search with "/"
  3. apply macro to match with ","
  4. skip match with "n"
Jim U
  • 3,318
  • 1
  • 14
  • 24
2

I have this in my ~/.vimrc:

" <A-@>         Repeat last macro with a single key combination.
nnoremap <A-2> @@

So, first I replay as usual with the register name, e.g. @a. Then I repeat with @@. Then I realize I need several more repeats (but cannot use a [count]), so I switch to Alt + @ (@ is above 2 on the US-English keyboard layout).

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324