1<html> 2<head> 3<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8"> 4<title>Exuberant Ctags FAQ</title> 5</head> 6<body> 7 8<h1>Exuberant Ctags FAQ</h1> 9 10<ul> 11 <li><a href="#1"> 12 1. Why do you call it <strong><em>Exuberant Ctags</em></strong>? 13 </a> 14 <li><a href="#2"> 15 2. Why doesn't my editor work with these tag files? 16 </a> 17 <li><a href="#3"> 18 3. What are these strange bits of text beginning with <code>;"</code>? 19 </a> 20 <li><a href="#4"> 21 4. Why doesn't XEmacs' Speedbar module work with <strong><em>Exuberant 22 Ctags</em></strong>? 23 </a> 24 <li><a href="#5"> 25 5. Why doesn't XEmacs correctly locate the tag in the source file? 26 </a> 27 <li><a href="#6"> 28 6. Why doesn't NEdit correctly locate the tag in the source file? 29 </a> 30 <li><a href="#7"> 31 7. Why can't I jump to <code>class::member</code>? 32 </a> 33 <li><a href="#8"> 34 8. How can I avoid having to specify my favorite option every time? 35 </a> 36 <li><a href="#9"> 37 9. Why do I end up on the wrong line when I jump to a tag? 38 </a> 39 <li><a href="#10"> 40 10. How do I jump to the tag I want instead of the wrong one by the same name? 41 </a> 42 <li><a href="#11"> 43 11. What is <strong>Vim</strong>? 44 </a> 45 <li><a href="#12"> 46 12. How can I locate all references to a specific function or variable? 47 </a> 48 <li><a href="#13"> 49 13. Why does appending tags to a tag file tag so long? 50 </a> 51 <li><a href="#14"> 52 14. How do I get regex support for Win32? 53 </a> 54 <li><a href="#15"> 55 15. How should I set up tag files for a multi-level directory hierarchy? 56 </a> 57</ul> 58 59<hr> 60<h2> 61<a name="1"> 621. Why do you call it <strong><em>Exuberant Ctags</em></strong>? 63</a> 64</h2> 65 66Because one of the meanings of the word <em>exuberant</em> is: 67<ul> 68<B>ex·u·ber·ant</B> : produced in extreme abundance 69: <FONT SIZE=-1>PLENTIFUL</FONT> 70<B><I>synonym</I></B> see <FONT SIZE=-1>PROFUSE</FONT> 71</ul> 72 73Compare the tag file produced by <strong><em>Exuberant Ctags</em></strong> 74with that produced by any other ctags and you will see how appropriate the 75name is. 76 77<hr> 78<h2> 79<a name="2"> 802. Why doesn't my editor work with these tag files? 81</a> 82</h2> 83 84<h2> 85<a name="3"> 863. What are these strange bits of text beginning with <code>;"</code> which 87follow many of the lines in the tag file? 88</a> 89</h2> 90 91These are "extension flags". They are added in order to provide extra 92information about the tag that may be utilized by the editor in order to 93more intelligently handle tags. They are appended to the EX command part of 94the tag line in a manner that provides backwards compatibility with existing 95implementations of the Vi editor. The semicolon is an EX command separator 96and the double quote begins an EX comment. Thus, the extension flags appear 97as an EX comment and should be ignored by the editor when it processes the 98EX command. 99<p> 100Some non-vi editors, however, implement only the bare minimum of EX commands 101in order to process the search command or line number in the third field of 102the tag file. If you encounter this problem, use the option 103<code>--format=1</code> to generate a tag file without these extensions 104(remember that you can set the <code>CTAGS</code> environment variable to any 105default arguments you wish to supply). Then ask the supplier of your editor to 106implement handling of this feature of EX commands. 107<p> 108 109<hr> 110<h2> 111<a name="4"> 1124. Why doesn't XEmacs' Speedbar module work with <strong><em>Exuberant 113Ctags</em></strong>? 114</a> 115</h2> 116 117The default command line switches used by XEmacs for <i>etags</i> are not 118compatible with <strong><em>Exuberant Ctags</em></strong> options. By default, 119<strong><em>Exuberant Ctags</em></strong> installs a symbolic link, "etags", 120pointing to the ctags executable. When <strong><em>Exuberant 121Ctags</em></strong> is started with the name "etags", it produces Emacs-style 122tag files by default. 123<p> 124To fix this, add the following lines to your .emacs file, replacing the path 125to <i>etags</i> with the path where the symbolic link was installed. 126<p> 127 128<code> 129<pre> 130(autoload 'speedbar "speedbar") 131(setq speedbar-fetch-etags-command "/usr/local/bin/etags" 132 speedbar-fetch-etags-arguments '("-f" "-")) 133</pre> 134</code> 135 136<hr> 137<h2> 138<a name="5"> 1395. Why doesn't XEmacs correctly locate the tag in the source file? 140</a> 141</h2> 142 143This has been observed with version 20.3. It seems that when XEmacs searches 144for a tag, it searches using the tag name instead of the search string located 145in the TAGS file. This is a bug in XEmacs and does not occur in the GNU 146version of Emacs. This has been reported to be corrected as of version 21.4.16. 147 148<hr> 149<h2> 150<a name="6"> 1516. Why doesn't NEdit correctly locate the tag in the source file? 152</a> 153</h2> 154 155Versions of <a href="http://nedit.org">NEdit</a> prior to 5.1 did not support 156the extended tag file format generated by <strong><em>Exuberant 157Ctags</em></strong> by default. Either upgrade to version 5.1 or specify the 158option <code>--format=1</code> when running ctags to output the old tag file 159format. 160<p> 161 162<hr> 163<h2> 164<a name="7"> 1657. Why can't I jump to <code>class::member</code>? 166</a> 167</h2> 168 169Because, by default, ctags only generates tags for the separate identifiers 170found in the source files. If you specify the <code>--extra=+q</code> 171option, then ctags will also generate a second, class-qualified tag for each 172class member (data and function/method) in the form <code>class::member</code> 173for C++, and in the form <code>class.method</code> for Eiffel and Java. 174<p> 175 176<hr> 177<h2> 178<a name="8"> 1798. How can I avoid having to specify my favorite option every time? 180</a> 181</h2> 182 183Either by setting the environment variable CTAGS</code> to your custom 184options, or putting them into a <code>.ctags</code> file in your home 185directory. 186<p> 187 188<hr> 189<h2> 190<a name="9"> 1919. Why do I end up on the wrong line when I jump to a tag? 192</a> 193</h2> 194 195By default, ctags encodes the line number in the file where macro (#define) 196tags are found. This was done to remain compatible with the original UNIX 197version of ctags. If you change the file containing the tag without 198rebuilding the tag file, the location of tag in the tag file may no longer 199match the current location. 200<p> 201 202In order to avoid this problem, you can specify the option 203<code>--excmd=p</code>, which causes ctags to use a search pattern to locate 204macro tags. I have never uncovered the reason why the original UNIX ctags used 205line numbers exclusively for macro tags, but have so far resisted changing the 206default behaviour of <strong><em>Exuberant Ctags</em></strong> to behave 207differently. 208<p> 209 210<hr> 211<h2> 212<a name="10"> 21310. How do I jump to the tag I want instead of the wrong one by the same name? 214</a> 215</h2> 216 217A tag file is simple a list of tag names and where to find them. If there 218are duplicate entries, you often end up going to the wrong one because the 219tag file is sorted and your editor locates the first one in the tag file. 220<p> 221 222Standard Vi provides no facilities to alter this behavior. However, Vim 223has some nice features to minimize this problem, primarly by examining all 224matches and choosing the best one under the circumstances. Vim also provides 225commands which allow for selection of the desired matching tag. 226<p> 227 228<hr> 229<h2> 230<a name="11"> 23111. What is <strong>Vim</strong>? 232</a> 233</h2> 234 235Vim is a vi-compatible editor available as source and compilable for any 236platform. Yeah, I know the first reaction is to shy away from this. But you 237will never regret getting it, and you will become greatly attached to its 238features, which you can learn gradually. I would be willing to say that it 239is the best vi-clone available within 4 light-years of Alpha Centauri. It 240works (nearly) exactly like standard vi, but provides some incredibly useful 241extensions (some of which I have participated in designing with the author). 242Most Linux distributions have adopted Vim as its standard vi. 243<p> 244 245<hr> 246<h2> 247<a name="12"> 24812. How can I locate all references to a specific function or variable? 249</a> 250</h2> 251 252There are several packages already available which provide this capability. 253As of this writing, they are: 254 255<ul> 256<li><a href="http://www.gnu.org/software/global">GLOBAL source code tag system</a> 257<li><a href="http://www.gnu.org/software/idutils/idutils.html">GNU id-utils</a> 258<li><a href="http://cscope.sourceforge.net">cscope</a> 259<li><a href="ftp://www.ibiblio.org/pub/Linux/devel/lang/c">cflow</a> 260</ul> 261 262<hr> 263<h2> 264<a name="13"> 26513. Why does appending tags to a tag file tag so long? 266</a> 267</h2> 268 269Sometimes, in an attempt to build a global tag file for all source files in 270a large source tree of many directories, someone will make an attempt to run 271ctags in append (-a) mode on every directory in the hierarchy. Each time 272ctags is invoked, its default behavior is to sort the tag file once the tags 273for that pass have been added. As the cumulative tag file grows, the sort 274time increases exponentially. 275<p> 276 277The best way to avoid this problem (and the most efficient) is to make 278use of the --recurse (or -R) option of ctags by executing the following 279command in the root of the directory hierarchy (thus running ctags only once): 280<p> 281 282<code> 283<pre> 284 ctags -R 285</pre> 286</code> 287 288If you really insist on running ctags separately on each directory, you can 289avoid the sort pass each time by specifying the option <code>--sort=no</code>. 290Once the tag file is completely built, use the sort command to manually sort 291the final tag file, or let the final invocation of ctags sort the file. 292<p> 293 294<hr> 295<h2> 296<a name="14"> 29714. How do I get regex support for Win32? 298</a> 299</h2> 300 301You need to download the GNU regex package for Win32 from the following 302location: 303 304<blockquote> 305 <a href="http://people.delphiforums.com/gjc/gnu_regex.html"> 306 http://people.delphiforums.com/gjc/gnu_regex.html</a> 307</blockquote> 308 309Then point the makefile macro, REGEX_DIR, found in mk_mvc.mak and mk_bc5.mak, 310to the directory created by extracting this archive. 311 312<hr> 313<h2> 314<a name="15"> 31515. How should I set up tag files for a multi-level directory hierarchy? 316</a> 317</h2> 318 319There are a few ways of approaching this: 320<p> 321 322<ol> 323<li>A local tag file in each directory containing only the tags for source 324 files in that directory. 325 326<li>One single big, global tag file present in the root directory of your 327 hierarchy, containing all tags present in all source files in the 328 hierarchy. 329 330<li>A local tag file in each directory containing only the tags for source 331 files in that directory, in addition to one single global tag file 332 present in the root directory of your hierarchy, containing all 333 non-static tags present in all source files in the hierarchy. 334 335<li>A local tag file in each directory of the hierarchy, each one 336 containing all tags present in source files in that directory and all 337 non-static tags in every directory below it (note that this implies 338 also having one big tag file in the root directory of the hierarchy). 339</ol> 340 341Each of these approaches has its own set of advantages disadvantages, 342depending upon your particular conditions. Which approach is deemed best 343depends upon the following factors: 344<p> 345 346<ol type=A> 347<li>The ability of your editor to use multiple tag files. 348 <p> 349 350 If your editor cannot make use of multiple tag files (original vi 351 implementations could not), then one large tag file is the only way to 352 go if you ever desire to jump to tags located in other directories. If 353 you never need to jump to tags in another directory (i.e. the source 354 in each directory is entirely self-contained), then a local tag file 355 in each directory will fit your needs. 356 <p> 357 358<li>The time is takes for your editor to look up a tag in the tag file. 359 <p> 360 361 The significance of this factor depends upon the size of your source 362 tree and on whether the source files are located on a local or remote 363 file system. For source and tag files located on a local file system, 364 looking up a tag is not as big a hit as one might first imagine, since 365 vi implementations typically perform a binary search on a sorted tag 366 file. This may or may not be true for the editor you use. For files 367 located on a remote file system, reading a large file is an expensive 368 operation. 369 <p> 370 371<li>Whether or not you expect the source code to change and the time it 372 takes to rebuild a tag file to account for changes to the source code. 373 <p> 374 375 While <strong><em>Exuberant Ctags</em></strong> is particularly fast in 376 scanning source code (around 1-2 MB/sec), a large project may still result 377 in objectionable delays if one wishes to keep their tag file(s) up to date 378 on a frequent basis, or if the files are located on a remote file system. 379 <p> 380 381<li>The presence of duplicate tags in the source code and the ability to 382 handle them. The impact of this factor is influenced by the following 383 three issues: 384 <p> 385 386 1. How common are duplicate tags in your project? 387 <p> 388 389 2. Does your editor provide any facilities for dealing with duplicate 390 tags? 391 <p> 392 393 While standard vi does not, many modern vi implementations, such 394 as Vim have good facilities for selecting the desired match from 395 the list of duplicates. If your editor does not support duplicate 396 tags, then it will typically send you to only one of them, whether 397 or not that is the one you wanted (and not even notifying you that 398 there are other potential matches). 399 <p> 400 401 3. What is the significance of duplicate tags? 402 <p> 403 404 For example, if you have two tags of the same name from entirely 405 isolated software components, jumping first to the match found 406 in component B while working in component A may be entirely 407 misleading, distracting or inconvenient (to keep having to choose 408 which one if your editor provides you with a list of matches). 409 However, if you have two tags of the same name for parallel builds 410 (say two initialization routines for different hosts), you may 411 always want to specify which one you want. 412</ol> 413 414Of the approaches listed above, I tend to favor Approach 3. My editor of 415choice is Vim, which provides a rich set of features for handling multiple 416tag files, which partly influences my choice. If you are working with 417source files on a remote file system, then I would recommend either 418Approach 3 or Approach 4, depending upon the hit when reading the global 419tag file. 420<p> 421 422The advantages of Approach 3 are many (assuming that your editor has 423the ability to support both multiple tag files and duplicate tags). All 424lookups of tag located in the currect directory are fast and the local 425tag file can be quickly and easily regenerated in one second or less 426(I have even mapped a keystroke to do this easily). A lookup of a 427(necessarily non-static) tag found in another directory fails a lookup in 428the local tag file, but is found in the global tag file, which satisfies 429all cross-directory lookups. The global tag file can be automatically 430regenerated periodically with a cron job (and perhaps the local tag files 431also). 432<p> 433 434Now I give an example of how you would implement Approach 3. Means of 435implementing the other approaches can be performed in a similar manner. 436<p> 437 438Here is a visual representation of an example directory hierarchy: 439<p> 440 441<code> 442<pre> 443project 444 `-----misccomp 445 | `... 446 `-----sysint 447 `-----client 448 | `-----hdrs 449 | `-----lib 450 | `-----src 451 | `-----test 452 `-----common 453 | `-----hdrs 454 | `-----lib 455 | `-----src 456 | `-----test 457 `-----server 458 `-----hdrs 459 `-----lib 460 `-----src 461 `-----test 462</pre> 463</code> 464 465Here is a recommended solution (conceptually) to build the tag files: 466<p> 467 468<ol> 469<li>Within each of the leaf nodes (i.e. hdrs, lib, src, test) build a tag 470 file using "ctags *.[ch]". This can be easily be done for the whole 471 hierarchy by making a shell script, call it "dirtags", containing the 472 following lines: 473 474<code> 475<pre> 476 #!/bin/sh 477 cd $1 478 ctags * 479</pre> 480</code> 481 482 Now execute the following command: 483 484<code> 485<pre> 486 find * -type d -exec dirtags {} \; 487</pre> 488</code> 489 490 These tag files are trivial (and extremely quick) to rebuild while 491 making changes within a directory. The following Vim key mapping is 492 quite useful to rebuild the tag file in the directory of the current 493 source file: 494 495<code> 496<pre> 497 :nmap ,t :!(cd %:p:h;ctags *.[ch])&<CR><CR> 498</pre> 499</code> 500 501<li>Build the global tag file: 502 503<code> 504<pre> 505 cd ~/project 506 ctags --file-scope=no -R 507</pre> 508</code> 509 510 thus constructing a tag file containing only non-static tags for all 511 source files in all descendent directories. 512 513<li>Configure your editor to read the local tag file first, then consult 514 the global tag file when not found in the local tag file. In Vim, 515 this is done as follows: 516 517 :set tags=./tags,tags,~/project/tags 518 519</ol> 520 521If you wish to implement Approach 4, you would need to replace the 522"dirtags" script of step 1 with the following: 523<p> 524 525<code> 526<pre> 527 #!/bin/sh 528 cd $1 529 ctags * 530 # Now append the non-static tags from descendent directories 531 find * -type d -prune -print | ctags -aR --file-scope=no -L- 532</pre> 533</code> 534 535And replace the configuration of step 3 with this: 536<p> 537 538<code> 539<pre> 540 :set tags=./tags,./../tags,./../../tags,./../../../tags,tags 541</pre> 542</code> 543 544As a caveat, it should be noted that step 2 builds a global tag file whose 545file names will be relative to the directory in which the global tag file 546is being built. This takes advantage of the Vim 'tagrelative' option, 547which causes the path to be interpreted a relative to the location of the 548tag file instead of the current directory. For standard vi, which always 549interprets the paths as relative to the current directory, we need to 550build the global tag file with absolute path names. This can be 551accomplished by replacing step 2 with the following: 552<p> 553 554<code> 555<pre> 556 cd ~/project 557 ctags --file-scope=no -R `pwd` 558</pre> 559</code> 560 561<p> 562<hr> 563<a href="http:index.html">Back to <strong>Exuberant Ctags</strong></a> 564 565</body> 566</html> 567