Complete Vim code

Source: Internet
Author: User
Tags egrep

Source: Use ctags and omnicppcomplete-taglist-cs1_globalHttp://blog.chinaunix.net/uid-11770217-id-323264.html

Automatic Code Completion and code jump reading should be one of the most common functions for programmers. I will not explain what the two refer. Microsoft's Visual Studio relies on these two technologies to firmly occupy the hearts of the majority of Windows programmers (here we must have a strong vs plug-in visual assistant x credit )... But Linux programmers are actually happier. They can do these two functions without spending any money.

Essentially, the implementation of both depends on the same thing: tag. Tag is the keyword in the program. In C ++, it mainly includes variables, function names, and class names. Code auto-completion is actually a tag match (for example, when a programmer inputs a class, class can be used to match a class with a Class C ++ tag ); code jump reading is actually a tag search (for example, if a programmer wants to find a function func (), he just needs to find the location of the func tag in another file ).

[Preparation]

My current system is Ubuntu Desktop 10.04 lts. Of course, the premise of all work is that you can access the Internet and configure an available source.

1. Install the vim and VIM basic plug-ins

We need to install the basic plug-ins of vim and VIM first. These can be installed using apt-get:

Sudo apt-Get install Vim vim-Doc vim-Scripts

Vim-scripts is some basic plug-ins of VIM, including support for syntax highlighting and indentation.

2. Vim configuration file

Vim has two powerful functions:Plug-ins, AndConfiguration File.

The basic Vim plug-in has been downloaded above. The following describes the basic configurations of vim. The system configuration folder of VIM is in the/usr/share/Vim/and/etc/Vim/folders. we generally do not need to change these folders and it will not be easy to recover after the change. We need to create our own configuration file in the user folder. Assume that the user name is user. Enter the user folder (/home/user/) and use gedit to create a file named. vimrc:

3. install and use ctags

Here download ctags: http://sourceforge.net/projects/ctags/files/

Or

Sudo apt-Get install exuberant-ctags (cannot be the ctags of Emacs) (this is recommended)

Ctags divides C/C ++ files into tags and stores them in a file named tags by default. Suppose we have some. h /. c /. the CPP file is in the/home/user/code/folder. Execute the following command in this folder to generate the tags file for this project:

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

In this directory, you can find a file named tags.

4. Code jump

After the above steps, you canWithin this projectThe implementation code jumps, but cannot jump to tags not defined in the Code of this project. For example, in the preceding figure, a Class A is defined locally and an object of Class A is defined in the main function."Ctrl +]"The key combination jumps to the row of Class. Then you can pressCTRL + O"Jump back.

To redirectNon-ProjectTo let Vim know where these tags are. The project is under the/home/user/code/directory. Now, assume that another project is under/home/user/Program, then we need to generate the tags file of the program project under this project (or use the following ctags command ).

