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
    Commented Oct 16, 2014 at 15:16

4 Answers 4


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.

  • 2
    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. Commented Oct 16, 2014 at 19:36
  • 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. Commented Oct 16, 2014 at 22:51
  • 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
    Commented Dec 20, 2016 at 20:36
  • I've never encountered that. Perhaps it's due to some indent setting or a plugin you've forgotten about. I would ask on Vim's mailing list: vim.org/maillist.php . Commented Dec 21, 2016 at 1:25
  • This doesn't seem to answer the problem in the OP though, in some popup menus one wants to accept with return, but not have a new line.
    – baxx
    Commented Mar 13, 2020 at 15:30

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 uses the CtrlP plugin might find this mapping convenient.

  • The mapping from <TAB> to <CR> in the first line looks like a typo.
    – JeremyKun
    Commented Nov 22, 2020 at 6:53
  • 1
    No, it'a not a typo.
    – Dmitrii
    Commented Nov 23, 2020 at 9:22

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>"))

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. Commented Oct 16, 2014 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. Commented Oct 16, 2014 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .