1

I'm coding c++ with vim and I want to set a shortcut while typing parenthesis and new line. Here is an example,

if (a == 1) {}|  // the cursor is at the end of the line and the vim is under INSERT mode

What I need is to set a shortcut to make this piece of code as below:

if (a == 1) {
    |  // the cursor is here now and the vim is under INSERT mode
}

I've tried like this:

inoremap <C-v><Enter> <C-o>i<CR><C-o>O

But with this setting, if I type <C-v><C-Enter>, the code will become:

if (a == 1) {}
|    // the cursor is here now and the vim is under INSERT mode

I'm confused now. Because I've tested with my vim and it should work.

if (a == 1) {}|   // the cursor is here with INSERT mode

Typing <C-o>i: if (a == 1) {|} // the cursor is here now with INSERT mode
Then, typing CR<C-o>O could make the code become what I expected:

if (a == 1) {
    |  // the cursor is here with INSERT mode
}

As you see, it worked as expected, but why can't it work with inoremap?

0

What about of simply using the following?

:inoremap <buffer> µ <left><cr><esc>O

PS: in my bracketing plugin, I test on <cr> whether my cursor is between brackets to trigger that behaviour (without the left part as I start from {|} instead.

| improve this answer | |
  • You see, my issue is that the cursor is at the end of line, instead of between brackets. I tried inoremap <C-v><Enter> <left><cr><esc>O, but it's not working... So weird... – Yves Apr 21 at 13:41
  • You see, when we code in vim, switching from INSERT mode to NORMAL mode will move the cursor to left. You can try like this: i <esc> i <esc> i <esc> ... and you will find that the cursor is moving towards the left. But it doesn't seem to work in shortcut settings... – Yves Apr 21 at 13:44
  • That's extremely odd add I tested my mapping with µ and <c-v><enter> (which I think is a bad idea as <c-v> can be extremely useful), and it works from toto {}|$ – Luc Hermitte Apr 21 at 13:47
  • So weird. It's not working here... I give up :( – Yves Apr 21 at 13:58
  • I removed all plugins from the ~/.vim/bundle/, and I found the imap worked. Then I re-added the plugins one by one and tested for each adding, the imap always worked. Finally, I re-added all of my plugins and the imap still worked... That's weird but at least this issue disappear... – Yves Apr 22 at 1:19

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.