Building on Alexander's answer, I have something like these lines in my .vimrc
"unix like completion - longest substring
set completeopt+=longest
"add command to retrigger longest substring
"Naive version... see below
inoremap <C-m> <C-e><C-n>
So the 'longest' bit does most of the work, however subsequent presses of <C-n>
will just start to cycle through the options. If you press <C-e>
that (e)nds autocompletion, triggering it again with <C-n>
matches the next longest substring... adding a shortcut for that stops you having to press two keys....
You don't want to bind that to <C-n>
as it will stop you cycling in the familiar manner
I chose M as it is right next to N and checking (:h insert-index
) defaults to just being bound to same as <CR>
.... because this is vim and nothing is ever easy it turns out that this means that vim can't actually distinguish between the two keypresses!
If you don't care about using <C-m>
then just pick something else, otherwise read on...
Based on that other post I adapted the answer at the bottom and cleaned up the syntax a little (based on this)
I ended up with this:
"add command to retrigger longest substring
inoremap <expr> <C-M> pumvisible() ? "\<C-e><C-n>" : "\<C-m>"
pumvisible() returns a flag that says whether the pop-up menu is open. This stops us changing the behaviour of the enter key in insert mode when the popup menu isn't open.
Unfortunately I can't find any documentation for the ? and : and why we have to pass strings in.
1
Please don't post the identical question on multiple sites: http://stackoverflow.com/questions/15697498/bash-like-code-completion-in-vim
– Ingo Karkat – 12 years ago