Home PHP The autocompletion C/C + + in vim

The autocompletion C/C + + in vim

Autocompletion is a great feature for programmers who work on various types of programming languages. Vim editor provides this feature under Windows or Linux. The autocompletion of C/C++ in Vim searches for words that are typed in from a file that has previously saved its tags on it. The user, with the hardware’s component of the system help (i the keyboard), will configure a key to jumpstart the process of looking through the stored tags. However, it would be prudent to keep an eye open for any words that do not make sense.

This tutorial is aimed at people running Vim on Linux. Autocomplete is a familiar function to these users, however it can sometimes suggest words that do not make sense in a context. This can be frustrating:

class plop(){ protected: int plopons; public: plop(){} void plopez(){} }; int main(){ plop p; p. //

A plugin based on ctags enables to create a more intelligent Auto-complete that can take into account the context in which a word is used.

Installation

We begin installing ctags. For example, under Debian or Debian based distributions (Ubuntu, Xandros...):

sudo aptitude update

sudo aptitude safe-upgrade

sudo aptitude install exuberant-ctags

You can also obtain the Vim plugin for auto-completion here.

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

mkdir -p ~/.vim/tags

mv omnicpp*zip ~/.vim

cd ~/.vim

unzip omnicpp*zip

cd -

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

It unpacks the archive and creates tags from the STL:

tar xjvf cpp_srcar.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. For example, in the libraries OpenGL, SDL and QT, type the following three commands:

ctags -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, add at the end of the file ~ /. Vimrc the following lines:

" prerequisite 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 :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

noremap :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

inoremap :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .

" 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 tags were only generated for some files, comment on the other one by adding to the beginning of the 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 autocompletion. Simply press Ctrl p or n. Once the list appears, you can use the arrows to highlight the good proposal and press ENTER.

However, it should regenerate the tags of symbols (variables, functions, types ...) specific to the project that is being developed. This will once again generate a tag file. And it will refresh the file every time you add, delete or change a symbol of the project so that it is current.

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

https://vimandomom/wiki/C%2B%2B_code_completion

https://www.vimrg/scripts/script.php?script_id=1520

https://www.vimrg/scripts/script.php?script_id=2358

Image: © Everypixel

  • PHP

LEAVE A REPLY

Please enter your comment!
Please enter your name!