0

I am trying to do a remapping when I'm in insert mode to insert a comment but am having a tough time figuring out what all the keys map to. What I am trying to do is:

:inoremap leadercspace ==> escleadercspacei

Basically, if I'm in insert mode I want to get out of insert mode to insert the comment (leader+c+space) and then go back into insert mode.

What would the correct :inoremap mapping for this be? What I have right now is:

:inoremap <leader>c<space> <Esc><Leader>c<space>i

But this doesn't seem to work (at least the latter half of it -- it does seem to be executing the mapping command). Note: the plugin I'm trying to remap is:

https://github.com/preservim/nerdcommenter

[count]<leader>c<space> |NERDCommenterToggle|

Toggles the comment state of the selected line(s). If the topmost selected line is commented, all selected lines are uncommented and vice versa.

4
  • 2
    See double post here
    – Zorzi
    Apr 16, 2020 at 22:34
  • 1
    As @Zorzi helpfully reminds, cross-posting is discouraged on the SE network
    – D. Ben Knoble
    Apr 16, 2020 at 23:00
  • @D.BenKnoble sure, which one should I post on then? It seems like SO is more active.
    – David542
    Apr 16, 2020 at 23:01
  • 1
    Eh, disagree. Stack Overflow has more traffic but is increasingly less willing to allow vim questions. Vi and Vim, on the other hand, is only for them (and a few dedicated users check the new questions practically every day, so)
    – D. Ben Knoble
    Apr 16, 2020 at 23:02

1 Answer 1

1

Couple of thoughts:

  1. The "do one normal command in insert mode" is <C-o>, so you could go with (<plug>NERDCommenterComment is the "comment map"):
inoremap <Leader>c<space> <C-o><plug>NERDCommenterComment

But that won't work, because noremap prevents <plug> mappings from working (well it prevents all mappings on the right hand side, including <plug>).

  1. You could then try
imap <Leader>c<space> <C-o><plug>NERDCommenterComment
  1. But at this point, since you're using recursive maps, you might as well do
imap <Leader>c<space> <C-o><Leader>c<space>

But make sure you don't map <C-o> in insert mode.


P.S. If you ever have to type <Leader>c<space>, this mapping will get in your way... (e.g., my leader is space, although many use comma—what if you have to type that for some reason?). Recommended usage is to use normal mode for most things (like triggering commenting) and insert mode for, well, inserting things.

2
  • 1
    Ugh I ended up answering on the SO one instead... :-( But basically agreed with your answer here, use Ctrl+O and insert mode mappings are typically somewhat problematic. My guidance there was to learn to use Ctrl+O and use it directly in this case rather than creating a mapping for it.
    – filbranden
    Apr 17, 2020 at 17:55
  • 1
    I feel your pain :) @filbranden
    – D. Ben Knoble
    Apr 17, 2020 at 18:10

Your Answer

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

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