Use VIM + cscope in Linux to view code

Source: Internet
Author: User
Tags egrep

In the past two days, I want to write a large amount of code in Linux. I heard that VIM + csinsight can implement functions similar to sourceinsight, and it took me half a day to learn how to use csinsight.

1. Prepare Vim

Vim -- version | GRPE
Cscope to check whether Vim supports cscope. If not, reinstall vim. The simplest is in./configure
After adding-enable-cscope, you can modify it in the MAKEFILE file (./src/makefile) (remove the original comment), which is the most fundamental:
Conf_opt_cs1_= -- enable-cs1 _
Then install: Make & make install

2. cscope preparation

Install cscope (my version 15.6). Because it is not a root user,./configure -- prefix = $ home/local/csfix is the address of the executable and man files.

3. generate the required file

The first two are available. Now we can use cscope in VIM to view the code. What do you think ?? Like sourceingight, cssag requires some data files (databases & indexes) to search for keywords related to the code.
These files need to be manually generated:Format: cssag [Options]


The following are common cssag options:

Enter the root directory of the code and then: cs1_rbq. This command generates three files: csexample. Out, csexample. In. Out, csexample. Po. Out. Its
Cssag. Out is a basic symbolic index, and the last two files are generated using the "-Q" option, which can speed up the cssag index.
Cssag parses only C files by default (. C and. h), Lex file (. l) and YACC files (. y), although it can also support C ++ and Java, but it will jump when scanning the Directory
Files that have passed the C ++ and Java suffixes. If you want cssag to parse C ++ or java files, you need to save the names and paths of these files in a file named cssag. Files.
. When cssag finds that cssag. files exists in the current directory, it will generate an index database for all files listed in cssag. Files.
Generally, use the following command to generate cscope. files containing CPP files:
Find./-name "*. H"-o-name "*. c"-o-name "*. cpp"> cs.pdf. Files
After cs1_. files is generated, you can obtain the index (. Out) file by using cs1_bq.

-R:
Search for the code in the subdirectory tree when generating the index file
-B:
Only index files are 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 an 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:
In this way, you do not need to switch to the directory where your database file is located or use it.

 

4. Use cscope in VIM

In
Vim
Csflood
It is very simple. First, call"
Cscope add
"Command to add
Cscope
Database, then you can call"
Cs1_find
"Command.
Vim
Supported
8
Type
The cssag query function is as follows:
For example, we want
Search for calls in code
Work ()
Function, 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
", Find this
The position where the symbol appears. Now Vim
Lists all the locations where this 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
Egrep
Program functions.

S:
Search for the C language symbols, that is, find the places where the function name, Macro, and enumeration value appear.
G:
Find the locations defined by functions, macros, and enumerations, similar to the functions provided by ctags.
D:
Find the function called by this function
C:
Find the function that calls this function
T:
Search for the specified string
E:
Searching for the egrep mode is equivalent to the egrep function, but the searching 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

 

As you can see, if you want to search for a keyword, you need to enter a lot of commands to reduce the efficiency. In fact, you can use the shortcut key to achieve quick search. The following is a script segment for cssag option and keyboard ing in. vimrc found on the Internet:

 

"""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""
"Cscope setting
"""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""
If has ("CSAs ")
Set csprg =/usr/bin/cs.pdf
Set CSTO = 1
Set CST
Set nocsverb
"Add any database in current directory
If filereadable ("cscope. Out ")
CS add cscope. Out
Endif
Set csverb
Endif

NMAP <c-@> S: CS find S <C-R> = expand ("<cword>") <CR>
NMAP <c-@> G: CS find G <C-R> = expand ("<cword>") <CR>
NMAP <c-@> C: CS find C <C-R> = expand ("<cword>") <CR>
NMAP <c-@> T: CS find t <C-R> = expand ("<cword>") <CR>
NMAP <c-@> E: CS find e <C-R> = expand ("<cword>") <CR>
NMAP <c-@> F: CS find F <C-R> = expand ("<cfile>") <CR>
NMAP <c-@> I: CS find I ^ <C-R >=expand ("<cfile>") <CR >$ <CR>
NMAP <c-@> D: CS find d <C-R> = expand ("<cword>") <CR>

The first time you use it, you may feel uncomfortable and get used to it. In
Vim
There are many websites
Cscope
Related Plugin: http://www.vim.org/scripts/script_search_results.php? KEYWORDS = cscope & script_type = & order_by = rating & direction = descending & search = search
And the home page of cssag: http://cscope.sourceforge.net/

.

Address: http://hi.baidu.com/ganss/blog/item/bbb676d35a3351023af3cfe2.html

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.