VI/Vim advanced: Smart completion

Source: Internet
Author: User

Http://easwy.com/blog/archives/advanced-vim-skills-omin-complete/

<Return to vim and use advanced: Directory

The help entry for the commands used in this section:

 
: Help ins-completion: Help compl-Omni: Help 'osmnifunc ': Help I _CTRL-X_CTRL-O: Help ins-completion-menu: Help popupmenu-keys: Help 'completeop ': help compl-omni-filetypes: Help omnicppcomplete.txt

Those who have used source insight must be impressed with its auto-completion function. In many integrated development environments, it also supports auto-completion. Vim, as an excellent editor, is indispensable. In addition, as a General Editor, the complete functions implemented by VIM are not limitedProgramFile Name completion, dictionary-based completion, buffer-based or other buffer-like content completion, File Syntax completion, etc, it even allows users to write their own functions for customized completion.

As an article in The Vim advanced series, This article focuses on the completion of VIM programs, and also introduces other completion methods. This article is divided into two parts. The first part mainly introduces Vim's omni completion, the next section briefly introduces other completion methods, and the supertab plug-in.

Vim's omni completion (hereinafter referred to as "all-around completion") supports multiple programming languages, including C, C ++, XML/html, CSS, JavaScript, PHP, Ruby, etc, for a detailed list, see": Help compl-omni-filetypes". In this article, we will mainly introduce C and C ++'s all-around completion.

Vim sets different completion functions for different types of files based on the file type. That is to say, to implement the all-around complementing function, you need to enable file type detection. Add the following command to your vimrc:

 
Filetype plugin indent on

You can view'Omnifunc'Option to know what the current completion function is.

For C and C ++CodeThe exuberant ctags must be used to generate the complete Tag file.ArticleDescribes how to use the exuberant ctags program to generate a Tag file. However, if your exuberant ctags version is 5.5.4, you need to add it"Typename:"Field patches can support all-around completion of C. Download the patch here:

 
Ftp://ftp.vim.org/pub/vim/unstable/patches/ctags-5.5.4.patch

You can find the compiled executable version on MS-Windows:

 
Http://georgevreilly.com/vim/ctags.html

However, we recommend that you use the latest version 5.6, exuberant ctags. Download from the following website:

 
Http://ctags.sourceforge.net/

You can directly download the compiled rpm version orSource code. If it is the latter, use the following command to compile the source code:

 
Tar zvxf ctags-5.6.tar.gzcd ctags-5.6./configuremakemake install

If you do not have the write permission for the system directory, you may want to install the exuberant ctags to your home directory"./Configure"Change command"./Configure-Prefix =/home/xxx"That's all.

After the ctags is upgraded, use"Ctags-R"Update the Tag file. Now you can complete it in the C program by entering vim. We still use the source code of VIM 7.0 as an example.

For example, in the vimmain () function, input"Gui"Three characters, and then press"CTRL-X CTRL-O", Displayed in VIM status line"Omni completeion", Indicating that all the matching tags are being completed, and a drop-down menu is displayed. You can use it"CTRL-P"And"CTRL-N"Select up and down. When selected, the selected item is placed at the cursor position, and you do not need to press enter to place it at the cursor position (like source insight ).

If you are more familiar with the source insight method, you can use the top and bottom labels to select a project, and press enter to place the selected project to the cursor position. In this way, your finger will leave the main editing area and you need to enter another enter key.

At the end of this article, a key binding is provided to allow"CTRL-P"And"CTRL-N", Enter the carriage return to indicate completion, rather than inserting the carriage return.

If the completion is activated, you can use"CTRL-E"Stop completing and return to the original entered text. "CTRL-Y"You can stop completing and accept the currently selected project.

Click to view the chart

Is a screenshot selected using CTRL-N. In this figure, I select the "gui_exit (" function, and then you can directly enter the parameters of this function. This will end the current completion and insert the parameters I entered.

Click to view the chart

Is to complete the screenshot of the structure members:

Click to view the chart

By default, VIM uses the drop-down menu and a preview window (Preview window) to display matched items. The drop-down menu lists all matched items, and the preview window displays the details of the selected items. Opening the preview window will cause the drop-down menu to shake, so I usually remove the display of the preview window, which needs to be changed'Completeopt'Value. My settings are as follows:

 
Set completeopt = longest, menu

The settings above indicate that only matching items are displayed in the drop-down list, and the same text of all matching items is automatically inserted.

