Getting omnicppcomplete and ctags to work in VIM

zeboidlund picture zeboidlund · Sep 25, 2011 · Viewed 7.2k times · Source

Here's my .vimrc

syntax on
set number
set nowrap
set autoindent
" configure tags - add additional tags here or comment out not-used ones
set tags+=~/.vim/tags/cpp_files
set tags+=~/.vim/tags/cpp_src/
set tags+=~/.vim/tags/qt
" build tags of your own project with Ctrl-F12
map C-F12 :!ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q .CR

" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1
let OmniCpp_ShowPrototypeInAbbr = 1 " show function parameters
let OmniCpp_MayCompleteDot = 1 " autocomplete after .
let OmniCpp_MayCompleteArrow = 1 " autocomplete after ->
let OmniCpp_MayCompleteScope = 1 " autocomplete after ::
let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
" automatically open and close the popup menu / preview window
au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
set completeopt=menuone,menu,longest,preview

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete
au BufNewFile,BufRead,BufEnter *.cpp,*.hpp set omnifunc=omni#cpp#complete#Main
autocmd FileType cpp set omnifunc=cppcomplete#CompleteCPP

I've followed this guide for getting it to work, but nothing really happens. As you can see I've tried a variation of autocmd and au type commands for this to work, but nothing actually happens. Am I doing something wrong? The paths in the set tags* lines are correct...

Answer

Andrew Radev picture Andrew Radev · Sep 27, 2011

This line should be the one that causes the problem:

autocmd FileType cpp set omnifunc=cppcomplete#CompleteCPP

You see, you have the following commands:

au BufNewFile,BufRead,BufEnter *.cpp,*.hpp set omnifunc=omni#cpp#complete#Main
autocmd FileType cpp set omnifunc=cppcomplete#CompleteCPP

The thing is, the first autocommand is executed when entering a buffer with the extension "cpp" or "hpp". The second is executed when the filetype is set to cpp, which always happens after opening the buffer. It doesn't even matter how you order them, the second one will always be executed after the first one, so the omnifunc will always be set to cppcomplete#completeCPP, and you don't want that. You should replace both of these lines with this one line:

autocmd FileType cpp set omnifunc=omni#cpp#complete#Main

Just in case, if it still doesn't work, try only this instead:

au BufNewFile,BufRead,BufEnter *.cpp,*.hpp set omnifunc=omni#cpp#complete#Main

For future debugging issues, a small tip: you can check the value of omnifunc by executing set omnifunc. That way, you can check if the completion function really is the one you want.