I am trying to use the omnicomplete. It is quite simple to use in insert mode, but I'm having a hard time using it from a function. Here is what I have so far:

" Cmd-. to autocomplete tags, especially HTML, see *i_CTRL-X_CTRL-O*
function Autocomplete()
    normal! i</<C-x><C-o>
    " If it was an empty omni-complete, delete those three characters
    let char_three_left = getline('.')[col('.')-3]
    if (char_three_left == '<')
inoremap <leader>. <C-O>:call Autocomplete()<CR>\

However, it seems to enter in the text literally, and I am having a hard-time escaping it when in insert mode. What would be the proper way to do this?

I suppose maybe I just need to call :call htmlcomplete#CompleteTags(...) but there seems to be little to no documentation on that.


This turned out to be quite tricky, but it's possible to manually trigger the omni-complete call. The important help sections are: omnifunc and primarily complete-functions:

" Cmd-. to autocomplete tags, especially HTML, see *i_CTRL-X_CTRL-O*
function Autocomplete()
    " See :h *complete-functions* for how to use the omni-complete
    " note: the following function is geared towards HTML
    let idx = htmlcomplete#CompleteTags(1, '')
    let completion = htmlcomplete#CompleteTags(0, idx)[0]
    if completion != '>'
        execute ('normal!i' . completion)
        normal! XX
inoremap <leader>.  </<C-O>:call Autocomplete()<CR><Right>
nnoremap <leader>. i</<C-O>:call Autocomplete()<CR><Esc><Right><Right>

Notice also the use of </ before entering the function -- why? I have no idea, but it seems the insert wasn't being done (or at least registering) for the autocomplete function to pick it up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.