Search code examples
vimpluginssyntaxautocompletevi

Vim omni autocomplete issue


I put in my ~/.vimrc file the next lines of code:

filetype plugin on                                                                                                                                  
set omnifunc=syntaxcomplete#Complete

but nothing happened, when I created a file with .c extension and I pressed <Ctrl-X><Ctrl-O> the vim editor popped up a "pattern not found" message. Here one can see my .vimrc: enter image description here


Solution

  • You have two problems.

    First problem

    The command filetype plugin indent on tells Vim to:

    • detect the filetype of every buffer,
    • source the appropriate filetype plugin for the detected filetype,
    • source the appropriate indent script for the detected filetype.

    Those things happen automatically, as you load a new buffer. In this specific case (and others) the C ftplugin contains this line:

    setlocal ofu=ccomplete#Complete
    

    which effectively overrides your:

    set omnifunc=syntaxcomplete#Complete
    

    There are several ways to solve this problem but the most sensible is to make your own filetype plugin:

    1. Create ~/.vim/after/ftplugin/c.vim.

    2. Add setlocal omnifunc=syntaxcomplete#Complete to it.

    See :help filetype-plugin.

    Second problem

    As explained under :help ft-syntax-omni, syntaxcomplete#Complete pulls its suggestions from the current syntax definition. Since syntax highlighting is not enabled by default and you don't enable it explicitly, there is no "current syntax" and thus no syntax-based completion.

    You are supposed to enable it explicitly, in your vimrc:

    syntax on
    

    Conclusion

    This is how your vimrc should look like:

    filetype plugin indent on
    syntax on
    

    And this is your brand new custom filetype plugin for C:

    " ~/.vim/after/ftplugin/c.vim
    setlocal omnifunc=syntaxcomplete#Complete