Skip to Content

C++ AutoComplete feature in VIM

I really needed a context-sensitive C++ autocomplete feature for Vim, which would allow me to do crazy things like autocompleting class member names.  As it turns out, there is a way to do that, and it works pretty well.  I decided to write up how I set things up myself, in hopes that this will help others as well.

Here is what you need in order to set things up:

So, you need to install Vim and Ctags (which is platform dependent, and I leave it to you to figure out how to do that in your own platform).  Once you get those set up, grab the OmniCppComplete plugin, and extract it to the .vim directory under your home directory (create it if it doesn't exist).  After that, you want to put the following inside your ~/.vimrc:

  1. " omnicppcomplete options
  2. map <C-x><C-x><C-T> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q -f ~/.vim/commontags /usr/include /usr/local/include ~/moz/obj-ff-dbg/dist/include<CR><CR>
  3. set tags+=~/.vim/commontags
  4.  
  5. " --- OmniCppComplete ---
  6. " -- required --
  7. set nocp " non vi compatible mode
  8. filetype plugin on " enable plugins
  9.  
  10. " -- optional --
  11. " auto close options when exiting insert mode or moving away
  12. autocmd CursorMovedI * if pumvisible() == 0|pclose|endif
  13. autocmd InsertLeave * if pumvisible() == 0|pclose|endif
  14. set completeopt=menu,menuone
  15.  
  16. " -- configs --
  17. let OmniCpp_MayCompleteDot = 1 " autocomplete with .
  18. let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
  19. let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
  20. let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
  21. let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
  22. let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype (i.e. parameters) in popup window
  23. let OmniCpp_LocalSearchDecl = 1 " don't require special style of function opening braces
  24.  
  25. " -- ctags --
  26. " map <ctrl>+F12 to generate ctags for current folder:
  27. map <C-x><C-t> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR>
  28. " add current directory's generated tags file to available tags
  29. set tags+=./tags
  30.  
  31. " Setup the tab key to do autocompletion
  32. function! CompleteTab()
  33.   let prec = strpart( getline('.'), 0, col('.')-1 )
  34.   if prec =~ '^\s*$' || prec =~ '\s$'
  35.     return "\<tab>"
  36.   else
  37.     return "\<c-x>\<c-o>"
  38.   endif
  39. endfunction
  40.  
  41. inoremap <tab> <c-r>=CompleteTab()<cr>

You can of course customize this however you like, but the only part that you really need to customize is the second line.  You would probably want to leave /usr/include and /usr/local/include there, but you can replace ~/moz/obj-ff-dbg/dist/include to whatever project specific directory which contains header files which contain definitions which you want to include in the C++ autocompletion.  ~/moz/obj-ff-dbg/dist/include is a path on my system which contains all header files we use in Mozilla.

The autocompletion functionality is based on the ctags database.  This is a list of all symbols found in header files.  With this setup, you have two separate ctags databases, one is for your system-wide headers (which should change rarely), and the other is for your project-specific symbols.  The former is stored in ~/.vim/commontags, and the latter is stored as a file named tags in your current directory.  To update the system-wide ctags database, you can use Ctrl+X, Ctrl+X, Ctrl+T.  To update the local ctags database (which is created from the contents of the current directory, recursively), you can use Ctrl+X Ctrl+T.  You would usually update the local ctags database from time to time to make sure that the latest changes in your source files are reflected in the ctags database.

Now, open a C++ file inside Vim.  Let's say you type the beginning of a symbol, and then press Tab.  A list of symbols matching what you have types appear on the screen, like this:

Autocompletion using tab

Also, if you type ., -> or ::, you will be presented with a list of autocomplete options as well.

Autocompletion after .Autocompletion after ->Autocompletion after ::

And that's it! If you feel adventurous, you can type :help omnicpp in Vim and start exploring what else you can do, but this setup is working perfectly fine for me.

Trackback URL for this post:

http://ehsanakhgari.org/trackback/108

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This is very cool, thank you!

This is very cool, thank you!

Also, you need Vim 7.0 or

Also, you need Vim 7.0 or later (the latest release & patchlevel is 7.2.402 as of this writing), and if you are on Windows it's not .vim/ but vimfiles/ -- still as a subfolder of your home directory (and if you're in doubt about "what" is your home directory, type ":echo $HOME" (without the quotes, and hit Enter at the end) in a running Vim, and it will tell you.

Re: Also, you need Vim 7.0 or

Thanks for the additional tips, Tony.
However, I think this can be made to work with lower versions of Vim, but anyway if someone has a lower version, they should probably consider upgrading!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.