20

A rather annoying feature of vim is that if you are in insert mode and do an autocomplete (Ctrl-N), arrow key down to the desired item, and press the Enter key, then it inserts the item but also inserts a newline which you then have to delete.

Is there a way to select an item out of the autocomplete list without getting an additional unwanted newline?

  • 1
    This is not how it works for me...I select something, press Enter, and the cursor is still on the same line afterward. – goldilocks Oct 16 '14 at 15:16
  • hmmmmmmmmmmmmmmmm – Tyler Durden Oct 16 '14 at 15:24
35

It depends on which popup menu state you are in (see :help popupmenu-completion). I understand from your question that you're in state 2 (since you've pressed arrow keys to find a completion). However, the default behavior for Enter in state 2 is to insert the completion without newline; what you describe is normally the behavior of state 1 (which is when you use Ctrl+N/Ctrl+P.)

A way that works consistently in all states is to use Ctrl+Y. I like to remember the Y as standing for "yes, accept that word." It's also possible to just start typing the text that should come after the completed word, unless you've remapped things as in geedoubleya's answer.

In the same context, you can press Ctrl+E to cancel the menu and leave your text as it was before you invoked it. If you're used to the pairings of Ctrl+E and Ctrl+Y in other contexts (e.g. to scroll up or down in normal mode, or to insert the character below or above the cursor in insert mode), that's one way to remember it here. I guess you could also think of it as "exiting" the menu or similar.

See :help popupmenu-keys for more.

  • 1
    Ah ha, that makes sense. I have been using Ctrl-N to open the drop down. I have just been using the arrow keys to navigate the drop down. I didn't know you could use the arrow keys to open the drop down. – Tyler Durden Oct 16 '14 at 19:36
  • Are you Eric Christopherson from Chatham? – Tyler Durden Oct 16 '14 at 19:37
  • The arrow keys can't open the dropdown, in the default configuration. The distinction I mentioned was about navigating the menu once it's been opened. – echristopherson Oct 16 '14 at 22:51
  • 8
    No, I'm not. I'd like to ask you if you're the Tyler Durden from Fight Club, but I'm guessing you wouldn't admit it if so. – echristopherson Oct 16 '14 at 22:55
  • For some reason when I tried to do this with <kbd>Ctrl</kbd>+<kbd>Y</kbd> it also indented the line, which is undesired. Any thoughts as to why that would happen? – mpacer Dec 20 '16 at 20:36
5

Personally i use this one:

inoremap <expr> <TAB> pumvisible() ? "\<C-y>" : "\<CR>"
inoremap <expr> <Esc> pumvisible() ? "\<C-e>" : "\<Esc>"
inoremap <expr> <C-j> pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <C-k> pumvisible() ? "\<C-p>" : "\<Up>"

Anyone who use CtrlP plugin could find this mapping convenient.

2

As an alternative to using your arrow keys, enable your j & k keys to scroll through the autocomplete list.

Doing this changes the current line to match the selected word as you scroll.

Therefore you do not have to press enter as the cursor is still in insert mode at the end of the substituted word.

To enable this add this to your .vimrc (Thanks to others at stackoverflow):

inoremap <expr> j ((pumvisible())?("\<C-n>"):("j"))
inoremap <expr> k ((pumvisible())?("\<C-p>"):("k"))

Separately, instead of using the arrow keys, you could just repeat the Ctrl-n which will curse through the options (Ctrl-p to go backwards) and substitute on the current line as it moves, no need for Enter or vim key mappings.

  • This behavior can be a little confusing, since it is both 1) in insert mode and 2) interpreting j and k to do things they don't do in insert mode. In particular, you can't select a menu entry and then follow it directly with something beginning with j or k. – echristopherson Oct 16 '14 at 18:59
  • Agreed, that is why I provided the other option if this was a step too far. Personally, I would prefer the confusion rather than move my hand to use the arrows. – geedoubleya Oct 16 '14 at 21:50
2

I know this is old, but kind of piggy backing on geedoubleya's answer since although echristopherson's answer was informative and awesome, if anyone wants a quick and dirty solution:

inoremap <expr> <cr> ((pumvisible())?("\<C-y>"):("\<cr>"))

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.