Question

I Want to use Ctrl+Space for omni-completion (and keyword completion if there is no omni-completion) in vim. I've tried this which I found somewhere on the web:

inoremap <expr> <c-space> pumvisible() ? "\<C-n>" : "\<C-x>\<C-o>\<C-n>\<C-p>\<C-r>=pumvisible() ? \"\\<Down>\" : \"\\<CR>\""

however it's not working. Anyone who is using Ctrl+Space for this too who can show me the correct way (which works) to do it?

Worth noting is that it needs to work in the terminal version of vim NOT gvim.

Was it helpful?

Solution

Try this:

inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
\ "\<lt>C-n>" :
\ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
\ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
\ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
imap <C-@> <C-Space>

OTHER TIPS

The above way is "kind of" working, but it's so unreadable that almost nobody could say what it actually does. The solution above is not good.

Short Answer - Use this:

function! Auto_complete_string()
    if pumvisible()
        return "\<C-n>"
    else
        return "\<C-x>\<C-o>\<C-r>=Auto_complete_opened()\<CR>"
    end
endfunction

function! Auto_complete_opened()
    if pumvisible()
        return "\<Down>"
    end
    return ""
endfunction

inoremap <expr> <Nul> Auto_complete_string()
inoremap <expr> <C-Space> Auto_complete_string()

This answer also respects that there are two possible values (depending on terminal/gvim usage) for Ctrl+Space: <C-Space> and <Nul>.

I use a similar approach as the first one in jedi-vim, but more customizable.


Long Answer - What the above does:

The whole escaping of the above answer is so confusing, that I've split the above answer into a readable format:

function! Auto_complete_string()                               
    if pumvisible()                                            
        return "\<C-n>"                                        
    else                                                       
        return "\<C-x>\<C-o>\<C-r>=Auto_complete_opened()\<CR>"
    end                                                        
endfunction                                                    

function! Auto_complete_opened()                               
    if pumvisible()                                            
        return "\<c-n>\<c-p>\<c-n>"                            
    else                                                       
        return "\<bs>\<C-n>"                                   
    end                                                        
endfunction                                                    

inoremap <expr> <Nul> Auto_complete_string()

This clearly shows what it is doing. There's some weird stuff happening in Auto_complete_opened. It's not just doing completion, it's doing two additional things after trying to complete:

  • When trying to use the omnicompletion, it somehow does a <C-n><C-p><C-n>, which could IMHO just be abbreviated to <C-n>.
  • In case completion is unsuccessful, it uses a backspace and does a completion again, not <C-o><C-x> but <C-n>, which just doesn't make a lot of sense.

I'm not saying that this is not what some user might want, but it's probably not what most users want! My short answer takes that in credit and gives you a simple way to edit it. You can now just easily change things if you want to (for example <Down> to <C-n>, if you want the first entry to be written from the beginning on).

For iterm2 and vim these lines works for me, I got from jedi-vim

" Next three lines are to enable C-Space to autocomplete, omnicomplete
inoremap <C-Space> <C-x><C-o>
imap <buffer> <Nul> <C-Space>
smap <buffer> <Nul> <C-Space>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top