How do we browse code through Vim

The most important thing about code browsing is tracking code, tracking definitions, tracking declarations, tracking calls, tracking references…

Vim tracing is typically implemented in two ways:

  • Tags:ctagsThe tool generates the tags file
  • Cscope database: Yescscopegtags-cscopeGenerate the CScope database

The two methods have their own advantages and disadvantages, and can be used together without conflict. Let’s look at them one by one.

ctags

Ctags definition: Generates tag files to help locate objects in source files. Contains the following objects:

  • class names(the name of the class)
  • macro definitions(Macro definition)
  • enumeration names(Enumeration name)
  • enumerators(Enumeration variable)
  • function definitions(Function definition)
  • function prototypes/declarations(Function definition/declaration)
  • class.interface.struct, and union data members(Class, interface, structure, union)
  • structure names(Structure name)
  • typedefs(alias)
  • union names(Consortium name)
  • variables (definitions and external declarations) variable

Install ctags

There are two kinds of ctags that we commonly refer to

  • Exuberant Ctags: This is orthodoxCtags, has fallen into disrepair and was last updated in 2009
  • Universal Ctags: it is oldExuberant CtagsOne of theforkVersion, inherited all of its benefits and is still actively updated

To sum up, it is recommended to install Universal Ctags by using the following method

brew install --HEAD universal-ctags/universal-ctags/universal-ctags
Copy the code

Generate tags

Use ctags -r — c++-kinds=+px — fields=+iaS — extra=+q. You can process the contents of all files in the current directory to generate./tags files. These options do the following:

  • -R: ctags Loops to generate tags for subdirectories
  • --c++-kinds=+pxl: ctagsrecordc++Function declarations and various external and forward declarations in the file (lRepresents logging local variables, which can be thought of aslocalThe abbreviation of)
  • --fields=+iaS: ctagsInformation required to describe, where I indicates that the parent class is identified if there is inheritance; A indicates that if the element is a member of the class, the call permission (that is, public or private) should be specified. S indicates the signature of the function if it is a function.
  • extra=+q: MandatoryctagsIf a syntax element is a member of a class,ctagsThe default is to record a row, you can askctagsA second line of the same syntax element ensures that multiple functions with the same name in VIM can be distinguished by different paths.

Configuring the Tags Path

After the tags file is generated, we need vim to find which tags file corresponds to the current browsing file. We should add such configuration in ~/.vimrc

set tags. = /tags; .tags
Copy the code

First half./. Tags; Tags is a symbol file whose name is.tags. A semicolon is a symbol file whose name is.tags. A semicolon is a symbol file whose name is.tags. The comma-separated tags refer to a directory that is also in vim’s current directory (the: PWD command returns the directory, which can be: CD.. Command change) look for the tags file below.

The use of tags

Tags are very simple to use. If you move the cursor over an element, CTRL+] jumps to the definition, and CTRL+ O goes back to where it started.

There are also a few other tags related commands that can be used:

  • :tag func: jump tofuncWhere the function is implemented
  • :tnext: Next label match
  • :tprev: matches the previous label
  • :tfirst: matches the first label
  • :tlast: where the last label matches
  • :tags: Indicates all matching labels
  • :tselect: displays all matching labels and lets you select the specified one

cscope

Cscope is a similar tool to CTags, but can be considered an enhanced version of CTags because it can do more than CTags:

  • Where is the symbol used?
  • Where is the symbol defined?
  • Where does a variable get its value?
  • What is the definition of a global variable?
  • In what file is this function in the source code?
  • What function calls this function?
  • What function does this function call?
  • The messageout of spaceFrom where?
  • The structure of the source file in the directory?
  • Which files contain this header file?

How to use cscope

Use cscope-rbkq in the project path in the terminal:

  • R: indicates that all files in subdirectories are indexed
  • bSaid:cscopeInstead of launching the native user interface, only the symbol database is created
  • qGenerated by:cscope.in.outcscope.po.outFile, speed up cscope index speed
  • k: Does not search when generating index files/usr/includedirectory

Cscope-rb is usually used to get cscope.out in the current path

Cs is short for cscope, as well as the following command.

Then in VIm use :cs add cscope.out: Add a new CScope database link. Then you can use it in VIm :cs… A series of commands

  • :cs show: View the cScope database links that are currently linked
  • :cs a ...: Find assignments to this symbol
  • :cs c ...: Find functions calling this function
  • :cs d ...: Find functions called by this function
  • :cs e ...: Find this egrep pattern
  • :cs f ...: Find this file
  • :cs g ...: Find this definition
  • :cs i ...: Find files #including this file
  • :cs s ...: Find this C symbol
  • :cs t ...: Find this text string

gtags