To support all-around completion of C ++, go to the vim homepage to download the omnicppcomplete plug-in. The link is as follows:

 
Http://www.vim.org/scripts/script.php? Script_id = 1, 1520

After the download, decompress it to your. Vim directory (which is the vimfiles directory in Windows). It will install the following files:

 
After \ ftplugin \ CPP. vimautoload \ omni \ common \ debug. vim \ utils. vimautoload \ omni \ CPP \ complete. vim \ mongodes. vim \ items. vim \ maycomplete. vim \ namespaces. vim \ Settings. vim \ tokenizer. vim \ utils. vimdoc \ omnicppcomplete.txt

Make sure that you have disabled the VI compatibility mode and allow file type detection:

 
Set nocpfiletype plugin on

Next, use the following command to generate a Tag file for the c ++ file. Assume that your file is under the src directory:

 
Ctags-r -- C ++-kinds = + p -- fields = + IAS -- extra = + q SRC

When completing the C ++ file, the omnicppcomplete plug-in requires the Tag file to contain additional C ++ information. Therefore, the ctags command above is different from the one we used previously, it generates additional information for the c ++ language. The meanings of the preceding options are as follows:

 
-- C ++-kinds = + P:Add function prototype labels for C ++ files-- Fields = + IAS:Add Inheritance Information (I), class member access control information (a), and function fingerprint (s) to the Tag file)-- Extra = + Q:Add a class modifier to the label. Note: If this option is not available, the class members cannot be supplemented.

Now, go to vim and set the tag option (I have introduced it in the previous article ). Great. Vim can automatically complete C ++!

I wrote a simple example to demonstrate the auto-completion function of C ++, as shown in"T.", The omnicppcomplete plug-in will automatically pop up the struct test1 member for selection, and in the input"B->"Then, the members of the class base will be automatically displayed for selection, which is very convenient and can be connected"CTRL-X CTRL-O"You do not need to enter it. The default settings of the omnicppcomplete plug-in comply with my habits, so you do not need to adjust the settings. If you need to adjust the settings, refer to the omnicppcomplete help page.

Click View big chart to view big chart

The following table lists the key bindings set in my vimrc. pumvisible () is used to determine whether the drop-down menu is displayed. If the drop-down menu is displayed, the key is mapped to a value. If not, it is mapped to another value.

 
"Mappinginoremap <expr> <CR> pumvisible ()? "\ <C-Y>": "\ <CR>" inoremap <expr> <C-J> pumvisible ()? "\ <Pagedown> \ <C-N> \ <C-P>": "\ <C-X> <C-O>" inoremap <expr> <C-K> pumvisible ()? "\ <Pageup> \ <C-P> \ <C-N>": "\ <C-K>" inoremap <expr> <C-U> pumvisible ()? "\ <C-E>": "\ <C-U>"

The above mappings are all ing in insert mode, which are explained as follows:

    • If the drop-down menu is displayed, the carriage return ing accepts the selected items. Otherwise, the ing is still the carriage return;
    • If the drop-down menu is displayed,CTRL-JMaps to pages down in the drop-down menu. Otherwise, the ing isCTRL-X CTRL-O;
    • If the drop-down menu is displayed,CTRL-KMaps to pages up in the drop-down menu; otherwise, it is still mappedCTRL-K;
    • If the drop-down menu is displayed,CTRL-UIngCTRL-E, That is, stop completion. Otherwise, it is still mappedCTRL-U;

In the next article, we will continue to introduce other completion methods provided by VIM.

[Reference]

    • Vim Manual
    • Vim Chinese manual
    • Http://www.vim.org/tips/tip.php? Tip_id = 1, 1228
    • Http://www.vim.org/tips/tip.php? Tip_id = 1, 1386

<Return to vim and use advanced: Directory

For Original Articles, please read the license for footer. For more information, see:Reposted from Yishui blog [http://easwy.com/blog/]

Link:Http://easwy.com/blog/archives/advanced-vim-skills-omin-complete/

The footer information of the article is automatically generated by WordPress's WP-posturl plug-in.

Related Articles
    • VI/Vim advanced: Automatic completion
    • VI/Vim advanced tutorial: Use a Tag file
    • VI/Vim use advanced: Problems Encountered during vim compilation and Solutions
    • VI/Vim advanced application: A Tool for programmers-cs.pdf
    • VI/Vim use advanced: lookupfile plug-in
    • VI/Vim advanced: Use GDB for debugging in vim-use vimgdb
    • VI/Vim advanced: Use the taglist plug-in
    • VI/Vim advanced usage: Mobile, mobile, and flying (2)
Related Article

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.