The autocompletion C/C + + in vim

Last update on April 25, 2009 08:03 AM by netty5
Published by netty5

The autocompletion C/C + + in vim







This tutorial is for people who use Vim under Windows or Linux. Here, I will instead direct the tutorial for those running Linux.
Introduction

The auto-completion is something well known since linux uses it regularly, typically in a console. However, the only auto-completion suggests that Vim does not take into account the semantics of the language in which we code.

Indeed when you press Ctrl-N (or Ctrl P) when you are in the process of typing a word, Vim looks in the file a word that begins with the same letters. Unfortunately, the proposed word does not necessarily make sense, because such a self-completion does not take into account that symbolizes the word. Thus, for example, Vim will be prompted to propose a "word" which corresponds to a type where a method is expected.


class plop(){
  protected:
   int plopons;
  public:
   plop(){}
   void plopez(){}
};

int main(){
  plop p;
  p. // <-- Ctrl P proposera successivement : plopez, plop... alors que c'est forcément plopons
  return 0;
}


For the "semantic" is taken into account, we used a plugin based on ctags vim. Ctags allows for "reference" certain symbols (types, functions, classes) for different languages, including C + +.

When Ctags considering a tree of source files, it creates a file (called tags) that references each of the symbols contained therein.

Installation


We begin by installing ctags. For example, under Debian or Debian based distributions (ubuntu, xandros ...):

sudo aptitude update
sudo aptitude safe-upgrade
sudo aptitude install exuberant-ctags


It also recovers the Vim plugin for auto-completion:

http://www.vim.org/scripts/script.php?script_id=1520

We will do everything regarding self-completion in ~ /. Vim:

mkdir -p ~/.vim/tags
mv omnicpp*zip ~/.vim
cd ~/.vim
unzip omnicpp*zip
cd -


Ctags is able to consider the problem of headers QT, OpenGL, SDL. However, for the STL, you must retrieve headers "simple" here:

http://www.vim.org/scripts/script.php?script_id=2358

It unpacks the archive and creates tags from the STL:

tar xjvf cpp_src.tar.bz2
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ cpp_src && mv tags ~/.vim/tags/stl


Now it generates the tags for the libraries installed (to be adapted if the libraries are installed elsewhere). For example, in the libraries OpenGL, SDL and QT, simply type the following three commands:

c
tags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ /usr/include/GL/  && mv tags ~/.vim/tags/gl

ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ /usr/include/SDL/ && mv tags ~/.vim/tags/sdl

ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ /usr/include/qt4/ && mv tags ~/.vim/tags/qt4

Configuration


Now we must tell vim to load the plugin files and the different tags. To do this, simply add to the end of the file ~ /. Vimrc the following lines:

" prérequis tags
set nocp
filetype plugin on

" configure tags - add additional tags here or comment out not-used ones
set tags+=~/.vim/tags/stl
set tags+=~/.vim/tags/gl
set tags+=~/.vim/tags/sdl
set tags+=~/.vim/tags/qt4

" build tags of your own project with CTRL+F12
"map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
noremap <F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<cr>
inoremap <F12> <Esc>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<cr>

" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1
let OmniCpp_MayCompleteDot = 1
let OmniCpp_MayCompleteArrow = 1
let OmniCpp_MayCompleteScope = 1
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


If only some files were generated tags, comment on the other by adding the "beginning of line. For example if we have not generated ~ /.vim/tags/gl and ~ / .vim/tags/sdl:

set tags+=~/.vim/tags/stl
"set tags+=~/.vim/tags/gl
"set tags+=~/.vim/tags/sdl
set tags+=~/.vim/tags/qt4



We just have to save the file and (re) start vim so that they reflect changes to ~ /. Vimrc.

Use


Everything that has been previously tagged (ie in this tutorial tags STL, QT, SDL, and OpenGL) is already available in the auto completion. Simply press ctrl ctrl p or n. Once the list appears, you can use the arrows to highlight the good proposal and press enter.

However, it is not completely finished. It should be (re) generate the tags of symbols (variables, functions, types ...) specific to the project that is developed. This will once again generate a tags file. And of course, it will refresh the file every time you add, delete or change a symbol of the project so that it is current.

As is quite common, it is recommended that you map a key on the keyboard to trigger a process of ctags. In the example file ~/.Vimrc that I gave, this is ensured by pressing F12.

Links


http://vim.wikia.com/wiki/C++_code_completion
http://www.vim.org/scripts/script.php?script_id=1520
http://www.vim.org/scripts/script.php?script_id=2358
Best answers for « The autocompletion C/C + + in vim » in :
Linux - The Vi editor Show Introduction to Vi Despite its very limited ergonomics, Vi i is one of the most popular text editors texte under Unix type systems (with Emacs and pico). Under Linux, there is a free version of Vi called Vim (Vi Improved). Vi (pronounced vee-eye)...
S-video (Y/C) Show The S-Video standard The S-Video standard (for "Sony Video"), sometimes called Y/C, is a video transmission mode with separate components using different cables to carry information regarding luminance (luminosity) and chrominance (colour). An S...
Change the default installation folder C: \ Program Files ShowChange the default installation folder C: \ Program Files The installation of most software programs are set by default in the C: \ Program Files. Below is a means of how to change into another partition. Under Windows Vista, select...
[C language]Checking whether an integer is a prime number Show[C language]Checking whether an integer is a prime number Definition of a prime number Algorithm 1: dividers between 2 and N-1 will be tested Algorithm 2: Even dividers will not be tested, research is limited to odd dividers Algorithm 3:...
[C language]Handling 64-bit integers Show[C language]Handling 64-bit integers Unsigned 64-bit integer Example: Signed 64-bit integer Example: Basically in C language, an unsigned number over 32 bit can not exceed the value of 4 294 967 295. It may happen that you are...
Download NetBeans IDE ShowNetBeans IDE 6.1 is an excellent IDE of C/C ++ development written completely in Java. The syntactic tint, the "Code Completion" and many other options belong to NetBeans. One of the main asset of NetBeans is that it is based on a module concept :...
Windows - Diagnostic tools ShowAccessing diagnostic tools On the Desktop, when you right-click on a drive and go to Properties, the "Tools" tab gives you access to three tools: Defragmenter (c:\windows\defrag.exe) Scandisk (c:\windows\command\scandisk.exe) Backup (c:\program...