After the command is generated, you must add the following content to the vim configuration file (/home/user/. vimrc:

"-- Ctags setting -- set tags = tagsset tags + =./tags" add current directory's generated tags fileset tags + = ~ /Program/tags "Add new tags File

The last line indicates that when searching for the tags file, you also need to search for the tags file in the/home/user/Program/folder. After saving and restarting, you should be able to jump between different projects. This is because we often develop software based on some libraries. We often need this function when we need to view the function or class definitions in the library.

5. Automatic completion Installation

The Automatic completion function can be implemented through a Vim plug-in: omnicppcomplete. Can be downloaded here to: http://www.vim.org/scripts/script.php? Script_id = 1, 1520

This plug-in is implemented based on the ctags database, that is, the tags file.

The above section also describes how to create your own Vim configuration file independent of the system configuration file. When we download the vim plug-in ourselves, we can also create a directory and place our own plug-ins. This directory is usually/home/user /. vim, you also need to create a plug-in subdirectory, a sub-directory of the plug-in documentation. You can go to the/home/user directory and run the following command:

Mkdir. vimcd. vimmkdir pluginmkdir Doc

Then, put the downloaded ZIP file under the. Vim directory and decompress it directly (the plug-in also contains two directories, one plugin and one Doc, so it will be automatically placed in the corresponding directory ).

Finally, I decompressed omnicppcomplete-0.41.zip in ubuntu10.10and put the three folders: After, autoload, and Doc. the Vim folder is not in the plugin folder, because I put these three folders under the plugin folder and open vim, that is, when editing a file with vim, there will be an error message, but put it in. vim can also be automatically completed normally. Try both methods.

Unzip omnicppcomplete-0.41.zip

Plug-in. Then add the following configuration to the vim configuration file:

"-- Omnicppcomplete setting -- set completeopt = menu, menuonelet 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 stored ded fileslet omnicpp_showprototypeinabbr = 1" show function prototype in popup windowlet omnicpp_globalscopesearch = 1let
Omnicpp_displaymode = 1let omnicpp_defaultnamespaces = ["STD"]

(The first few lines provide prompts and Automatic completion of the./->/: and other operators in C ++ ).

6. Automatic completion test

The C ++ standard library code is often used in C ++ development, so Automatic completion of STL is very important. You can download the source code of the C ++ standard library to test the automatic complementing function.

Sudo apt-Get install build-essential

Then, you can find the header file of the standard library under/usr/include/C ++. Generate the tags file in this folder and add it to the vim configuration file (no longer repeat the above content). Then, you can use the automatic complementing function during programming.

I made ctags for all the files under/usr/include/directly. That is to say, if you want to add the class library to the prompt function of VIM code, you only need to execute ctags-r -- C ++-kinds = + p -- fields = + IAS -- extra = + q under the underlying folder of this class library, at this time, a tags file will be generated under this folder, and then the tags file will be added to/home/user /. in the vimrc configuration file (refer to the above)

The following shows the completion of a vector function:

PS: at the auto-completion point, VIM must know the definition that may be supplemented. For example, variables and functions in the namespace STD namespace must be exposed using namespace STD; otherwise, they cannot be completely supplemented. In the. cpp file, this is not a good practice in the. h file. I am not sure whether the problem is due to my own configuration error or the program is not implemented.

Referenced from: http://xiaozhi.de/2010/code-auto-complete-and-jump-to-under-vim

1. Functions and usage of other Vim plug-ins:

The taglist is lean and requires the support of ctags. You can list functions and global parameters on the left. (Sorted)

Csflood is powerful and can jump to functions and some type definitions, but it seems that the definition of analysis enumeration cannot be correctly found under some conditions due to some bugs. That is, view the definition of the type you are using.

The new version of Global can be embedded in VIM to provide complete parsing and type indexes. Compared with cscope, A little worse, the print list of the type reference does not identify the function in which the reference is carried out. In fact, I think global may not be born for VIM. Its main purpose is to use HTML to express related associations and indexes. It is not as appropriate as csflood.

Both csflood and global view the definition of the type you are using. The difference between the two is described above.

You can install the agent based on your own needs and actual conditions. The above plug-ins are based on ctags. Therefore, you must first install ctags and then install other plug-ins.

2. Related Extension Installation
Code
Apt-Get install exuberant-ctags cs1_global

Cppcomplete and taglist are to be downloaded from the extended list of www.vim.org.

Taglist:
Http://www.vim.org/scripts/script.php? Script_id = 1, 273
Cppcomplete:
Http://www.vim.org/scripts/script.php? Script_id = 1, 527

Then create a directory
Code
$ Home/. Vim/plugin

Copy the downloaded cppcomplete. vim and taglist. Vim to $ home/. Vim/plugin.
After global is installed, the latest version 4.8.6 and later has an extension with vim. Copy it to $ home/. Vim/plugin.

After Debian Sid version Global is installed, the extension file is
Code
/Usr/share/doc/global/examples/gtags.vim.gz

Copy to $ home/. Vim/plugin and decompress the package. decompress the package:
Code
Gzip-D gtags.vim.gz

3. Use these tools
1) To prepare, first modify the $ home/. vimrc file.

Add the following content for better use of cssag, so that the jump of Ctrl-] will be analyzed by the tags of cssag.
Code

If has ("CSAs ")
Set csprg =/usr/bin/cs.pdf
Set CSTO = 0
Set CST
Set nocsverb
"Add any database in current directory
If filereadable ("cscope. Out ")
CS add cscope. Out
"Else add database pointed to by Environment
Elseif $ cscope_db! = ""
CS add $ cscope_db
Endif
Set csverb
Set cscopetag
Set cscopequickfix = s-, G-, C-, D-, T-, e-, F-, I-
Endif

2) use cscope
The simplest way to generate a cssag tag is:
In C: cs1_rbq
Run csw.indexer in the top-level directory of your development project. it traverses all the following directories and generates two files. files, this file record needs to generate the tags file name, which can be manually modified, and the other is cssag. out.

Use cscope in C ++
-R: Search for the code in the subdirectory tree when generating the index file
-B: Only the index file is generated, and the cssag interface is not displayed.
-K: when the index file is generated, the/usr/include directory is not searched.
-Q: generate the cssag. In. Out and cssag. Po. out files to speed up the cssag index.

#! /Bin/sh
Find.-Name "*. H"-o-name "*. c"-o-name "*. cc"-name "*. cpp"> cscope. Files
Cscope-bkq-I cscope. Files
Ctags-R

After the job is finished, you generate a directory named csexample. Out to open any file of the project, you can use ctrl-] to jump to the search type definition.
Code
After entering vim, the first thing is to import the generated cssag file to VIM. Use the following command:
: CS Add/home/wooin/vim71/csloud. Out/home/wooin/vim71
The preceding command is very important and must be fully written. You cannot write only the first half of the sentence:
: CS Add/home/wooin/vim71/cs.pdf. Out

