Use OmniComplete in Vim to automatically complete C/C ++

Source: Internet
Author: User

OmniComplete is not the plug-in name, but one of Vim's many completion methods (all-around completion ). To put it bluntly, OmniComplete is actually to guess the content after the cursor Based on the Content Before the cursor. The specific guess depends on the script used.

OmniCppComplete is a complete script for OmniComplete written specifically for C/C ++.

So what is the Ctags that often appear with OmniCppComplete? The full name of Ctags Exuberant Ctags is an independent program (that is, it has nothing to do with Vim ). It can generate language element index files for the source code of various languages. For C/C ++, it is to generate index files for other programs to use various elements in the source code, such as macros, functions, classes, class members, and their related information.

The OmniCppComplete script is completed based on the index file generated by Ctags.

The installation steps are as follows:

Install Ctags
  1. Download the Ctags executable from the official website, the website is http://ctags.sourceforge.net/
  2. Extract the downloaded file (only the EXE file) to a directory, such as D:/ctags.
  3. Add this directory to Environment VariablesPATH
Prepare the index file

Take the generation of the C ++ standard library index file as an example:

  1. Download the libstdc ++ header file modified for Ctags
  2. Decompress it to a directory, such as D:/ctags/cpp_src
  3. Run the following command to enter D:/ctags/cpp_src:
    ctags -R --sort=1 --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f cpp .
  4. We recommend that you put the D:/ctags/cpp_src/cpp file generated in the previous step into a directory dedicated to storing the index file for unified settings. For example, you can put it in D:/ctags/tags.

Index files of other databases can also be processed according to law. You only need to switch to the include folder of the database and execute:

Ctags-R -- sort = yes -- c ++-kinds = + p -- fields = + iaS -- extra = + q -- language-force = C ++-f <File Name>.
Install OmniCppComplete
  1. Download OmniCppComplete
  2. Decompress the downloaded file to the vimfiles folder in the Vim installation directory.
  3. Add in the vimrc File
"Ctags index file (add an index file based on the generated index file. Here I have added an additional hge and curl index file)
Set tags + = D:/ctags/tags/cpp set tags + = D:/ctags/tags/hge
Set tags + = D:/ctags/tags/curl "OmniCppComplete
Let OmniCpp_NamespaceSearch = 1
Let OmniCpp_GlobalScopeSearch = 1
Let OmniCpp_ShowAccess = 1
Let OmniCpp_ShowPrototypeInAbbr = 1 "show function parameter list
Let OmniCpp_MayCompleteDot = 1 "input. Auto-completion
Let OmniCpp_MayCompleteArrow = 1 "input-> auto-completion
Let OmniCpp_MayCompleteScope = 1 "input: auto-completion
Let OmniCpp_DefaultNamespaces = ["std", "_ GLIBCXX_STD"]
"Auto-closing completion window
Au CursorMovedI, InsertLeave * if pumvisible () = 0 | silent! Pclose | endif
Set completeopt = menuone, menu, longest

In addition, you need to confirm thatfiletypeOtherwise, OmniComplete cannot automatically identify the C/C ++ file type for completion.

In this way, when editing the C/C ++ source file in insert mode, press.Or->Or::Or press Ctrl + X Ctrl + O to bring up the auto-completion window. You can move the cursor up and down with Ctrl + N and Ctrl + P.

Beautify?

Maybe you have noticed a problem, that is, "the color of the auto-complete window is ugly 」, only two or three colors of Vim's color schemes change the ugly purple in the auto-complete window. The others are basically against the sky and use Gray to represent the selected items and purple to represent other items.

To change the color of the auto-completion window, add the following to vimrc:

highlight Pmenu    guibg=darkgrey  guifg=black 
highlight PmenuSel guibg=lightgrey guifg=black

PmenuIs the color of all items,PmenuSelIs the color of the selected item,guibgAndguifgCorresponding to the background color and foreground color respectively.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.