Thank You, Ctags!

by chromatic

I've used Vim for most of a decade now, having completely failed to understand Emacs, then going through a vi tutorial on an HP-UX 9.x box and watching how fast text editing can be. I'm proud that the Unix command-line is my IDE. I have plenty of great tools like Perl, grep, find, ack. I'm quick and productive.

I've known about Ctags, and I used it once or twice, but never really took advantage of it until last week.

I've used vim -t tag to open my editor to a function's definition, but that seemed a little bit clunky. If I were in a Vim session already, I'd have to background the process, launch the new editor, and remember to switch back and forth between the two processes. (screen makes this easier, but it's less of a benefit for short-lived processes.)

Then I had a thought, and looked it up in a Vim reference to confirm. Ctrl-] on an identifier jumps to the definition of that symbol. Ctrl-t jumps back. This works without having to start a new Vim process.

Thirty seconds of research bumped ctags from something I use once in a while to an indispensible tool for programming and debugging. Thanks to everyone who's contributed both to ctags and to the Vim integration!


10 Comments

Miel Vanacker
2007-09-17 06:42:07
On my keyboard (French Mac layout), I form ] by pressing ALT+SHIFT+°. Typing CTRL+] should thus be CTRL+ALT+SHIFT+°, but this doesn't work in Vim. Can you use some other key combo for this function?


Thanks, Miel (miel.vanacker@defraine.net)

Adam
2007-09-17 08:58:52
ctlr-] and ctlr-shift-t will do it as a stack, which is even more useful.


Matthias
2007-09-18 01:38:42
@Miel: Try mapping the command to something you can access, ie. use something like
nmap
in your .vimrc.


@chromatic: Ten years of Vim and now you're discovering ? The whole help system navigation is built around tags, have you never ever looked at it?

Matthias
2007-09-18 01:41:37
Whoops, should have used Preview (HTML ate my angle brackets). It's


nmap <F6> <C-]>

IainT
2007-09-18 02:08:35
Don't forget insert mode CTRL-X CTRL-] - it does completion using the tags file. Not perfect, but helps me greatly.


:help ins-completion and
:help i_CTRL-X_CTRL-]


(Currently updating my .vimrc)

Ralph Corderoy
2007-09-22 04:56:48
That was a poor vi tutorial if tags didn't get covered.


To find if you know vi (not Vim) examine every key on the keyboard and recall its function in vi. If you find one that you think does nothing, look it up. There's very few that do nothing.


I assume you're aware of ^N and ^P in Vim's insert mode? And ^A in insert mode? And ^A and ^X in command mode? All very useful.

chromatic
2007-09-25 16:30:13
@Matthias, I've used Vim's help system extensively, but I didn't realize the connection between that and ctags. I always knew I was using only 10% of Vim's features at most, but it always surprises me when I learn a new trick.


@Ralph, I've never run into ^A or ^X before, but they look useful. Also the vi tutorial I used was a very basic one as part of an introduction to Unix in an HP-UX 9.x book in the '90s. I wouldn't necessarily recommend it for anyone these days!

meonkeys
2007-09-25 17:07:30
Yes, ctags rocks. And don't forget cscope! Both are nicely integrated into Vim.
sg
2007-09-26 11:55:45
I prefer id utils to ctags because the scope of ctags is restricted to declarations and definitions; but the scope of id utils is everything. Here's the setup for vim in Windows:


Run the following (single line) command to create the id ("tags") file:

c:\opt\djgpp\bin\mkid.exe *.c *.cc *.h *.hh -m c:\opt\djgpp\share\id-lang.map

Use it from the command line with commands such as:

c:\opt\djgpp\bin\lid -i -Rgrep -s a_key_word

Use from VIM is much nicer, and uses a trick to set an option to a dynamic value (Bram once showed this trick on the vim mailing list but for some other setting). Have the following (one line) in your vimrc:

command! SetGrepPrg exec 'set grepprg=c:\opt\djgpp\bin\lid\ -Rgrep\ -s\ -f\ '.expand('%:p:h').'\ID'

To use id-uitls on a project, use the SetGrepPrg to specify the id ("tags") file for the project. Then just grep for the keyword and use navigation commands such as :copen :cn, :cp etc.


Incidentally, the id-utils that works on Windows is from djgpp (the one from gnu win32 has bugs). It would be interesting to replace mkid.exe by by newer indexing programs such as from google.

sg
2007-09-26 11:58:47
PS: to see that id-utils has a wider scope than ctags, note that one can write a vim script that filters the output of id-utils to restrict it to declarations and definitions.