Cs f s xxxx is used to find the place where XXXX appears. It can list in detail which file row of which function reference and the content of this row, which is good


Cscope find usage:
CS find c | d | E | f | G | I | S | T name
0 or s search for the c symbol (you can skip the annotation)
1 Or G search for this definition
2 or D. Search for the function called by this function.
3. Search for the function that calls this function.
4 or t to search for this string
6. Or E. Find the current egrep mode.
7. Search for this file.
8 or I
Cscope find shortcut:
Add ~ /. Vimrc, and restart VIM:
NMAP <c-c> <c-k>: CS find S <C-R> = expand ("<cword>") <CR>
NMAP <c-c> <c-G>: CS find G <C-R> = expand ("<cword>") <CR>
NMAP <c-c> <c-l>: CS find C <C-R> = expand ("<cword>") <CR>
NMAP <c-c> <c-t>: CS find t <C-R> = expand ("<cword>") <CR>
NMAP <c-c> <c-e>: CS find e <C-R> = expand ("<cword>") <CR>
NMAP <c-c> <c-F>: CS find F <C-R> = expand ("<cfile>") <CR>
NMAP <c-c> <c-I>: CS find I ^ <C-R> = expand ("<cfile>") <CR>
NMAP <c-c> <c-d>: CS find d <C-R> = expand ("<cword>") <CR>

For how to use all its commands, see:
Code

Help cscope

3) Use taglist
The taglist function is used to generate the function list and global variable list of the current file in real time for indexing.
Use in VIM command mode
Tlist to enable or disable the index of the current file;
Tlistsync immediately locates the function or structure definition where the current cursor is located in the index window that opens.
For other commands, see its help file.

4) Use of ctags
In fact, csags are used to replace the ctags function. ctags is widely used and many other software rely on it.

For example, to generate the tags file required by cppcomplete, run the following command:
Code

Ctags-n-F cppcomplete. tags -- fields = + AI -- C ++-types = + p *-l cs.pdf. Files

Note that the "-l cscope. Files" parameter is used at the end to help ctags query related project files and generate tags by pulling the file index generated by cssag.

5) Use cppcomplete
Cppcomplete usage I just found that it can automatically complete the tags file generated according to the above method.
It is easy to use, that is, when you need to complete the type of members, press F8 to pull. Generally, when it is used for the first time, you need to confirm whether to use the existing tags file or generate it again. Generally, we control the generation time of the tags file by ourselves, and use it to pull it.
However, it is also limited by the poor analysis capability of ctags, and may not help you every time.

6) Use global
Run the following command in the top directory of the project:
Gtags
Wait for it to generate all the required tags in the global format.
After the generation, you need to open any files in your project in the generated tags directory and run the following command to query the reference and association relationships of related types:
Code

Search for the definition of xxxx in gtags xxxx
Gtags-r xxxx
Gtags-s xxxx

Its prompt information is obvious, which is not as intuitive as csflood, but relatively complete for large-scale analysis.
Sometimes it cannot analyze the type normally. You can try to find the type definition by using the parameter with-s. No, I don't know how to pull smile.gif.

Indent-Kr-nut-ts2-I3-L-1 *. c
Valgrind -- leak-check = full -- leak-resolution = high -- show-reachable = Yes

Execute cscope-indexer in the top directory of your development project.
Ctags-n-F cppcomplete. tags -- fields = + AI -- C ++-types = + p *-l cs.pdf. Files

:! Ctags-R
Tlist to enable or disable the index of the current file;
Tlistsync immediately locates the function or structure definition where the current cursor is located in the index window that opens.
U update the tag in the taglist window
S. Change the sorting method and switch between name-based and order-based.
[[Jump to the previous file
] Jump to the next file

Place the cursor in the position of a function or variable. Gd will highlight all functions or variables in the current file, and press n to view the next
Gg cursor returns to the top of the file

CTRL + P: auto match completion
CTRL +]: Jump Ctrl + T: Return jump
Move the cursor to any character in "stdio. H" and type "gf", VIM will automatically open the/usr/include/stdio. h file. Use "Ctrl-o" to return to the original file.

Usage of "cs1_find:
CS find c | d | E | f | G | I | S | T name
0 or s search for the c symbol (you can skip the annotation)
1 Or G search for this definition
2 or D. Search for the function called by this function.
3. Search for the function that calls this function.
4 or t to search for this string
6. Or E. Find the current egrep mode.
7. Search for this file.
8 or I
: CW displayed in the window

VIM:
Copy 5 rows: Y5Y, P
Move the cursor between words W B
Row movement: 0 (first row) $ (last row)
Split Window: Switch to Ctrl + W for split
Scroll down: Ctrl + D Ctrl + u
Replace: % S/four/4/g
Delete multiple lines of N dd

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.