Code completion (IntelliSense) for C++ in Vim with OmniCppComplete

    技术2022-05-19  18

    转自:http://design.liberta.co.za/articles/code-completion-intellisense-for-cpp-in-vim-with-omnicppcomplete/

     

    C++ autocompletion is possible with VIM.

    This is officially the coolest tech discovery I’ve made this year.

    And it’s easy to setup.

    Here’s how to configure C++ IntelliSense for Vim, with an example that demonstrates how to enable code completion for the the C++ Standard Template Library (STL) .

    1. Download

    You need the following:

    Vim Exuberant Ctags OmniCppComplete

    2. Install

    With Ubuntu 8.10 you can install Vim and Exuberant Ctags like so:

    sudo apt-get install vim exuberant-ctags

    Install OmniCppComplete by downloading the plugin and extracting it to your Vim settings (~/.vim/ ) folder.

    Then open Vim, and type the following:

    :helptags $HOME/.vim/doc

    3. Configure Vim

    Add the following to your .vimrc file (you only need the required and ctags sections, but I included options I found useful – hack as you please):

    " --- OmniCppComplete --- " -- required -- set nocp " non vi compatible mode filetype plugin on " enable plugins

    " -- optional -- " auto close options when exiting insert mode autocmd InsertLeave * if pumvisible() == 0|pclose|endif set completeopt=menu,menuone

    " -- configs -- let OmniCpp_MayCompleteDot = 1 " autocomplete with . let OmniCpp_MayCompleteArrow = 1 " autocomplete with -> let OmniCpp_MayCompleteScope = 1 " autocomplete with :: let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert) let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype (i.e. parameters) in popup window

    " -- ctags -- " map <ctrl>+F12 to generate ctags for current folder: map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> " add current directory's generated tags file to available tags set tags+=./tags

    4. Generate Ctags

    To generate the ctags and enable code completion for the code you are currently working on (everything in your current working directory and its sub-directories), you can simply hit CTRL+F12.

    To understand how this works, check out the key-binding you enabled in your .vimrc file in step 3 of this tutorial.

    To enable code completion for an external library, you need to:

    Do pre-processing on the library (if needed).Generate a ctags file for the library.Tell Vim where to find the ctags file.

    I will demonstrate using the Standard Template Library (STL) as an example.

    Start by downloading the STL source and extracting it somewhere meaningful (e.g. /usr/local/lib/stl3.3 ).

    For most libraries you can skip the first step, but some libraries (like the STL) will need a little pre-processing because of the complexity introduced by macros.

    Change to the directory where you extracted the STL source, and run the following:

    $ find . -name '*.h' | xargs sed -i 's/__STL_BEGIN_NAMESPACE/namespace std {/' $ find . -name '*.h' | xargs sed -i 's/__STL_END_NAMESPACE/}/'

    Now we’re ready to generate the ctags. In the directory where you extracted the STL source, run:

    $ ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ./

    This will create a file called ‘tags’ in the current directory. Rename and move the file somewhere meaningful, e.g.:

    $ mkdir ~/.myTags $ mv tags ~/.myTags/std3.3.tags

    Lastly, tell Vim where to find the tags by adding the following to your .vimrc file:

    set tags+=~/.myTags/std3.3.tags

    And voila.

    Tags: autocompletion , C++ , code completion , IntelliSense , Linux , omnicppcomplete , UNIX , vim


    最新回复(0)