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.
- qaq clears the a register
- qa starts recording to a
- f/x deletes the next forward slash
- @a re-runs the macro
- 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 someth开发者_JS百科ing 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 )
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
精彩评论