How to trigger omnicomplete/auto-completion on keystrokes in INSERT mode?

Solution 1:

Can Omnicomplete be triggered on A-Za-z keystrokes in INSERT mode?

You can listen to InsertCharPre event and check what keys have been pressed.

function! OpenCompletion()
    if !pumvisible() && ((v:char >= 'a' && v:char <= 'z') || (v:char >= 'A' && v:char <= 'Z'))
        call feedkeys("\<C-x>\<C-o>", "n")
    endif
endfunction

autocmd InsertCharPre * call OpenCompletion()

Also you should set completeopt+=menuone,noselect,noinsert to avoid vim inserting completion text automatically. But keep in mind it is not common decision and you will probably have some issues. The one I have found is it breaks cgn command.

For further reading do help InsertCharPre, help autocmd-events, help pumvisible(), help feedkeys().

How to get Omnicomplete to insert the func's parameters?

I used sublime for a some years and it represents builtin functions (default vim omnicompletion) with snippets. So if you want parameters to be inserted into the buffer you need to:

  1. Install snippet manager like ultisnips, neosnippet, minisnip, xptemplate or others.
  2. Create a bunch of snippets or install from somewhere.
  3. Make your completion system recognize preferred snippet manager (many completion managers recognize ultisnips).

If you change your mind in the way of using so called preview window you mentioned above here are some tips:

  1. It is possible to show preview window at the bottom of the screen set splitbelow.
  2. You can set previewheight option to change its size.
  3. To automatically close window after completion is done autocmd CompleteDone * pclose.

For further reading do help ins-completion and help preview-window

Solution 2:

I found that the answer by @Evgeniy to be almost perfect - but I did find that when replaying macros, the cursor would sometimes drop down a line (which would break the macro). I managed to solve it with the following functions and autocmds:

set completeopt+=menuone,noselect,noinsert " don't insert text automatically
set pumheight=5 " keep the autocomplete suggestion menu small
set shortmess+=c " don't give ins-completion-menu messages

" if completion menu closed, and two non-spaces typed, call autocomplete
let s:insert_count = 0
function! OpenCompletion()
    if string(v:char) =~ ' '
        let s:insert_count = 0
    else                    
        let s:insert_count += 1
    endif
    if !pumvisible() && s:insert_count >= 2
        silent! call feedkeys("\<C-n>", "n")
    endif
endfunction

function! TurnOnAutoComplete()
    augroup autocomplete
        autocmd!
        autocmd InsertLeave let s:insert_count = 0
        autocmd InsertCharPre * silent! call OpenCompletion()
    augroup END
endfunction

function! TurnOffAutoComplete()
    augroup autocomplete
        autocmd!
    augroup END
endfunction

function! ReplayMacroWithoutAutoComplete()
    call TurnOffAutoComplete()
    let reg = getcharstr()
    execute "normal! @".reg
    call TurnOnAutoComplete()
endfunction

call TurnOnAutoComplete()

" don't let the above mess with replaying macros
nnoremap <silent> @ :call ReplayMacroWithoutAutoComplete()<CR>

" use tab for navigating the autocomplete menu
inoremap <expr> <TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
inoremap <expr> <S-TAB> pumvisible() ? "\<C-p>" : "\<TAB>"