Use Vim to develop software projects

Source: Internet
Author: User
Tags prototype definition tagname

For more information, see:

Http://www.kuqin.com/developtool/20080601/9136.html

Section 1:

The help entry for the commands used in this section:

: Help 'tags'
: Help: Tag
: Help: tags
: Help Ctrl-]
: Help CTRL-T
: Help vimgrep
: Help CW
: Help Pattern

Although there are already a lot of related articles, the Tag file is very useful, so it is a long time.

Tag files are undoubtedly a powerful tool for developers. With the help of tag files, you can view the function call relationships, classes, structures, macros, and other definitions in vim, any tag
...... I believe that those who have used source insight are familiar with these functions. In vim, the implementation of this function depends on the Tag file.

For programs, the Tag file stores names such as functions, classes, structures, macros, the files in which they reside, and how to jump to these tags using the ex command. It is a plain text file, so you can edit it manually or operate it using a script.

Generally, we use a program named ctags to generate such a Tag file. Vim can directly use the Tag file generated by the ctags program. Ctags in UNIX systems
So we usually use exuberant
Ctags (in most Linux systems, it is the default ctags Program), it can support up to 33 programming languages, enough to meet our development needs. If this is not installed on your system
Program, please download to http://ctags.sourceforge.net.

Emacs uses etags to generate tag files. If you want Vim to support the Tag file format of etags, add the "+ emacs_tags" option when compiling vim.

The tag file must follow a certain format. The Tag file generated by the exuberant ctags is in the following format by default:

{Tagname} {tab} {tagfile} {tab} {tagaddress} {term} {field }..

The name of the {tagname} identifier, such as the function name, class name, structure name, and macro. It cannot contain tabs.

{Tagfile} contains {tagname} files. It cannot contain tabs.

{Tagaddress} can locate the {tagname} ex command at the cursor position.

{Term} is set to ";". This is to be compatible with the VI editor, so that vi ignores the {field} field.

{Field} .. this field is optional and is usually used to indicate that the type of {tagname} is a function, class, Macro, or other.

Between {tagname}, {tagfile}, and {tagaddress}, use a Tab character ("/t" in C) to separate them, that is to say, {tagname} and {tagfile} cannot contain tabs.

The tag file can start with "! A row starting with _ Tag _. It is used to add other information to the Tag file. Vim can recognize two of these tags, often using the "_ tag_file_sorted" tag, for example:

! _ Tag_file_sorted <tab> 1 <tab> {anything}

The above tag indicates that the Tag file is sorted, and the time zone is case sensitive. For the sorted tag, VIM uses the binary method for searching, which greatly accelerates the search speed; if the value is 0, the tag files are not sorted; if the value is 2, the tag files are case-insensitive.

The reason for this introduction is that the format of the Tag file is generated by yourself in the lookupfile plug-in mentioned later.

 

Although ctags has many options, it is usually very simple to use. Let's take the vim 7.0 code as an example. We execute:

Cd ~ /Src/vim70
Ctags-r SRC

The above command will be in ~ The/src/vim70/directory generates a file named tags, which contains ~ Labels in all. C and. H files under the/src/vim70/src/directory. It is a text file. You can use Vim to open it. This file is sorted by case-sensitive characters by default, so it can be used directly by VIM.

Now let's go to vim and execute the following command:

: Cd ~ /Src/vim70 "switch the current directory ~ /Src/vim70
: Set tags = tags "set the tags option to the tags file in the current directory

Now, we have set the tags option, and then we will use it:

: Tag vimmain

You will see that Vim opened the src/Main. c file, and positioned the cursor on line 7 vimmain.

We know that the function name of the main program is main. If you try the following command:

: Tag main
# Pri kind Tag file
1 f main src/xxd. c
Main (argc, argv)
2 fs d main src/if_python.c
46
Choice number (<enter> cancels ):

There is no src/Main. c file here. Why? This is because ctags is not a compiler, and it is limited in processing the compilation preprocessing commands, so it does not generate a label for the main () function in src/Main. C. You can specify parameters for the ctags when generating the Tag file to solve this problem.

Alternatively, you can use ": grep" or ": vimgrep" to find the main (this is beyond the scope of this article, so we will only give an example and explain it in subsequent articles ):

