Code completion (IntelliSense) for C++ in Vim with OmniCppComplete

C++ autocompletion is possible with VIM.

Vim OmniCppComplete autocomplete example 2

This is officially the coolest tech discovery I’ve made this year.

And it’s easy to setup.

Here’s how to configure C++ IntelliSense for Vim, with an example that demonstrates how to enable code completion for the the C++ Standard Template Library (STL).

1. Download

You need the following:

2. Install

With Ubuntu 8.10 you can install Vim and Exuberant Ctags like so:

sudo apt-get install vim exuberant-ctags

Install OmniCppComplete by downloading the plugin and extracting it to your Vim settings (~/.vim/) folder.

Then open Vim, and type the following:

:helptags $HOME/.vim/doc

3. Configure Vim

Add the following to your .vimrc file (you only need the required and ctags sections, but I included options I found useful – hack as you please):

" --- OmniCppComplete ---
" -- required --
set nocp " non vi compatible mode
filetype plugin on " enable plugins

" -- optional --
" auto close options when exiting insert mode
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
set completeopt=menu,menuone

" -- configs --
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype (i.e. parameters) in popup window

" -- ctags --
" map <ctrl>+F12 to generate ctags for current folder:
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR>
" add current directory's generated tags file to available tags
set tags+=./tags

4. Generate Ctags

To generate the ctags and enable code completion for the code you are currently working on (everything in your current working directory and its sub-directories), you can simply hit CTRL+F12.

To understand how this works, check out the key-binding you enabled in your .vimrc file in step 3 of this tutorial.

To enable code completion for an external library, you need to:

  • Do pre-processing on the library (if needed).
  • Generate a ctags file for the library.
  • Tell Vim where to find the ctags file.

I will demonstrate using the Standard Template Library (STL) as an example.

Start by downloading the STL source and extracting it somewhere meaningful (e.g. /usr/local/lib/stl3.3).

For most libraries you can skip the first step, but some libraries (like the STL) will need a little pre-processing because of the complexity introduced by macros.

Change to the directory where you extracted the STL source, and run the following:

$ find . -name '*.h' | xargs sed -i 's/__STL_BEGIN_NAMESPACE/namespace std {/'
$ find . -name '*.h' | xargs sed -i 's/__STL_END_NAMESPACE/}/'

Now we’re ready to generate the ctags. In the directory where you extracted the STL source, run:

$ ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ./

This will create a file called ‘tags’ in the current directory. Rename and move the file somewhere meaningful, e.g.:

$ mkdir ~/.myTags
$ mv tags ~/.myTags/std3.3.tags

Lastly, tell Vim where to find the tags by adding the following to your .vimrc file:

set tags+=~/.myTags/std3.3.tags

And voila.

Vim OmniCppComplete autocomplete example 3
Vim OmniCppComplete autocomplete example 1
Vim OmniCppComplete autocomplete example 2

Tags: , , , , , , ,

  1. shankar

    This is officially the coolest tech blog discovery I’ve made this year :).
    —- I mean it. Great Post. Thanks.

    please also put the info on how to set the background to black by default(although this can be done by a bit googling but when seeing for the first time the code completion , the adernaline goes much higher when seen on black background).

  2. Marco

    I follow your tutorial but I’ve got an error in the .vimrc parsing. line 19:
    E337: Menu not found – check menu names

    I’m on Ubuntu 8.10


  3. Francois Viljoen


    I was also using Ubuntu 8.10 when I wrote this tutorial.

    What is the text at line 19 in your .vimrc file?

  4. Francois Viljoen


    You need to change your VIM colorscheme to set the background to black.

    Try putting something like

    colorscheme desert

    in your .vimrc file.

  5. Marco

    Sorry, my fault. Works fine!

  6. tony

    Wow! This is one of the most useful set of instructions that I’ve read in a while. Kudos, man! BTW, the ctags file I generated for the entire boost library was 1.4GB!

  7. Francois Viljoen


    1.4GB… hectic.

    It may be that OmniCppComplete only uses a subset of the generated CTAGS content.

    perhaps it will be possible to get the size down by playing with the CTAGS options or post-processing the generated file??

  8. eric

    Great work !
    Same problem with boost template library..: it appears that ctags definitly have problems with templates.
    The generate ctags file is huge and the auto-completion in gvim for boost:: is very slow …
    does anyone fix this ?

    Nice tuto again !

  9. jeremy zhang

    Hi there,

    really like this article and it help me a lot. but there is one thing might worth be mentioned :

    for the command to generate tag $ ctags -R –c++-kinds=+p –fields=+iaS –extra=+q ./ , I think instead of use “./” ,the absolute path should be used otherwise when you move the generated tags to elsewhere in the file system, ctags will suppose that all the std files all in the same directory with the tags file .

    Just my thoughts, it might be other explaination for this issue .
    anyway, thanks again.

  10. Ruslan

    Hi! It is a great article! Thanks a lot!

    But I think you have fogot about headers without extention *.h in stl, like string, memory, valarray
    $ find . -name ‘*.h’ | xargs sed -i ‘s/__STL_BEGIN_NAMESPACE/namespace std {/’
    $ find . -name ‘*.h’ | xargs sed -i ‘s/__STL_END_NAMESPACE/}/’

  11. Yaman

    Thanks for the article.

    Please be carefull to a tiny detail :
    - “voila” is a French word, that mean exactly what you think (something like “ta-da”)
    - “viola” is a French word too. It comes from the verb “violer” which means “to rape”.

  12. Francois Viljoen



    Thanks for pointing that out. :)

  13. alex

    omnicppcomplete doesn’t seem to know what a smart pointer is. Does anyone know how to solve this:

    class C {
    public: int get_int() {return 10;}

    shared_ptr cp(new C);
    cp-> // complete shows get_int() as well here, not just the shared_ptr interface!!!

    thanks in advance,


  14. Christopher

    Great article.

  15. Sai Kumar

    Nice one.

    Thank you

  16. Žilvinas

    Great tutorial, thank you very much!

Add a Comment