Install and use ctags in VIM

Source: Internet
Author: User

In the past two days, I saw a blog on the Internet that successfully transformed Vim into a powerful ide. Suddenly, I experienced it myself.

All posted by netizens are good. Here I just list various plug-ins separately and then add some tips.

I hope this article will help you!


"Plugin Introduction 』

The ctags tool is used to traverse source code files to generate tags files. These tags files can be used by the editor or other tools to quickly find the symbols (tag/symbol) in the source code, such as the variable name, function name. For example, the tags file is the basis for taglist and omnicppcomplete.

"Download and install 』

1. I installed it with APT-get:

Sudo apt-Get install ctags

Ii. Download the source code for installation (available online)

1) decompress the source code directory after downloading the source code package from http://ctags.sourceforge.net,

2) then go to the source code root directory and execute./configure,

3) then execute make,

4) after the compilation is successful, execute make install.

"Basic Function usage 』

List of Common commands:

1. $ ctags-R * ($ is a Linux shell prompt)

2. $ vi-T tag (replace the tag with the variable or function name you want to find)

3.: TS (the command starting with ":" In tags list is the command line mode command in VI)
4.: TP (TP note: tags Preview)
5.: tn (TN mnemonic: tags next)
6. CTRL +]
7. CTRL + T

Command explanation:

"$ Ctags-R *": "-R" indicates recursive creation, which includes all subdirectories under the source code root directory (current directory. "*" Indicates all files. This command will generate a "tags" file in the current directory. When you run VI in the current directory, it will automatically load this tags file.

The tags file contains a list of these objects:

  1. Macro defined by # define
  2. Enumerated variable value
  3. Function Definition, prototype, and Declaration
  4. Namespace)
  5. Type Definition (typedefs)
  6. Variables (including definitions and declarations)
  7. Class, struct, Enum, and Union)
  8. Class, structure, and union member variables or functions

Vim uses this "tags" file to locate the marked objects above.


The remaining commands are the methods for locating these objects:

"$ Vi-T tag": Add the "-T" parameter when running vim, for example:

[/Usr/src] $ vim-t Main

This command will open the file defining "Main" (variable or function or other), and move the cursor to this line.


If this variable or function has multiple definitions, in the VI Command Line Mode

": Ts"Command to list a list for the user to choose from.

": TP"Mark the file for the previous tag,

": Tn"Mark the file for the next tag. Of course, if the current tags file contains only one variable or function name, the ": TP,: tn" command is unavailable.


The most convenient way is to move the cursor over the variable or function name, and then press"Ctrl +]"In this way, you can directly jump to the variable or function-defined source file and position the cursor to this line. Use"Ctrl + T"Can be returned to the original place. Even if you use "Ctrl +]" for n times to find n variables, you can press "Ctrl + T" for n times to return to the originally opened file, which will be returned in the original way.

More functions run the help ctags command under the command man ctags or Vim command line to query.


Note:When running vim, you must run it in the directory where the "tags" file is located. Otherwise, run the ": settags =" command to set the path of the "tags" file so that Vim can find the "tags" file.

When coding, You can manually delete the tags file (the hacker cannot get it, and the dust won't run away by yourself ^_^ ).


"Read instructions 』

1) Vim has multiple configuration files vimrc, such as/etc/vimrc. This file affects Vim of the entire system. And ~ /. Vimrc. This file only affects the user's vim. And ~ The configuration in the/. vimrc file overwrites the configuration in/etc/vimrc. Here we only modify ~ /. Vimrc file.

2) The Vim plug-in is installed in the runtimepath directory of VIM. You can run the "set RTP" command on the vim command line to view the plug-in. Here we choose to install in ~ /. Vim directory. If no directory exists, create one.

3) when this article says "run the cmdxx command under the vim command line", it means to run the cmdxx command in the command line mode of vim, in the normal mode of VIM, enter the colon ":" to enter the command line mode, and then enter the command running xx. The colon ":" is omitted in the subsequent description.

4) if "run a command under Vim command line" is not specified, the command is executed in shell.

5) If the command is left blank or contains characters that are easily confused with the body, I will use double quotation marks to distinguish the command from the body. Therefore, do not enter quotation marks at the beginning and end of the command during actual operations.

6) This article describes the combination of shortcut keys, in the form of a-B shortcut keys, that means to press the key and B key at the same time, and in the form of "A-B C" shortcut keys, it means to press the key and B key at the same time, then open the AB key, and then press the C key.

7) the system I use is ubunt 11.10, And the vim version is VI improved 7.3.

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.