1

When doing <ESC>qq you can record a macro into the q register (still in command mode though). How can I make <ESC>qq go into insert mode right away (without having to type i)? map qq qqi will record and put me into insert mode, but I am unable to replay with @@ or @q. The docs say you cannot use the q command inside a macro, but it does not specify what happens in my case.

8
  • 1
    What problem are you trying to solve? Yes, a macro needs to begin and end in normal mode. Perhaps what you want is a mapping instead? If you share more details of the specific problem you want to solve, it might be easier to give you good suggestions.
    – filbranden
    Commented Aug 17, 2019 at 5:10
  • 1
    Yeah... But what exactly do you want to do with macros in insert mode? Can you give a specific example of what you'd use that for?
    – filbranden
    Commented Aug 17, 2019 at 5:25
  • 1
    A macro will always end in normal mode, since you need to type the command q in normal mode to end it. Furthermore, a macro can only be called from normal mode, since the @ command is a normal mode command as well. Perhaps what you want is a inoremap or a iabbrev? If you can ask the question more specifically (such as "how can I have this key sequence in insert mode expand to this text, with headers and brackets?" and add an example of what it looks before and after), it would be much easier to give you a good answer.
    – filbranden
    Commented Aug 17, 2019 at 5:36
  • 1
    inoremap and iabbrev didnt do the trick, but Id agree and say its probably best to just type it out. Say the cursor isnt positioned before I start the macro, then I would have to hit <ESC> and move it again anyways. Thanks for clearing that up.
    – Dosisod
    Commented Aug 17, 2019 at 5:46
  • 3
    Welcome to Vi and Vim! As filbranden mentions, editing to clarify your question would be helpful—I think we’re in a bit of an XY problem situation here. It sounds like you may have a use case for snippets or templates, but it’s really quite hard to tell what your true goal is (because we’re hung up on this macro/insert mode thing). Again, as @filbranden says, macros must start and end in normal mode (and normal mode rocks). Consider editing your question to describe your real goals
    – D. Ben Knoble
    Commented Aug 17, 2019 at 13:18

1 Answer 1

0

While I agree with filbranden and D. Ben Knoble that there are likely better ways of achieving your actual goal, it is possible to map qq to start recording a macro that immediately enters insert mode. You can use feedkeys() to execute the qqi as if you typed it after your mapping completes:

function! RecordMacroAndStartInsert() abort
  call feedkeys('qqi', 'nt')
endfunction

noremap qq <Cmd>call RecordMacroAndStartInsert()<CR>

With the above, typing e.g. qqtest<Esc>q would result in a macro in register "q with the contents itest^[. When replayed, this macro will enter insert mode, insert the text test, and then exit insert mode.

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.