idbrii
idbrii

Reputation: 11916

Is it possible to record and run recursive macro's with Vim's normal command?

Given:

https://stackoverflow.com/questions/ask

From normal mode at the first character, typing in qaqqaf/xb@aq@a clears all of the forward slashes.

  1. qaq clears the a register
  2. qa starts recording to a
  3. f/x deletes the next forward slash
  4. @a re-runs the macro
  5. q ends the recording

But running normal qaqqaf/xb@aq@a stops after b -- it seems to bail at the recursive call. The same happens if you try to use map the command.

Is there something wrong with my syntax? Or is it impossible to record a recursive macro with normal?


Note: I know it's possible to write a recursive macro with let. I'm wondering if this is the only way to write a recursive macro without recording it manually:

let @a = "f/xb@a"
normal @a

(I ask because of this answer: Remove everything except regex match in Vim )

Upvotes: 2

Views: 620

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45107

If you want to create a map to a recursive macro I suggest you start by doing something like so:

nmap <f2> :let @a = "f/xb@a"|normal @a

Of course this clobbers the @a register and if you find you self doing many of these kinds of mappings maybe a function would better suit your needs.

Here is a safer alternative to making recursive macro mappings:

function! RecMacroExe(cmds)
  let a = @a
  let @a = a:cmds . "@a"
  try
    normal @a
  finally
    let @a = a
  endtry
endfunction

nmap <f2> :call RecMacroExe("f/xb")<cr>

Edit: Changed function according to @Luc Hermitte comment

Upvotes: 3

Related Questions