Ubuntu uses Vim to create a Linux programmer programming artifact (ctags + csags + taglist + code_complete)

Source: Internet
Author: User
Tags function definition function prototype egrep

The so-called "to do a good job, you must first sharpen the tool", the so-called "sharpen the knife without mistaken cut firewood".

In Linux, vim is the most common editor, and many linux distributions come with their own (however, Ubuntu must be installed by itself at present, with only vi by default ). Today, we have been engaged in Vim for a long time. We have used several plug-ins to arm my vim, so that it can become a powerful tool for C/C ++ programmers like in the legend.

Through configuration and plug-ins, the functions are: syntax highlighting + automatic indent + function auto-completion + variable/function tracking + listing functions/variables in the source code.

Tools used: Vim +. vimrc configuration file + code_complete + ctags + taglist + cs.pdf

PS: This effect is amazing !~

Here we will not detail how to use the shortcut keys for various Vim plug-ins. I just want to list my common usage and take a note. I will repost several articles to describe the details of each plug-in detail (too much, I can't remember ).

Vim download address: http://www.vim.org
Code_complete.vim plug-in http://www.vim.org/scripts/script.php? Script_id = 1, 1764
Ctags usually installed after vim will have, ubuntu as if there is no attached installation, need to download: http://www.vim.org/scripts/script.php? Script_id = 1, 610
Taglist Download: http://www.vim.org/scripts/script.php? Script_id = 1, 273
Cscope to its home page download: http://cscope.sourceforge.net/
The command to install these packages in Ubuntu is:

Sudo apt-get install vim
Sudo apt-get install ctags (exuberant-ctags)
Sudo apt-get install cscope


I. Basic vim configuration

1. Configuration file location
Under the/etc/directory, there is a file named vimrc, which is a public vim configuration file in the system and is valid for all users. In each user's HOME directory ($ HOME), you can create a private configuration file (only valid for this user) named ". vimrc ". For example, A. vimrc file already exists in the/root directory. I created the. vimrc file in my $ HOME directory to configure it.
2. Set syntax highlighting
1) open. vimrc and add the following statement to highlight the syntax:
Syntax on
2) if the syntax is still not highlighted, add the following statement to the profile file in the/etc directory:
Export TERM = xterm-color
3. set C/C ++ Auto Indent (add the following set statement to. vimrc)
1) it is not discussed whether the tab width is 8 or 4 spaces. Here, the (soft) tab width is set to 8 (Linus specifies the C-code style in Linux kernel ):
Set tabstop = 8
Set softtabstop = 8
2) set the number of spaces for indentation to 8.
Set shiftwidth = 8
In addition, the "set expandtab" option will expand (replace) the TAB with spaces of the corresponding number. I do not recommend this option.
3) set automatic indent: that is, the indent value of each line is equal to that of the previous line; use noautoindent to cancel the settings:
Set autoindent
4) set the automatic indent mode in C/C ++ language:
Set cindent
5) set the specific indent mode of the C/C ++ language (the following settings are for reference only ):
Set cinoptions = {s, t0, n-2, p2s, (03 s, =. 5 s,> 1 s, = 1 s,: 1 s
6) to display the line number of the text on the left, use the following statement:
Set nu
PS: automatic indent has two options: "set autoindent" and "set cindent ".
Autoindent means automatic indentation. When you insert a new row with the enter key in the input state or insert a new row with o or O in the normal state, autoindent automatically copies the indentation of the current row to the new line, that is, "auto-alignment". Of course, if you do not enter any characters in the new line, the indent will be automatically deleted.
Cindent is different. It will automatically adjust the indent length according to the C language syntax. For example, when you enter half a statement and press enter, the indent will automatically add a TABSTOP value, when you type a right curly braces, a TABSTOP value is automatically reduced.

II. code_complete configuration

Copy the downloaded code_complete.vim file to the plugin directory, which is usually under (my)/usr/share/vim/vim72/plugin (different vim versions of vim72 may be different, my current version 7.2)
Use the key in vim to complete the function prototype. For details about the definition of the complete shortcut key (for example, in stands for # include "), see the definition in the code_complete.vim file.

III. Use of ctags

Use the ctags command to generate the tags file, ctags-R-c-kinds = + p-fields = + S/usr/include should be regarded as creating an index table (the tags file will be generated in the current directory by default ), then copy the generated tags file to your programming directory.
Note: If no absolute path is added after ctags-R, indexes of all files under the current directory will be created. Therefore, the index files created are relative paths written, when you copy the tags file to another directory, it will not find the corresponding function/variable.
Note: when running vim, it must be run in the directory where the "tags" file is located. Otherwise, run the ": set tags =" command to set the path of the "tags" file before vim can find the "tags" file.
"-R" indicates recursive creation, which includes the source program under all subdirectories under the source code root directory.
The "tags" file contains a list of these objects:
Macro defined by # define
Enumerated variable value
Function definition, prototype, and declaration
Namespace)
Type Definition (typedefs)
Variables (including definitions and declarations)
Class, struct, enum, and union)
Class, structure, and union member variables or functions

