Results 1 to 4 of 4

Thread: Autocompletion C / C + + in vim

  1. #1
    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.

  2. #2
    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

  3. #3
    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.

  4. #4
    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.

Similar Threads

  1. Disable Autocompletion in Vista Mail
    By Conrad in forum Tips & Tweaks
    Replies: 1
    Last Post: 19-02-2009, 01:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,392,434.61647 seconds with 16 queries