tags:

views:

1251

answers:

5

I'm having the problem that ctags takes me to a forward declaration allot of times instead of to the actual definition of the function.

Any way to get around that?

+3  A: 

You should be able to use tn and tp to jump to the various matching tags.

  • Press ^] to take you to the first match.
  • If that's not the match you want, type :tn to go to the next.
  • If you typed :tn too many times you can type :tp to return to the previous one.
Nathan Fellman
+1  A: 
:tselect my_little_function

and you would get a list of matches. or if you jump to a tag and you are not happy with it, then type

:tselect

And you get a list of alternative for the last active tag.

Johan
+3  A: 

I believe Vim goes to the first tag in the tags file by default. You can select a different one if you prefer: use :tj (similar to :tselect, but auto-jump if there's only one match) or Ctrl-] followed by :tn).

The only way of changing the default is to change the order of the tags file, but I don't believe ctags offers a command-line option to do this.

This isn't as hard as it sounds as you basically need a script that opens the tags file, sorts it by the 'kind' of tag and writes it back out again. The 'kind' in the tag is a single character describing whether it's a function (f), a function prototype (p), a macro, a enumerated name etc etc etc. If you're using Linux, it could, in theory, be as simple as:

#!/bin/sh
ctags -R -f - . | tac > tags

Since tac reverses the order of lines in a file, this will automatically put the definition first. However, it gets a bit more complicated as the header needs to be maintained and Vim prefers the tag file to be sorted, so it's better to go through the file and sort on the first entry (the tag name) in forward order and then the kind in reverse order. Therefore, something more complicated may be better.

I apologise for the shameless plug, but I have written a Vim plugin that (indirectly) does what you need. It is intended for adding lots of extra highlighting groups for things like function names, macros, enums etc. However, one of the other things that this does is re-sort the tag file so that the function implementation comes before the function declaration, thereby achieving what you want (I had the same need as you). If you don't want any of the highlighting functionality, you could probably strip it all out quite easily: it's a fairly simple python program and an even simpler Vim script and is available from my site.

Al
+3  A: 

I'm a little late with this answer, but I think that the easiest way is to use "g ctrl-]" instead of just "ctrl-]". If there is only one match, it will take you there. If there are multiple matches, it will list them all, letting you choose the one you want, just like :tselect. The best of both worlds. :)

Derek
Looks promising, I'll check it out - thanks.
Robert S. Barnes
For me, this just gives me two forward declarations (in different files) of functions that share the name.But it doesn't show the implementation of the function.
frankster
A: 

This option worked for me better

Put the following line in your .vimrc and now you can use double click of the mouse (on the variable/entry in your file) to jump to the tagged location. If single match is found, it will jump right away. If multiple entries are matches, it will prompt for user input..

:map <2-LeftMouse> g< c-]>

-Saravanan

Saravanan Veeraraghavan