Tags cscope in VIM

Source: Internet
Author: User
Tags egrep
Document directory
  • I. Software Installation
  • Ii. generate an index file
  • 3. index files are generated in common source files.
  • 4. Search Using cssag in VIM
  • 5. Use the tags search symbol in VIM:
  • 6. Other commands:
  • 7. Special notes:
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 of the source file:

helight@helight-desktop:~/Desktop/ctags-5.7$  ./configure
helight@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 cscope
FILELST cscope.files
MAKE cscope.out
helight@helight-desktop:~/Downloads/linux-2.6.26$ make tags
MAKE tags
helight@helight-desktop:~/Downloads/linux-2.6.26$ ls tags -l
-rw-r–r– 1 helight ftp 69274841 2008-09-27 11:04 tags
helight@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/sh
find . -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files
cscope -bkq -i cscope.files
ctags -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 run the ": Help tags" command in VIM 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 eight types of cscope query functions. 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 the matching function is not found, and the function may not 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 functions 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 help
cscope 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 to
help : 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.