Select an item in vim autocomplete list without inserting line break

21,326

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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>"))
Share:
21,326

Related videos on Youtube

f1rstsurf
Author by

f1rstsurf

Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C. In other words: the choice of C is the only sane choice. I know Miles Bader jokingly said "to piss you off", but it's actually true. I've come to the conclusion that any programmer that would prefer the project to be in C++ over C is likely a programmer that I really would prefer to piss off, so that he doesn't come and screw up any project I'm involved with. The only way to do good, efficient, and system-level and portable C++ ends up to limit yourself to all the things that are basically available in C. And limiting your project to C means that people don't screw that up, and also means that you get a lot of programmers that do actually understand low-level issues and don't screw things up with any idiotic "object model" crap. So I'm sorry, but for something like git, where efficiency was a primary objective, the "advantages" of C++ is just a huge mistake. The fact that we also piss off people who cannot see that is just a big additional advantage. -- Linus Torvalds

Updated on September 18, 2022

Comments

  • f1rstsurf
    f1rstsurf over 1 year

    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?

    • goldilocks
      goldilocks over 9 years
      This is not how it works for me...I select something, press Enter, and the cursor is still on the same line afterward.
  • echristopherson
    echristopherson over 9 years
    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.
  • f1rstsurf
    f1rstsurf over 9 years
    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.
  • geedoubleya
    geedoubleya over 9 years
    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.
  • echristopherson
    echristopherson over 9 years
    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.
  • mpacer
    mpacer over 7 years
    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?
  • echristopherson
    echristopherson over 7 years
    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 .
  • baxx
    baxx about 4 years
    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.
  • JeremyKun
    JeremyKun over 3 years
    The mapping from <TAB> to <CR> in the first line looks like a typo.
  • Dmitrii
    Dmitrii over 3 years
    No, it'a not a typo.