Use vim + cssag/ctags

Source: Internet
Author: User
From: http://www.cnblogs.com/sunblackshine/archive/2011/08/25/2152962.html + http://easwy.com/blog/

 

With vim + cssag/ctags, You can implement the Source Insight function and conveniently view the Source code for analysis.


Key words: vim, cssag, ctags, tags
1. Check whether vim supports cscope $ vim -- version | grep cscope
2. compile VIM (-- enable-cscope) that supports cssag) # USE = "bash-completion cscope nls perl python-acl-gpm-minimal-ruby-vim-pager-vim-with-x" emerge vim # emerge-av dev-util/ cssag # emerge-av dev-util/ctags $ which csags $ which ctags
3. View help $ man csags $ man ctags: help csags (vim command) refer to [1] http://vimcdoc.sourceforge.net/doc/if_cscop.html
4. Use cssag [2]. The current directory has main. c, where print () in cstest. c is called. This function is declared in cstest. h. Use the following command to generate the symbolic index file of the Code: $ cs1_- Rbkq. This command generates three files: cs1_. out, cs1_. in. out, cs1_. 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 parameter Meanings of the preceding command are as follows:-R: When an index file is generated, search for the code-B in the subdirectory tree: only the index file is generated and the cssag interface is not displayed.
-D: only call out the cssag gui, not with the new cssag. out-k: when generating the index file, do not search for the/usr/include directory-q: Generate cscope. in. out and cscope. po. out file, accelerate the indexing speed of cssag-I: If the file name to save the file list is not cssag. files, you need to add this option to tell cscope where to find the source file list. You can use "-" to obtain the file list from the standard input. -I dir: in the directory specified by the-I option, find the header file-u: scan all files and regenerate the cross-index file-C: Ignore the case-P path: in this way, you do not need to switch to the directory where your database file is located or use it.
By default, cssag will enter its own query interface after the database is generated. This interface is generally not used, so the "-B" option is used. If you have already entered this interface, press the CTRL-D to exit.
Next, you can browse the code $ vim main in vim. c. add the symbol index library in the command status in vim: cs1_add cs1. then, you can view the corresponding function definition or file, and press ctrl + t to return. : Cs1_find g print: cs1_find f cstest. h # Note # The generated cs1_out and tags files must be in the folder where VIM is opened. Otherwise, VIM cannot find the relevant symbol information.
5. Create a shortcut to add the following content ~ /. In vimrc, vim automatically loads the symbol index cs1_out in the current directory. You can use ctrl + t or ctrl +. """""""""""""""""""""""""""""""""""""""" "Cscope setting
If has ("cssag") set csprg =/usr/bin/cssag "specifies the command set csto = 1 used to execute cssag" first searches for the tags Tag file, search for the cssag database set cst "to use |: cstag | (: cs find g) instead of the default one: tag set nocsverb "does not show whether the database is successfully added" add any database in current directory if filereadable ("csable. out ") cs add cscope. out "add cscope database endif set csverb": whether the added database is successful or not. 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> """""""""""""""""""""""""""""""""" "where <C-@> g ctrl + @ key, then press g. The function is to view the definition of the symbol where the current cursor is located.
6. After reading the code to add the cssag symbol index database, vim can call the "cssag find" command for search. vim supports the query functions of eight CSBs. For example, if you want to find the function that calls the work () function in the code, you can enter ": cs find c work" in the vim command state and press Enter. You can also perform string search, which searches for the content 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. : Cs help (query under vim command)
S: Find the C language symbol, that is, find the location where the function name, Macro, enumeration value, and so on appear g: Find the location defined by the function, Macro, enumeration, similar to the function d provided by ctags: find the Function c called by this function: Find the function t that calls this function: Find the specified string e: Find the egrep mode, 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 file containing this file
7. view and read c ++ code [3] cscope only parse 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 skip the C ++ and Java suffix files during directory scanning. To parse C ++ or Java files in cssag, 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. The following Command finds all the Suffixes in the current directory and subdirectory ". h ",". c "," cc "and ". cpp 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.
$ Find. -name "*. h "-o-name "*. c "-o-name "*. cc "-o "*. cpp "> cscope. files $ cscope-bkq-I cscope. files $ ctags-R
8. Use the tags search symbol in vim
View ctags help $ man ctags: help ctags (vim command): help tags (vim command) run the ctags-R command in the source code root directory to generate a Tag file for the program source code, its "-R" option indicates recursive operations, and tag files are also generated for subdirectories. Vim uses the generated Tag file to perform corresponding searches and switch back and forth between C language elements of different files. $ Ctags-R
A) Use ": tag xxx" in vim to jump to function or data structure xxx. When using the tag command, you can use the TAB key to perform matching search and continue to switch down by pressing the TAB key. When a function has multiple definitions: The tag jumps to the first definition, and the current file: tnext jumps to the first: tfirst to the previous count: [count] tprevious jumps to the next count: [count] tnext to the last one: tlast. You can also select: tselect tagname among all tagnames. If you want to skip to the identifier containing the block ": tag/block "and then use the TAB key for selection. Here '/' indicates that vim 'block' is a statement block tag.
B) use the "ctrl +]" shortcut to jump to the definition of the Function Identifier where the cursor is located.
C) use "ctrl + t" to return to the upper layer. If you want to select an identifier starting with write _, tselect/^ write _ here, '^' indicates the start. Similarly, '$' indicates the end.
D. Run vim in the directory where the "tags" file is located. Otherwise, run the ": set tags = xxx" command to set the path of the "tags" file, in this way, vim can find the "tags" file (we have already set "set tags = tags;", which can also be used in sub-Directories ).
E) shortcut keys for moving the cursor in the function: [{go to the previous "{"}] In the first column to the next "{" {go to the previous blank line} in the first column to the current empty line gd definition of the local variable pointed by the cursor * to the next occurrence of the word pointed by the current cursor # To the last occurrence of the word pointed by the current cursor
9. the taglist plug-in uses [4]. This plug-in can display the macros, global variables, and functions in the current file in the Symbol window like Source Insight. Click the above tag with the mouse, to the position defined by the tag. You can sort the tag in alphabetical order, the class or scope to which the tag belongs, and the position where the tag appears in the file. If you switch to another file, the tag in this file is updated in the Symbol window. Taglist depends on ctags.
To use the taglist plug-in, you must meet the following requirements: 1 ). enable the VIM file type automatic detection function; 2 ). the Exuberant ctags tool is installed in the system, and the taglist tool can be found (because the taglist needs to be called to generate the tag file); 3 ). your VIM supports system () calls;
Install the taglist plugin # emerge-av app-vim/taglist
View help: help helptags: help taglist-intro
Open the tag window: TlistToggle
. Configure the following in vimrc """""""""""""""""""""""""""""""""""" "ctags settingset tags =. /tags ,. /.. /tags ,. /*/tags;
"Tag list (ctags)
Filetype on "automatic file type detection
If MySys () = "windows" "sets the location of the ctags program in windows. let Tlist_Ctags_Cmd = 'ctags 'elseif MySys () = "linux" ": Set the ctags program location in linux. let Tlist_Ctags_Cmd = '/usr/bin/ctags' endif
Let Tlist_Show_One_File = 1 "the tag of multiple files is not displayed at the same time, only the current file's let Tlist_Exit_OnlyWindow = 1" if the taglist window is the last window, exit vimlet Tlist_Use_Right_Window = 1 ". The taglist window map is displayed in the right window. <silent> <F8>: tlistToggle <cr> "Open the tags window by ing the F8 key """""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""
10. Create an index database and view the kernel source code [5] liuby @ liuby ~ /Git/kernel/linux-2.6.30.5 $ make cs1_gen cscopeliuby @ liuby ~ /Git/kernel/linux-2.6.30.5 $ make tags GEN tagsliuby @ liuby ~ /Git/kernel/linux-2.6.30.5 $ ls tags-l-rw-r -- 1 liuby 89648962 03-12 06:12 tagsliuby @ liuby ~ /Git/kernel/linux-2.6.30.5 $ ls cscope. *-l-rw-r -- 1 liuby 403901 03-12 cscope. files-rw-r -- 1 liuby 182562815 03-12 cscope. out-rw-r -- 1 liuby 23011328 03-12 cscope. out. in-rw-r -- 1 liuby 133571208 03-12 cscope. out. po
References: [1] http://vimcdoc.sourceforge.net/doc/if_cscop.html#2] #http://easwy.com/blog/archives/vim-cscope-ctags/%4] small http://www.kongove.cn/web/doc/vim-ctags-cscope-source.html
Thanks to Yishui blog [http://easwy.com/blog/]
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.