: Cd ~ /Src/vim70
: Vimgrep // <main/>/src/*. c
: CW

In this case, the following window is displayed. In the quickfix window, locate the position we want to jump to (src/main in this example. c) Press enter to jump to the corresponding position.

If you only remember some tag names, you can use the search mode of the "tag" command. You can enter a Vim regular expression to indicate the name you are looking for, for example:

: Tag // C/</k/+ AIN/>
# Pri kind Tag file
1 f vimmain src/Main. c
Vimmain
2 f d bindtextdomain src/Vim. h
483
3 f d bindtextdomain src/Vim. h
502
4 f d bindtextdomain src/Vim. h
504
5 f main src/xxd. c
Main (argc, argv)
6 f d textdomain src/Vim. h
488
7 f d textdomain src/Vim. h
510
8 f d textdomain src/Vim. h
512
9 fs d bindtextdomain src/gui_gtk.c
54
10 fs d bindtextdomain src/gui_gtk_x11.c
37
11 fs f cmdsrv_main src/Main. c
Cmdsrv_main (argc, argv, servername_arg, serverstr)
12 fs d main src/if_python.c
46
13 fs d textdomain src/gui_gtk.c
51
14 fs d textdomain src/gui_gtk_x11.c
34
Choice number (<enter> cancels ):

This indicates that I want to find a tag that starts with one or more keywords. This Tag ends with Ain and is case sensitive in the search time zone. To read this regular expression, please ": Help pattern ".

 
Vim will save a jump tag stack to allow you to jump back after you jump to a tag. You can run the ": tags" command to find the location where you are on the tag stack.

We often use the tag jump command see the next (generally only need to know Ctrl-] And the CTRL-T can be ):

: Tag {ident} "Jump to the specified tag
: Tags "display tag Stack
CTRL-] "Jump to the tag under the current cursor
CTRL-T "jumps to an earlier tag in the tag Stack

 

For more information, see ": Help 29.1”(usr_29.txtand usr_30.txt ).

To learn more about tag commands and related knowledge, click ": Help tagsrch ".

I wrote an article about ctags and csags. For more information, see VIM + csags/ctags.

Section 2:

The help entry for the commands used in this section:

: Help helptags
: Help taglist.txt
The previous article describes how to use the Tag file in Vim. This article describes how to use the taglist plug-in.

I must have used source
Insight people remember this feature: Si can display the macros, global variables, functions, and other tags in the current file in the symbol window. Click the above tag with the mouse to jump to the tag.
Defined position. It can be sorted in alphabetical order, the class or scope to which the tag belongs, and the position where the tag appears in the file. If you switch to another file, the symbol window is updated and displayed.
Tag in this file.

In vim, The taglist plug-in Implements similar functions. Some functions are weaker than Si, and some functions are stronger than SI. In addition, the taglist plug-in is still being improved!

To use the taglist plug-in, you must meet the following requirements:

