VIM + csags + ctags + tags read source code in Linux

Source: Internet
Author: User

Go to: http://zhwen.org /? P = articles/cs1_tags

The linux Source Code provides the make command to create a cssag index file and a tags file.

I. Software Installation

Install cscope first:

sudo apt-get install cscope

After installing ctags:
Http://ctags.sourceforge.net/

You can use: ctags-version to check whether the current ctags version is emacs.
If emacs is installed, you must use the ctags address above. vim does not support the ctags of emacs. Therefore, you need to download and install the ctags, and then run the command in the directory where the source file is located:

helight@helight-desktop:~/Desktop/ctags-5.7$  ./configurehelight@helight-desktop:~/Desktop/ctags-5.7$ make

You can generate the ctags file and save it to the "/usr/bin/" directory.

Ii. generate an index file

Use the following two commands in the source code directory to generate the cssag index file and tags file.

helight@helight-desktop:~/Downloads/linux-2.6.26$ make cscopeFILELST cscope.filesMAKE    cscope.outhelight@helight-desktop:~/Downloads/linux-2.6.26$ make tagsMAKE   tagshelight@helight-desktop:~/Downloads/linux-2.6.26$ ls tags -l-rw-r–r– 1 helight ftp 69274841 2008-09-27 11:04 tagshelight@helight-desktop:~/Downloads/linux-2.6.26$ ls cscope.* -l-rw-r–r– 1 helight ftp  23814144 2008-09-25 20:08 cscope.in.out-rw-r–r– 1 helight ftp    609665 2008-09-25 20:06 cscope.list-rw-r–r– 1 helight ftp 174470617 2008-09-25 20:08 cscope.out-rw-r–r— 1 helight ftp 124973256 2008-09-25 20:08 cscope.po.out



3. index files are generated in common source files.

For ease of use, I wrote the following script to update the index files of cssag and ctags:

#!/bin/shfind . -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.filescscope -bkq -i cscope.filesctags -R


This command generates three files: csexample. out, csexample. in. out, csexample. po. out.
Cssag. out is the basic symbolic index, and the last two files are generated using the "-q" option, which can speed up the cssag index.
This script first uses the find command to find all the Suffixes in the current directory and subdirectory ". h ",". c "and ". c "file, and redirect the search result to the file cs.pdf. files. Then cssag generates a symbolic index file based on all files in cssag. files. The last command uses the ctags command to generate a tags file and execute ": help" in vim.
The tags command is used to query its usage. It can be used with cscope. The command parameters used above have the following meanings:
-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.
-Q: generate the cssag. in. out and cssag. po. out files to speed up the cssag index.
-K: when the index file is generated, the/usr/include directory is not searched.
-I: If the file name for saving the file list is not cssag. files, you need to add this option to tell cssag where to find the source file list. You can use "-" to obtain the file list from the standard input.
-I dir: Find the header file in the directory specified by the-I option.
-U: scan all files and regenerate the cross-index file.
-C: Case Insensitive during search
-P path: the path added before the file in the relative path. In this way, you can use it without switching to the directory where your database file is located.

4. Search Using cssag in VIM

1. Load the cssag. out file.
It is very easy to use cssag in VIM. First, call the "cssag add" command to add a cssag database, and then you can call the "cssag find" command for search. VIM supports the query functions of eight cscope. For example, if we want to find the function that calls the work () function in the code, we can enter: ": cs
Find c work ". After you press enter, you will find that no matching function is found, and no function may call work (). Enter ": cs find s work" to find the position where the symbol appears. Now vim lists all the positions where the symbol appears. We can also search for strings, which are enclosed by double quotation marks or single quotation marks. You can also enter a regular expression, which is similar to the function of the egrep program.
Open vim in the source code directory. To use cscope for search, you must load the cscope. out file. Run the following command in the vim command line:

:cs add cscope.out

Run the following command in the vim command line:

:cs helpcscope commands:add  : Add a new database             (Usage: add file|dir [pre-path] [flags])find : Query for a pattern            (Usage: find c|d|e|f|g|i|s|t name)       c: Find functions calling this function       d: Find functions called by this function       e: Find this egrep pattern       f: Find this file       g: Find this definition       i: Find files #including this file       s: Find this C symbol       t: Find assignments tohelp : Show this message              (Usage: help)kill : Kill a connection              (Usage: kill #)reset: Reinit all connections         (Usage: reset)show : Show connections               (Usage: show)


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 text that contains this file

2. Use cscope to find the do_fork Function Definition:
Run the following command in the vim command line:

:cs f g do_fork
5. Use the tags search symbol in VIM:

Run the following command in the vim command line:

:tag xxx

You can find the function, data structure, or function xxx you are looking.
For other tags usage, run the ": help tags" command in vim to query.

6. Other commands:

Ctrl +]: Jump to the Function Definition place where the function is called.
Ctrl + t: return the last lookup location

7. Special notes:

The generated cscope. out and tags files must be in the folder where VIM is located. Otherwise, VIM cannot find the relevant symbol information.

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.