Gtags, full name gnu Global, is a cScope-like tool (not a replacement for CTags!). Can also provide cross-indexing between source files. The uniqueness is that once the index file is generated, the process of modifying a file in the entire project and then incrementing the index is very fast.

The installation

brew install global
Copy the code

Once installed, there are three commands: global, gtags, gtag-cscope. Global is query, gtags is generate index file, gtag-cscope is the same interface as Cscope.

gtagsEnvironment configuration

Gtags is supported by way of ctags/ Universal-Ctags or Pyacknowledgments as a front end to analysis in over 50 languages. Ctags/universal-CTags can only be used as a front end to create a definition index, not a reference index, so pyacknowledgs will be installed to ensure that your $PATH contains Python, then:

pip install pygments
Copy the code

Make sure the following two environment variables are set in the environment:

export $GTAGSLABEL = 'native-pygments'
export $GTAGSCONF = '/path/to/share/gtags/gtags.conf'
Copy the code

The first GTAGSLABEL tells GTags that code with default C/C++/Java native support uses the Gtags native parser directly, while other languages use the Pyacknowledmodule. The second environment variable must be added or it will not be explained by native pyacknowledgments and language map. The path to Mac is /usr/local/share/gtags and you can copy it to ~/.globalrc

When pytags is actually used, gtags will start Python and run a script named Pygments_Parser. py to pipe it to complete the source code analysis, so you need to ensure gtags can call Python in $PATH. And the Pyacknowledmodule is installed in Python.

gtagsuse

$ cd project/
$ gtags
Copy the code

Gtags traverses subdirectories, extracts symbols from source files, thus generating index files of the entire directory, including Gtags, GRTAGS, GPATH three database files.

  • GTAGS: Define database
  • GRTAGS: Reference database
  • GPATH: pathname database

You can also use find to generate a list of files called gtags. Files, and then execute gtags to index only the files in gtags.

$ cd project/
$ find . -name "*.[ch]" > gtags.files
$ gtags
Copy the code

gtags-cscopevimThe use of

The commands used for the query are global and gdisgs-cscope. The former is a command line interface and the latter is a CScope-compatible Ncurses interface. Here is no more introduction, focus on how to query in vim:

Enter VIm first and then:

:set cscopeprg=gtags-cscope
:cs add GTAGS
Copy the code

It can then be queried like cscope with cs find g and other commands.

There is no need to call CS reset to restart the cScope child process like CScope. Once connected to gtags-cScope, you never need to restart the database. Whenever you update the database, The gtags-Cscope process can always find the latest symbols.

When we change a file, such as project/subdir1 / subdir2 / file1. C, want to update the index file (index file is the project/GTAGS), just like this:

$ cd project/subdir1/subdir2/
$ vim file1.c
$ global -u
Copy the code

The global-u command will automatically find project/GTAGS up and update their contents. Here’s where Gtags have an advantage over CScope: Incremental updates to individual files are extremely fast, almost instantaneous. With this advantage, we can add an autocmd that automatically updates the index file every time :w.

gutentags– a willctags.cscope.gtagsAn automated tool strung together

Gutentags generates the database for us and automatically cs add the Gtags database to Vim. After you configure it, everything is done silently in the background thread, so you don’t have to worry about anything. Refer to my Gutentags configuration

Editing one item is fine, but if you edit more than two items at once, Gutentags will connect both databases to Vim, so if you search for a symbol, both items will appear at the same time, making it almost unusable.

Skywind3000 / Gutentags_Plus allows you to use this script in conjunction with the skyWind3000 / Gutentags_Plus script. This script will keep loaded databases from being reloaded, and non-loaded databases will be cleaned immediately, so you won’t even notice the existence of gtags. Just use the GscopeFind g command to find the definition, GscopeFind s command to find the reference, and don’t care about the database loading problem or when to update, just write your code, open the project you want to read, The latest results can be queried via GscopeFind at any time and placed in a Quickfix window:

At the end of the script, we also define a series of shortcuts:

  • <leader>cg: Displays the definition of the symbol below the cursor
  • <leader>cs: View the reference to the symbol below the cursor
  • <leader>cc: View which functions call this function
  • <leader>cf: Finds the file under the cursor
  • <leader>ci: Finds which files include this file
  • <leader>cd: Functions called by this function
  • <leader>ct: Find text string under cursor
  • <leader>ce: Find egrep pattern under cursor
  • <leader>ca: Find places where current symbol is assigned
  • <leader>cz: Find current word in ctags database

Set excluded file types

Ctags exclusion is not the same as Gtags exclusion, you need to manually set skip in $GTAGSCONF

The last

My ViM configuration repository: HanleyLee/dotvim

The author of this article is Hanley Lee, first published in Shining Journey. If you agree with this article, please Follow