1. Enable the vim file type automatic detection function;
2. the exuberant ctags tool is installed in the system, and the taglist tool can be found (because the taglist needs to be called to generate the Tag file );
3. Your Vim supports system () calls;
In
In the first article in this series (vimrc preliminary), we used the vimrc example provided by VIM, which has enabled the file type detection function. In the previous article, we also used
Exuberant ctags; System () calls are supported in common Vim versions (only SuSE
In Linux, this feature is disabled for security reasons. Therefore, these three conditions are met.

Now let's go to http://www.vim.org/scripts/script.php? Script_id = 273 download the latest version of taglist plugin. The current version is 4.3.

After downloading the file ~ Decompress the/. Vim/directory, which will be in your ~ /. Vim/plugin and ~ Put one file in the/. Vim/doc directory:

Plugin/taglist. vim-taglist plugin

DOC/taglist.txt-taglist Help File

Note: Windows users need to decompress this plug-in your $ Vim/vimfiles or $ home/vimfiles directory.

 
Use the following command to generate a Help Tag (the following operations are performed in VIM ):

: Helptags ~ /. Vim/doc

After the Help Tag is generated, you can use the following command to view the help of the taglist:

: Help taglist.txt

Taglist provides a considerable number of features, which are configured in my vimrc as follows:

""""""""""""""""""""""""""""""
"Tag list (ctags)
""""""""""""""""""""""""""""""
If mysys () = "Windows" "sets the location of the ctags program in Windows
Let tlist_ctags_cmd = 'ctags'
Elseif mysys () = "Linux" "sets the location of the ctags program in Linux
Let tlist_ctags_cmd = '/usr/bin/ctags'
Endif
Let tlist_show_one_file = 1 "the tag of multiple files is not displayed at the same time, only
Let tlist_exit_onlywindow = 1 "if the taglist window is the last window, exit Vim
Let tlist_use_right_window = 1 "display the taglist window on the right

After this configuration, the following window is displayed when you enter ": tlistopen:

The taglist window appears on the right side of the screen. You can see all the tags defined in the main. c file: macros, definitions, variables, and functions. You can also double-click
To the position defined by the tag. You can also fold a certain type of tag (using Vim's line-Down Function) for easy viewing, just like macro and variable in the figure.
Sample. For more functions, see the Help page of taglist. This article also introduces some common functions.

 
The following describes common taglist configuration options. You can configure them according to your habits:

-The tlist_ctags_cmd option is used to specify the location of your exuberant ctags program. If it is not in the path defined by your path variable, you need to use this option to set it;

-If you do not want to display tags in multiple files at the same time, set tlist_show_one_file to 1. By default, tags in multiple files are displayed;

-
Set tlist_sort_type to "name" to sort the taglist by Tag name. By default, the taglist is sorted by the order in which the tag appears in the file. Show by tag
The range (namespace or class) of has been added to the todo list of taglist, but not yet supported;

-If you exit Vim when you want to use the taglist window as the last window, set tlist_exit_onlywindow to 1;

-If you want to display the taglist window on the right, set tlist_use_right_window to 1. It is displayed on the left by default.

-In gvim, if you want to display the taglist menu, set tlist_show_menu to 1. You can use tlist_max_submenu_items and tlist_max_tag_length to control the number of menu entries and the length of the displayed tag name;

-By default, When you double-click a tag, it will jump to the defined position of the tag. If you want to click the tag, set tlist_use_singleclick to 1;

-If you want to open the taglist window automatically after Vim is started, set tlist_auto_open to 1;

-If you want to automatically close the taglist window after selecting a tag, set tlist_close_on_select to 1;

-When the tag of multiple files is displayed at the same time, set tlist_file_fold_auto_close to 1, so that the taglist displays only the tag of the current file, and the tags of other files are collapsed.

-When Using tlisttoggle to open the taglist window, if you want to focus on the taglist window, set tlist_gainfocus_on_toggleopen to 1;

-If you want the taglist to always parse the tag in the file, set tlist_process_file_always to 1 regardless of whether the taglist window is opened;

-Tlist_winheight and tlist_winwidth can be used to set the height and width of the taglist window. Tlist_use_horiz_window: 1. Set the horizontal display of the taglist window;

In the taglist window, you can use the following shortcut keys:

<CR> jump to the position defined by the tag under the cursor. Double-click the tag with the mouse.
O display the tag under the cursor in a new window
<Space> display the prototype definition of the tag under the cursor
U update the tag in the taglist window
S. Change the sorting method and switch between name-based and order-based.
The X taglist window is zoomed in and out to facilitate viewing long tags.
+ Open a fold, same as Zo
-Fold the tag, same as ZC
* Open all folds, same as Zr
= Fold all tags, same as ZM
[[Jump to the previous file
] Jump to the next file
Q: Close the taglist window.
<F1> Show Help
You can use ": tlistopen" to open the taglist window and use ": tlistclose" to close the taglist window. Or use ": tlisttoggle" to switch between open and close. The following ing is defined in my vimrc. You can use the ", TL" key to open/close the taglist window:

Map <silent> <leader> TL: tlisttoogle <CR>
The taglist plug-in also provides a lot of ex commands, you can even use these commands to create a taglist session, and then load this session the next time you enter vim.

From: http://blog.csdn.net/easwy/archive/2007/03/01/1518468.aspx

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.