VIM uses the "tags" file to locate the marked objects above. The following describes how to locate these objects:
1) use the command line. Add the "-t" parameter when running vim, for example:
[/Home/admin/src] $ vim-t foo_bar
This command will open the file defining "foo_bar" (variable or function or other), and position the cursor to this line.
2) use the ": ta" command (abbreviated as the "tag" command) in the vim editor, for example:
: Ta foo_bar
3) the most convenient method is to move the cursor over the variable or function name, and then press "Ctrl-]". Use Ctrl-o to return the original location.
Run ": help tags" in Vim to query the ctags usage.

IV. Cssag configuration and use

You can.

[Root @ jay-cscope-15.8a] #./configure
[Root @ jay-cscope-15.8a] # make
[Root @ jay-cscope-15.8a] # make install
[Root @ jay-linux cscope-15.8a] #/usr/local/bin/csversion -- version
/Usr/local/bin/cs8a: version 15.8a

Some syntax errors may be reported during "make" compilation. For example, if the curses. H file is not found, you need to install the "ncurese-dev" software package.

Run the "cs1_rbq" command in the source code directory where you need to create a cs1_index to generate a cs1_index.

Then use Vim to open a c source program file and run the command ": cs add cscope. out to load the index file (if the directory where Vim is running contains csflood. out, then Vim will automatically load the cssag index ). Use commands starting with "cs" (for example, common ": cs fine xx yy) to use the powerful cscope function.

The query commands supported by "cs find" are described as follows:

S: Find the C Language symbol, that is, find the place where the function name, macro, enumerated value, and so on appear
G: find the locations defined by functions, macros, and enumeration, similar to the functions provided by ctags.
D: find the function called by this function.
C: find the function that calls this function.
T: Find the specified string
E: Find the egrep mode, which is equivalent to the egrep function, but the search speed is much faster.
F: find and open the file, similar to the find function of vim.
I: find the file that contains this file

For example, ": cs1_find s kvm_assign_device", ": cs f g kvm_assign_device", ": cs f d kvm_assign_device", and so on (note: cs1_command is abbreviated as cs, and fine is abbreviated as f ).

In vim, run the ": cs help" command to display the cssag command help manual.

Ctags and csags are very popular in the Linux World. Makefile in Linux kernel specifically defines the "tags" and "cscope" targets, you can directly "make tags" to generate the tags file. You can directly "make cssag" to generate the cssag index file, as shown below.

[Root @ jay-linux. git] # make help | grep-E '(tags | cscope )'
Tags/TAGS-Generate tags file for editors
Cs1_generate cs1_index
Gtags-Generate gnu global index
[Root @ jay-linux. git] # make tags
GEN tags
[Root @ jay-linux. git] # ll tags
-Rw-r -- 1 root 138602743 Oct 19 14:10 tags
[Root @ jay-linux. git] # make cscope
GEN cscope
[Root @ jay-linux. git] # ll cscope .*
-Rw-r -- 1 root 652930 Oct 19 cscope. files
-Rw-r -- 1 root 267625646 Oct 19 CSCT. out
-Rw-r -- 1 root 53239808 Oct 19 cscope. out. in
-Rw-r -- 1 root 384600480 Oct 19 cscope. out. po

V. Configuration and use of taglist

Download the taglist compressed package and decompress it. cp plugin/taglist. vim/usr/share/vim/vim72/plugin/; cp doc/taglist.txt/usr/share/vim/vim61/doc/
Of course, if it is only valid for the current user, you can copy the above file to the $ HOME/. vim/directory.

Start vim and use ": helptags. "to configure the help file, restart vim, and use": TlistToggle "to open and close the taglist window (you can also use the": TlistOpen "command to open and focus the input to the tag list window, ": TlistClose" command to close the tag list window). You can use ": help taglist" to obtain more help information.

In addition, the tag list window is opened on the left. You can use ctrl-W + W to switch between the source code window and the tag window (which is essentially a normal Vim window ).

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.