📄 ch30_28.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 30] 30.28 Keep Track of Functions and Included Files with ctags and tags</TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:45:48Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch30_01.htm"TITLE="30. vi Tips and Tricks"><LINKREL="prev"HREF="ch30_27.htm"TITLE="30.27 vi Compound Searches "><LINKREL="next"HREF="ch30_29.htm"TITLE="30.29 Setting Multiple tags Files "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch30_27.htm"TITLE="30.27 vi Compound Searches "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.27 vi Compound Searches "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 30<BR>vi Tips and Tricks</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch30_29.htm"TITLE="30.29 Setting Multiple tags Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.29 Setting Multiple tags Files "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1560">30.28 Keep Track of Functions and Included Files with ctags and tags</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-34124"></A><ACLASS="indexterm"NAME="AUTOID-34126"></A><ACLASS="indexterm"NAME="AUTOID-34129"></A><ACLASS="indexterm"NAME="AUTOID-34132"></A>The source code for a large C program will usually be spreadover several files. Sometimes, it is difficult to keep track ofwhich file contains which function definitions. To simplifymatters, a UNIX command called <EMCLASS="emphasis">ctags</EM> can be used togetherwith the <CODECLASS="literal">:tag</CODE> command of <EMCLASS="emphasis">vi</EM>.</P><PCLASS="para"><EMCLASS="emphasis">ctags</EM> creates an information file (a database)that <EMCLASS="emphasis">vi</EM> uses later to determine which files define whichfunctions. By default, this database file is called <EMCLASS="emphasis">tags</EM>.This file contains lines of the form:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="replaceable"><I>tag_ID file context</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">where <CODECLASS="replaceable"><I>tag_ID</I></CODE> is the name of the C function or macro,<CODECLASS="replaceable"><I>file</I></CODE> is the source file in which <CODECLASS="replaceable"><I>tag_ID</I></CODE> is defined,and <CODECLASS="replaceable"><I>context</I></CODE> is a search pattern that shows the line of codecontaining <CODECLASS="replaceable"><I>tag_ID</I></CODE>.</P><PCLASS="para">From within <EMCLASS="emphasis">vi</EM>, a command such as:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch30_26.htm"TITLE="30.26 Shell Escapes: Running One UNIX Command While Using Another">:!</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><CODECLASS="userinput"><B>:!ctags file.c</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">creates a file named <EMCLASS="emphasis">tags</EM> under your current directory.<EMCLASS="emphasis">tags</EM> is a database containing information on the functions defined in <EMCLASS="emphasis">file.c</EM>. A command like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:!ctags *.c</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">creates a <EMCLASS="emphasis">tags</EM> file describing all the C source files under thedirectory.[If you'll be using the tags file while you're in some other directory,be sure to use an absolute pathname, like this:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch09_16.htm"TITLE="9.16 Command Substitution ">`...`</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen"><CODECLASS="userinput"><B>:!ctags `pwd`/*.c</B></CODE></PRE></TD></TR></TABLE></P><PCLASS="para">That will store <SPANCLASS="link">absolute pathnames (<ACLASS="linkend"HREF="ch14_02.htm"TITLE="Using Relative and Absolute Pathnames ">14.2</A>)</SPAN>in the <EMCLASS="emphasis">tags</EM> file. <EMCLASS="emphasis">-JP</EM> ]</P><PCLASS="para">Now suppose your <EMCLASS="emphasis">tags</EM> file contains information on all thesource files that make up a C program. Also suppose that you wantto look at or edit a function in the program but do not know wherethe function is. From within <EMCLASS="emphasis">vi</EM>, the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:tag </B></CODE><CODECLASS="replaceable"><I>name</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">will look at the <EMCLASS="emphasis">tags</EM> file to find out which file contains thedefinition of the function <EMCLASS="emphasis">name</EM>.It will then read in the file and positionthe cursor on the line where the name is defined. In this way,you don't have to know which file you have to edit; you only haveto decide which function you want to edit.[My favorite <EMCLASS="emphasis">tags</EM> shortcut is to put the cursor on the first letter of afunction name in your buffer.Then press CTRL-<CODECLASS="literal">]</CODE> (Control-right square bracket).<EMCLASS="emphasis">vi</EM> will read the <EMCLASS="emphasis">tags</EM> file and open to the function name that wasunder your cursor.At least, my version of <EMCLASS="emphasis">vi</EM> will do that! <EMCLASS="emphasis">-JP</EM> ]</P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> If <EMCLASS="emphasis">tags</EM> isn't working, that may be because you have the <EMCLASS="emphasis">vi</EM>option <EMCLASS="emphasis">nowrapscan</EM> set.That's a problem on many versions of <EMCLASS="emphasis">vi</EM>.If typing the following command fixes <EMCLASS="emphasis">tags</EM> for you:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"><CODECLASS="userinput"><B>:set wrapscan</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">then add that command to your<SPANCLASS="link"><EMCLASS="emphasis">.exrc</EM> file (<ACLASS="linkend"HREF="ch04_09.htm"TITLE="Setting Up vi with the .exrc File ">4.9</A>)</SPAN>or<SPANCLASS="link"><EMCLASS="emphasis">EXINIT</EM> variable (<ACLASS="linkend"HREF="ch06_03.htm"TITLE="Predefined Environment Variables ">6.3</A>)</SPAN>.(Thanks to Chris Torek for this tip.)</P></BLOCKQUOTE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JS</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">Learning the vi Editor</CITE>, Chapter 7</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch30_27.htm"TITLE="30.27 vi Compound Searches "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 30.27 vi Compound Searches "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch30_29.htm"TITLE="30.29 Setting Multiple tags Files "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 30.29 Setting Multiple tags Files "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">30.27 vi Compound Searches </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">30.29 Setting Multiple tags Files </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -