Random thoughts shooting out of volatile mind
C/C++ Auto Complete for vim
In my last post I told how to make vim give auto complete suggestion for Python. Now its time for C and C++. During our college days in lab we used vim in C/C++ related labs and we had to remember each and every function the outcome was students bi-hearting the programs with out understanding it (of course they wont enjoy programing ;) ). Of course there are many editor with good features but the simple editor like vim also has very good feature (some time can say better than full fledged IDE's). Ok now lets get to the point. You need to download the following things before proceeding
1. OmniCppComplete plugin for the vim
2. exuberant-ctags. (if you are using Debian based distro's you can use this command sudo apt-get install exuberant-ctags)
3. CPP STL source for ctags

1. Unpack the omnicpp*.zip file. the resulting folder has following structure
      |_ doc
      |_ autoload
      |_ after
         |_ ftplugin
            |_ c.vim
            |_ cpp.vim
Copy each of this directory into ~/.vim folder. Note if you already have after directory structure in the ~/.vim just copy the 2 *.vim files into ~/.vim/after/ftplugin/ folder.
2. Now in the .vimrc if you haven't already enabled file type plugin enable it and the set nocp line above it. Your .vimrc should have lines looking like this
  1 " Enable file type plugins
  2 set nocp
  3 filetype plugin on
3. Create tags directory inside the ~/.vim.
4. Unpack the cpp_src.tar.bz2 and generate the tags for stl and move it to ~/.vim/tags directory. Use the following command
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ cpp_src && mv tags ~/.vim/tags/stl
Explanation for the options can be found in the man pages of ctags. In ubuntu machine you may need to use sudo before both command. Similarly you can create tags for the Open GL Qt4 or any other libraries which are present in /usr/include directory. Each library will be present as directory for eg. If you have installed libxml2 you will have libxml2 directory. In the above command just change the directory name to appropriate one and also generated tags file to avoid over writing.

5. Well now some changes needed to do be done in .vimrc file. Copy paste the below code into your .vimrc
  6 " configure tags - add aditional tags here or comment out not-used ones
  7 "set tags+=~/.vim/tags/gl
  8 set tags+=~/.vim/tags/stl
  9 set tags+=~/.vim/tags/libxml2
 10 set tags+=~/.vim/tags/llvmc
 11 set tags+=~/.vim/tags/llvm
 12 set tags+=~/.vim/tags/cairo
 14 " build tags of your own project with CTRL+F12
 15 "map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
 16 noremap <F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<cr>
 17 inoremap <F12> <Esc>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<cr>
 19 " OmniCppComplete
 20 let OmniCpp_NamespaceSearch = 1
 21 let OmniCpp_GlobalScopeSearch = 1
 22 let OmniCpp_ShowAccess = 1
 23 let OmniCpp_MayCompleteDot = 1
 24 let OmniCpp_MayCompleteArrow = 1
 25 let OmniCpp_MayCompleteScope = 1
 26 let OmniCpp_DefaultNamespaces = ["std", "_GLIBCXX_STD"]
 29 " automatically open and close the popup menu / preview window
 30 au CursorMovedI,InsertLeave * if pumvisible() == 0|silent! pclose|endif
 31 set completeopt=menuone,menu,longest,preview

set directive points omnicppcomplete plugin to the proper tags, do comment or uncoment according to tags which you have generated, also ignore the numbers since I had turned on numbering in my vim it came and I'm lazy to remove all the numbers :P.
The next line is mapping of F12 key to generate tags for project you are working on currently. You need to manually use it if you want to use auto complete feature with your currently working file set. Note command assumes that all your files are present in current working directory. Below is the screen shot of my vim's auto complete in action.

Well that's all folks. Hope you will enjoy coding with vim's auto complete feature. C ya


Posted by: copyninja on Sunday, 28 February 2010

blog comments powered by Disqus
Fork me on GitHub