Use VIM + ctags + csags to read the source code.

Source: Internet
Author: User
Tags egrep


A good tool can greatly improve the efficiency when reading the source code. Here, I will not talk much about the tool, VIM + ctags + cs.pdf. Here I mainly want to introduce some of the usage skills of this tool combination. I have found it on the Internet (here, thank you for the hard work of those elders.) You have accumulated your own resources. In short, record them.

Csflood requires the cooperation of vim and ctags, which are based on command lines.
The following describes the common methods of the three tools. For more functions, refer to the man manual or official documents.

I. Vim
Reference: http://oss.lzu.edu.cn/modules/lifetype/index.php? OP = viewarticle & ArticleID = 1014 & blogid = 1

Ii. ctags
Installation:

Aptitude install ctags
Run the ctags-R command in the source code root directory to generate a Tag file for the program source code. The-r option indicates
Operation. A label file is also generated for the subdirectory. Vim can search and
Switch back and forth between C language elements of different files.

Simple use of ctags in VIM
1) Jump to the specified function and enter Vim. Use ": Tag func_name" to jump to the function func_name. Use tag
Command, you can use the tab key for matching and continue to press the tab key to switch down.
When a function has multiple definitions

: Tag
Jump to the first definition and jump to the current file first.
: Tnext
Jump to the first
: Tfirst
Jump to the previous count
: [Count] tprevious
Jump to the next count
: [Count] tnext
Jump to the last
: Tlast
You can also choose from all tagnames:
: Tselect tagname

If you want to jump to the identifier containing the block: "tag/block", use the tab key to select. Here '/' is to tell Vim
'Block' is a statement block tag.
2) use the "Ctrl +]" shortcut to jump to the definition of the Function Identifier where the cursor is located.
3) Use "Ctrl + T" to return to the upper layer. If you want to select the identifier starting with write _, tselect/^
Write _ here, '^' indicates the beginning. Similarly, '$' indicates the end. Multiple identifiers with the same name

3. cssag is not installed in Debian by default.


1. Installation: ATP-Get install cs.pdf


Run the cssag command. Two panels are displayed. The top is a display panel of the search result, and the bottom is a search condition.
Specify the panel. Use the tab key to switch between the two panels. You can also use the top, bottom, left, and top pages keys in the same panel.
Change the position. Select an item in the display panel and press enter to enter the file. This is to call Vim to open the file.
It can be used in combination with ctags. Of course, you can also directly use Vim to open the file and read the source code in combination with ctags.
Before use, you must use the "cssag-indexer-R" command to recursively generate the index information file. In special cases
You must use the find command to generate an index information file and specify it to the cssag tool. Cssag provides the following
Nine query methods:

Find this C symbol:
# Search for the specified C symbol
Find this global definition:
# Search for the specified global definition
Find functions called by this function:
# Find the function called by the specified function
Find functions calling this function:
# Find the function that calls the specified function
Find this text string:
# Searching strings
Change this text string:
# Modifying a specified string
Find this egrep pattern:
# Search for matching characters
Find this file:
# Searching for a specified file
Find Files # Including this file:
# Specify the reference header file for search

Enter the Search Condition in the corresponding item, press enter to perform the query, and the result is displayed on the display panel.

K jump to the man page of the Function

Gg = g press indent to indent and format the entire file
= Format the entire line
= A {format the current {} program block

GF searches for files. In. vimrc, set Path =/u/infdev/** is used to set the search path.

/* The LINE folding function can condense a section into one line and only show its approximate .*/
Zo opens the line
ZC hide

In editing mode, you can use Ctrl + O to temporarily Execute Command mode.

[{The command jumps to the beginning of the current code block,
]} Will jump to the end of the current code block.
[[Search for the start of a function
] Lookup the start of a function
] [Search for the end of a function,
[] Looking for the end of a function,
{To the previous empty line
} To the next blank line
# To the previous word that is the same as the current word
* To the next word that is the same as the current word
% Enables the cursor to jump from the current bracket to the matching bracket,

CTRL +] Jump to the location defined by tags = Ctrl + t return the location where the tag is called

GD query variable, only in this file
GD query variable, only in this function

[I: Definition of query Variables
[<Tab> jump to the variable definition

Set makeprg = nmake can be defined by nmake.
: Make
: Cnext jumps to the next compilation Error
: CC to view the complete error message
: Clist: view all error messages

: E B <tab> in the command line, you can use the tab to add all the matches by pressing CTRL + D.
Q: LIST Command windows

Vim has a macro recording mechanism. Enter "QA" to input a macro into register variable 'A. Press down and you can enter the operations you want as usual, but these operations will be recorded by VIM into the macro named 'A', and then press the "Q" key again, the recording of macro 'A' is finished. When you want to repeat the operations you just recorded, you only need to use the "@ A" command. There are 26 available registers for you to record macros.


2. Generate a database for the code
2. First, you need to generate a cssag database for your code. It is easy to generate a database. Run the following command in the root directory of your project:
Cs1_rbq

If it is not C code, use:

$ Find. /-name "*. C "-or-name "*. H "-or-name "*. CPP "-or-name "*. s "| cs1_rbq
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.
The meanings of specific parameters can be obtained through man cscope. -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.


3. Use cscope in VIM
1. When editing with vim:
Vim filename. c
2. Import the generated cscope file to vim.
: CS Add/path/directory of cscope. out code
I am used to switch to the directory where the code is located and then operate, so I directly use:
: CS add cscope. Out.

3. Check whether the database is connected.
: CS s


4 Cs f s xxxx: Find the place where XXXX appears. It can list in detail the function reference of the row of the files and the content of the row.
First, call the "cssag Add" command to add a cssag database, and then you can call the "cscopefind" command for search. Vim supports eight types of cscope query functions. For example, if you want to find the function that calls the work () function in the code, you can enter ": csfind 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 Swork" to find the location where the symbol appears. Now Vim lists all the locations 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.

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 positions defined by functions, macros, and enumeration.
The cs f g main cursor jumps to the position of the main function.

D: Find the function called by this function.
Cs f d main lists the functions called by the main function. after entering the numbers corresponding to the function, you can adjust it to the corresponding function.

C: Find the function that calls this function.
Cs f c main will output the result that does not match, because no function calls the main function

T: Find the specified string
CS f t string

F: Find and open the file


In addition, the keyboard ing is added under. vimrc.
Map <F8>: tlist <CR>
In this way, you can use the shortcut key F8 to directly enter the tlist mode. Note that there is a space between <F8> and.

Add two lines under. vimrc.
Set tags = tags;
Set autochdir
In this way, you can first find the tags file in the current directory when using tags. If not, go to the parent directory for search. In turn.

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.