13

I want to record a macro within a macro, like so:

qa
    [ first macro ... ]
    qb
        [ second macro ... ]
    q
    [ some more stuff, involving @b ... ]
q

However, the second q is interpreted as a request to stop the first macro, and the rest is interpreted as commands.

Is there any way to begin recording a second macro within a macro?

If not, is there any other way to achieve the desired effect of repeating a certain sequence of keystrokes, while already inside a macro?

3
  • 1
    Can't you first record the macro b and then record the macro a calling @b?
    – statox
    Commented Sep 13, 2015 at 16:14
  • @statox The problem is that b has destructive effects, so I can't record it first (unless I want to record it, press u a bunch of times, and then start recording a).
    – Doorknob
    Commented Sep 13, 2015 at 16:19
  • 1
    I think that it is your best solution. Note that you could also record it in another buffer (enew, recording, bd) or directly edit the b register by writing in your file the content of your macro and then "recording" it by placing the characters in the register with "by.
    – statox
    Commented Sep 13, 2015 at 16:24

2 Answers 2

12

AFAIK, you cannot record two macros by qx...q in one shot. For your requirement, you have to create macro b by qb....q, then qa...@bq.

If you don't want to "record" b you can do let @b='whatever' then @b will replay the macro. E.g. let @b='ggdG' define a macro b to remove all content of your current buffer without "recording".

There is a macro definition trick, which may not relevant to your question. It is recursive macro, not nested macro. That is, you can do:

qa..whatever..@aq

this will recursively replay the macro a. It is sometimes useful, when we don't know how many times we want to replay the macro. Because when error occurs, the macro replay will automatically stop. It saves 999@a for example.

0
2

Recording keystrokes to macro @a is the same thing as yanking the text corresponding to those keystrokes to register a. Once something is stored in a register, you can either put it or play it as a macro/keystrokes. So you could start recording a macro to @a via qa, then while the macro is recording, yank some text to register b, perhaps using something like "bye. All that does is yank text into a different register than it would if you did not specify register b. Now that register b has some text in it, you can play back that text as keystokes by pressing @b. You can even do that while macro @a is still being recorded. It's metamacrolicious!

The main disadvantage of this is it would probably be easier to write some vimscript function that does what you want, and then perhaps call it during a macro recording. The beauty of macros is that you can see the effect of what you are recording while you record it, but this meta macro recording technique takes that away somewhat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.