When I use CTRL+n to autocomplete words a newline is inserted when I select a word from the list by pressing ENTER.
I don't want that, I want it to just insert the word and place the cursor at the end of the word.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityWhen I use CTRL+n to autocomplete words a newline is inserted when I select a word from the list by pressing ENTER.
I don't want that, I want it to just insert the word and place the cursor at the end of the word.
This question was asked on the Unix/Linux stack exchange.
The answer was found here.
As the manual states:
There are three states:
1. A complete match has been inserted, e.g., after using CTRL-N or CTRL-P.
2. A cursor key has been used to select another match. The match was not
inserted then, only the entry in the popup menu is highlighted.
3. Only part of a match has been inserted and characters were typed or the
backspace key was used. The list of matches was then adjusted for what is
in front of the cursor.
It looks like your selection happens during state 1, as referenced in the Unix/Linux stack exchange. The short solution is to use <C-Y>
.
Another additional solution for remapping is:
:inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
The above mapping will change the behavior of the key when the popup menu is visible. In that case the Enter key will simply select the highlighted menu item, just as does.
Taken from the previous link: These two mappings further improve the completion popup menu:
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
\ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
\ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
In the above mappings, the first will make work the way it normally does; however, when the menu appears, the key will be simulated. What this accomplishes is it keeps a menu item always highlighted. This way you can keep typing characters to narrow the matches, and the nearest match will be selected so that you can hit Enter at any time to insert it. In the above mappings, the second one is a little more exotic: it simulates to bring up the omni completion menu, then it simulates to remove the longest common text, and finally it simulates again to keep a match highlighted.