Find the function declaration with ctag in vi

Source: Internet
Author: User
Vi combines ctag to find the function declaration-general Linux technology-Linux programming and kernel information. The following is a detailed description. I used to do windows development. Writing code below windows mostly relies on powerful integrated development environment IDE, such as Microsoft Visual studio series, IBM Eclipse, Borland JBuilder, and Delphi. Now, when I suddenly switched to Linux, I feel uncomfortable using vi, for example, *. c file introduces a lot. h file, in this *. in the c file, a function (param1, param2) used does not know which file declares it. In this case, on the windows platform, any IDE can easily jump to the function prototype, but it is not that convenient in Linux, at this time, although grep can also be used, it is still a little annoying.

Ask colleagues who use Ctag to download exuberant-ctags from the Internet. After installation, create a tags file in the source code root directory. The command is as follows: Ctag-R
In this case, a file named tags is created in the current directory.
Second. use vi to open *. in the c file, if you want to see where the function (param1, param2) references or where it is declared, move the cursor to the number of functions described above, enter the tag function (or press CTL +}) in the command line status of vi, and vi will jump to the * Where the first function is located *. c file or *. h file.
Third, if you want to see how many files reference the function, you can enter ts in the vi command character state to see how many places the function is used.
4. Press the CTL + T key to return to the original location.

Vi + indent + ctag edit c

Vim works with ctag. indent is a good method for editing and viewing the C language source program.
Ctag completes cross-element association between files.
Ctag-R *
: Ta identifier
Ctrl +]
Ctrl + o
Ctrl + t
Vi-t identifier
Indent format the source code.
: %! Indent



Let's talk about using ctags and csags to read code on fedora.

The vim version I installed on fedora6 is 7.0 with support for ctags-5.6 and cscope-15.5. I chose a work machine to use linux fedora6. The system's built-in word and PDF file software are available, and the mail sending and receiving software is also available, which is very convenient and easy to use, that is, you need to configure the software you want to check the code for yourself. However, after configuration, the speed and usage habits are better than those of sourceinsight. Based on the experience of some people on the Internet, I have put it into practice. writing this document may help people who want to know about ctags and CSWs.

Many programmers prefer to use the vi editor on linux, and some prefer to use emacs. However, I personally prefer vi, but I do not like the editor on windows when I use vi, in fact, the reason is very simple: using the keyboard to complete the search and replacement is more accurate and faster than using the mouse, while vi uses the keyboard operation to implement the function. Currently, vim is an editor similar to vi. It is an open-source project with more functions, and ctags and csags are its plug-ins. The following describes how to configure and use ctags and csags to view code for vim.

1. Download and install

Taglist is a vim plug-in used to display the global variables and function names of codes in a window on the left of the screen.

: Http://www.vim.org/scripts/script.php? Script_id = 1, 273

What I download is taglist-43.zip.

The installation method is to expand the package under/usr/share/vim/vim70.


2. This process is also called training.

I need to check the three parts of the Code are the Linux kernel, and the other two parts are the library and Application Layer Code.

The Linux kernel code is compiled out of the database for the parts I need, and the irrelevant parts are not sorted out.

First build path:

Mkdir $ HOME/readcode

Mkdir $ HOME/readcode/cs1_kernel

Mkdir $ HOME/readcode/cs1_- app

Path:

Cd readcode

The file name that creates a cssag database is a create-kernel.sh

LNX = $ HOME/build/linux-2.4.22

Find $ LNX \

-Path "$ LNX/arch /*"! -Path "$ LNX/arch/arm *"-prune-o \

-Path "$ LNX/include/asm -*"! -Path "$ LNX/include/asm-arm *"-prune-o \

-Path "$ LNX/tmp *"-prune-o \

-Path "$ LNX/Documentation *"-prune-o \

-Path "$ LNX/scripts *"-prune-o \

-Name "*. [chxsS]"-print> $ HOME/readcode/cs1_kernel/cs1_. files

Cd $ HOME/readcode/csw.kernel

Cscope-B-q-k

Cd $ HOME/readcode

Execute the source create-kernel.sh

The csproducer. out file is generated under the $ HOME/readcode/csw.kernel directory.

Databases that generate CSWs for application-layer code are much simpler.

Its generation script:

APP = $ HOME/build/App

Find $ APP-name "*. cpp"-print> $ HOME/readcode/cscope-app/cscope. files

Find $ APP-name "*. [ch]"-print> $ HOME/readcode/cs1_- app/cs1_. files

Cd $ HOME/readcode/cscope-app

Cscope-B-q-k

Cd $ HOME/readcode

Some application code is c ++, so code in. cpp format is listed to generate a cssag database.


3. Configure the. vimrc File

The vimrc content is as follows:

Set cst

Set csto = 1

Set cscopequickfix = s-, c-, d-, I-, t-, e-, f-

Cs add/home/yangxl/readcode/cscope-kernel/csnel. out

Cs add/home/yangxl/readcode/cscope-app/cscope. out

Let Tlist_Enable_Fold_Column = 0

Let Tlist_WinWidth = 30

Let Tlist_Show_One_File = 1

Set tabstop = 4

Nmap : Cs find d = Expand (" ")

Nmap : Cs find c = Expand (" ")

Nmap : Cs find t = Expand (" ")

Nmap : Cs find e = Expand (" ")

Nmap : Cs find f = Expand (" ")

Nmap : Cs find I = Expand (" ")

Nnoremap : TlistToggle

The bottom lines define shortcuts for the F2-F9 feature

F9 is the shortcut to open and close the taglist window on the left

Its syntax is explained in vi by typing

: Help cscope

Learning and understanding


4. Usage

Use vi to open the training code and press ctrl-] on the method to be called. Is the prototype code of this method displayed? Press F9 to open the Left window, display the method name list, macro definition, and global variables, and then press F9 to disappear from the Left window.

Show loaded Databases

: Cs show

We can see that two csexample. out files are loaded.

: Cs find 2 method name or: cs find d method name corresponds to the shortcut key F2

Locate the name of the method called by this method

: Cs find 3 method name or: cs find c method name corresponding to the shortcut key F3

Find the method name to call this method

: Cs find 4 string name or: cs find t string name corresponding to the shortcut key F4

Find the string that appears

: Cs find 7 file name or: cs find f file name corresponding shortcut key F7

Find File

: Cs find 8 file name or: cs find I file name corresponding shortcut key F8

Find the file that includes this file

: TlistToggle corresponds to the shortcut key F9

5. Not done

Consider how to synchronize the cssag library when the code is constantly being written?
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.