12

After generating a tags file with exuberant-ctags I can jump from a function call to its declaration with <C-]> which is pretty convenient.

My question is how to do it in the other way? When my cursor is on the definition of a function, how can I go to the lines where the function is called?

When I look to the file generated by ctags I only see informations related to the location of the definition and not of the calls, does it means that I can't do that, that Vim as a clever workaround or that I need to use something else than exuberant-ctags?

11

You can't jump to function calls with ctags. For C/C++ and Java there is cscope, and it integrates nicely with Vim (see :help cscope). For other languages you might be able to find a tool that generates / exports cscope-compatible databases (f.i. hscope for Haskell, and starscope for Ruby, Go, and JavaScript). You can also use cscope with GNU global databases, by pointing cscopeprg to gtags-cscope.

On a side note: if you aren't put off by occasionally puzzling use of Engrish, ;) you probably also want to use universal-ctags instead of exuberant-ctags. The latter hasn't been updated in years. The former is an actively maintained fork.

  • Does cscope support other languages than C/C++ and Java ? – edi9999 Oct 2 '15 at 10:42
  • @edi9999 cscope itself supports C/C++, Lex, and Yacc. The support for Java is marginally useful. You can get support for (old-style) PHP and JavaScript from GNU global, via the gtags-cscope utility. And, as I said above, there are other indexers that can export cscope-compatible databases. – Sato Katsura Oct 2 '15 at 16:36
8

You can't do that with ctags.

Try a beefier alternative like cscope (which even has its own help section: :help cscope) or global.

  • Does cscope support other languages than C/C++ and Java ? – edi9999 Oct 2 '15 at 10:42
  • Yes, at least PHP and JavaScript. – romainl Oct 2 '15 at 11:20
  • 1
    @romainl Nope, cscope itself supports neither PHP, nor JavaScript. You can however index PHP and JavaScript files with GNU global, then run gtags-cscope to use the resulting gtags file with cscope. – Sato Katsura Oct 2 '15 at 16:40
  • @SatoKatsura, yes, cscope supports JavaScript (and CSS, appparently). – romainl Oct 2 '15 at 18:43
  • 2
    @romainl You should consider looking at cscope's sources instead of speculating based on quick experiments. cscope has a rather relaxed C parser. That's all it has, and all it does. With that parser it "supports" a lot of languages, for suitable values of "support". Constructs are recognized as long as they look more or less like C. Which is why f.i. Java classes and newer PHP constructs are not recognized. Compare f.i. with starscope, which actually allows adding new languages. – Sato Katsura Oct 3 '15 at 20:36
7

You could use Vim's grep command to search all the files in your project:

:grep! "\<<cword>\>" . -r
:copen

Let's put that in a keybind:

nnoremap <F4> :grep! "\<<cword>\>" . -r<CR>:copen<CR>

I find this one-key project-wide search invaluable when I am exploring a large unfamiliar codebase.

\< and \> are regexp sequences which indicate the start and end of a word, so you won't get partial matches. (The -w option to grep might be another way to achieve this.)

However, this is an unintelligent search, so it can produce false-positives if the same word appears in unrelated contexts.


Truthfully, I use my own modified version of the grep.vim plugin instead. This is slightly friendlier to use:

  • It lets you edit the search pattern, and also the grep commandline (so you can target specific folders like src/ lib/ instead of .) and remembers the changes.
  • I added support for Google's csearch. This can be much faster than grep because it builds an index of words rather than scanning each file every time. Notably, csearch uses a slightly different regexp standard, so \b must be used instead of \< and \>.

I configure it and exclude some standard files like this.

For fast access, I create two keybinds. F3 lets me edit the commandline, F4 uses the previously configured commandline without any extra keystrokes. The mechanism is pretty ugly, and could use some cleanup, but it has served me well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.