Use Vim to implement automatic completion of C ++ code

Source: Internet
Author: User

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. Moreover, as a General Editor, Vim's complete functions are not only limited to program completion, it can complete the file name, complete the file according to the dictionary, complete the file according to the buffer area or similar content in other buffers, and complete the file according to the File Syntax, it even allows users to write their own functions for customized completion.

As an article in the series, This article focuses on the completion of VIM programs, and also introduces other methods of completion. This article is divided into two parts. The first part mainly introduces Vim's omni completion, the second part 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:

 

You can view the 'ommnifunc 'option to know what the current completion function is.

To complete the complete complement of C and C ++ code, you need to use the Tag file generated by exuberant ctags. in the previous article, we introduced how to use the exuberant ctags program to generate the Tag file. However, if your exuberant ctags version is 5.5.4, you need to add the "typename:" field patch to it to support the 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 or the source code. If it is the latter, use the following command to compile the source code:

 

If you do not have the write permission for the system directory, you may want to install exuberant ctags to your home directory. /configure "command changed to". /configure-Prefix =/home/xxx.

After the ctags is upgraded, use "ctags-R" to update the Tag file. Now you can complete the update 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, we enter the "Gui" three characters, and then press "CTRL-X CTRL-O", the status line in VIM will display "Omni completeion ", it indicates that all the matching tags are being completed. A drop-down menu is displayed. You can use it for up and down selection. At the same time, 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.

A key binding is provided at the end of this article, allowing entering a carriage return to indicate completion ends, rather than inserting a carriage return, when using CTRL-P and CTRL-N.

If the completion is active, you can use "CTRL-E" to stop the completion and return to the original entered text. With CTRL-Y, you can stop completing and accept the currently selected items.

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 requires changing the value of 'completeopt'. My settings are as follows:

 

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 ++, You need to download the omnicppcomplete plug-in from the http://www.vim.org, the link is as follows:

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

 
Install details
1) unzip the plugin ~ /. Vim (UNIX) or % homepath %/vimfiles (Windows)
2) Run vim and type the following command:

: Helptags $ home/. Vim/doc

Or

: Helptags $ home/vimfiles/doc

3) type: H omnicppcomplete and please read the installation paragraph.

 

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

 

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

 

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

 

When completing the C ++ file, the omnicppcomplete plug-in needs to include additional C ++ information in the Tag file. 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:

 

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. ", the omnicppcomplete plug-in will automatically pop up the struct test1 member for selection, and after entering" B-> ", it will automatically pop up the class base member for selection, very convenient, do not enter even the CTRL-X CTRL-O. 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.

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.

 

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 pops up, the CTRL-J ing is to flip the page down from the drop-down menu. Otherwise mapped to CTRL-X CTRL-O;

-If the drop-down menu pops up, the CTRL-K maps to pages up in the drop-down menu; otherwise, the CTRL-K is still mapped;

-If the drop-down menu pops up, the CTRL-U maps to the CTRL-E, that is, stop completing; otherwise, the CTRL-U is still mapped;

 

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

 

[Reference]

1. Vim Help File

2. http://vimcdoc.sourceforge.net/

3. http://www.vim.org/tips/tip.php? Tip_id = 1, 1228

4. http://www.vim.org/tips/tip.php? Tip_id = 1, 1386

 

[Note]

This article can be freely used for non-commercial purposes. Indicate the source for reprinting.

Link: http://blog.csdn.net/easwy

 

[Version]

23apr07, easwy, v0.1, initial version

24apr07, easwy, v0.2, add description of ctags Option

15may07, easwy, v0.3, add filetype on, thanks paulhsu


Use Vim in Windows to implement automatic completion of QT code

1. Install vim.
2. Download ctags for Win32 and decompress it to the C:/ctags/directory;
3. Add C:/ctags to the path environment variable of windows;
4. Download omnicppcomplete and decompress it to the vimfiles directory in the vim installation directory;
5. go to the include directory of QT and run ctags-r -- C ++-kinds = + p -- fields = + IAS -- extra = + q.
Generate a tags file, which is a database used by omnicppcomplete and VIM for command completion.
6. Execute the above command again in the src directory of QT;
7. Edit the _ vimrc file under the vim installation directory and add the following two lines
Set tags + = C:/QT/4.3.3/include/tags
Set tags + = C:/QT/4.3.3/src/Tag
C:/QT/4.3.3/is the installation directory of QT on my machine. Make adjustments based on your situation.
8. Go to your project directory and execute the commands in step 5;

9. Use Vim to open a CPP file. Haha;

From: http://blog.csdn.net/lwhsyit/article/details/2907583

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.