Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-04-2009
Member
 
Join Date: May 2008
Posts: 29
Autocompletion C / C + + in vim

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.


Code:
class plop () ( 
    protected: 
     int plopons; 
    public: 
     plop () () 
     void plop () () 
  ); 

  int main () ( 
    plop p; 
  P.  / / <- Ctrl P will turn: Plop, plop ...  then it is necessarily 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 + +.

Ctags When considering a tree of source files, it creates a file (called tags) that references each of the symbols contained therein.
Reply With Quote
  #2  
Old 21-04-2009
Member
 
Join Date: May 2008
Posts: 29
Re: Autocompletion C / C + + in vim

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

Code:
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:

Code:
 mkdir-p ~ / .vim / tags 
  * zip omnicpp mv ~ /. vim 
  cd ~ /. vim 
  unzip zip omnicpp * 
  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:

Code:
tar xjvf cpp_src.tar.bz2 
  ctags-R - c + +-kinds = + p - fields = + IAS - extra = + q - language-force = C + + & & cpp_src tags mv ~ / .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:

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

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

  ctags-R - c + +-kinds = + p - fields = + IAS - extra = + q - language-force = C + + / usr/include/qt4 / tags & & mv ~ / .vim/tags/qt4
Reply With Quote
  #3  
Old 21-04-2009
Member
 
Join Date: May 2008
Posts: 29
Re: Autocompletion C / C + + in vim

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:

Code:
"Prerequisites 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 
  to 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:

Code:
 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.
Reply With Quote
  #4  
Old 21-04-2009
Member
 
Join Date: May 2008
Posts: 29
Re: Autocompletion C / C + + in vim

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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Autocompletion C / C + + in vim"
Thread Thread Starter Forum Replies Last Post
Disable Autocompletion in Vista Mail Conrad Tips & Tweaks 1 19-02-2009 01:41 PM


All times are GMT +5.5. The time now is 02:38 PM.