0

I know that when I am in insert mode I can enter Normal mode for one command only and automatically switch back to insert mode using <C-O>. For example to go to the end of the line when in Insert mode I could type <C-O>$

I have the following key mapping to format the current paragraph:

nnoremap <leader>fp mtvipgw`t

I would like to trigger this whilst in insert mode by typing <C-O><leader>fp. Unfortunately this doesn't work and I'm sent back into insert mode after the first command only (mt) and then the rest of the mapping is typed as normal characters.

How can I make this work?

2
  • Doesn't gw already put the cursor back in the right spot? Why the marks? I think you could just do <C-o>gwip
    – D. Ben Knoble
    Jun 25 at 18:17
  • Because I didn't know about gw behavior in insert mode. Your point is useful and correct, thanks. The question is about triggering multiple commands sequentially after using <C-O>, and the accepted answer is about that problem, not gw specifically.
    – john
    Jun 27 at 20:08
1

So your issue is that you are actually trying to execute several commands while <C-o> only allows to run one. What you could do is create a user defined command which will be executed as only one command but will run everything you need:

command! FormatParagraph normal! mtvipgw`t
nnoremap <leader>fp :FormatParagraph<CR>

See :h user-commands also, maybe you'd be interested in :h i_CTRL-\_CTRL-O to avoid playing with marks.

1
  • 1
    Perfect. I wasn't aware of user defined commands (only functions). <C-\> looks good too. Thanks statox!
    – john
    Jun 25 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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