Search code examples
linuxubuntuvimvi

How to prevent Vim from jumping to next line, when auto-completing words using `C-P`?


When auto-completing words in Vim using C-P and pressing Enter to select the first candidate, without first using the arrow-keys to select between candidates, Vim will jump the cursor to the next line.

Conversely, if I first use the arrow-keys to select between candidates, Vim won't jump to the next line.

How can I prevent Vim from jumping to the next line, when selecting first candidate using Vim's auto-completion feature C-P?

enter image description here


Solution

  • When popup (completion) menu is active, you're supposed to use <C-N> <C-P> <C-E> <C-Y> instead of <Down> <Up> <Esc> <CR> respectively (note that all these keys behave a little differently). But if you really like it you can use a mapping:

    inoremap <expr><CR> pumvisible() ? "\<C-Y>" : "\<CR>"
    

    See also Improve completion popup menu Vim tip.