11

Is it possible to go to a class or function definition by search?

E.g. in Sublime it is possible to search the file for class and function names, and the viewport will automatically jump to the current closest match.

Right now if I want to find a specific function i use

/<function name>/

and press n or N accordingly, but I also find all the places it has been used in the code. Also, as this highlights the text as well I need to type :noh afterwards to remove it.

2 Answers 2

18

You don't necessarily have to install a plugin like ctags (although this would be reliable and allow many more great features, because you have semantic knowledge of your code, of course).

Vim has a built in function to go to the definition by doing a clever search: With the cursor over a keyword, press gd or gD. I recommend reading the help page on these, if you want to understand how they work exactly.

If you want to search for a function from anywhere in your code, I would recommend the tagbar plugin. It requires ctags, however. I use it in a very similar way: Toggle the bar with a shortcut which gives me an automatically created list of function names and other tags. Like any other buffer, I can search it and jump to the definition with enter.

5
  • The problem with this (and with ctags) is that I need to be inside the function definition for it to work. Ideally, I would like to be anywhere in my file and start searching for a function name by writing. It is pretty much how the plugin CtrlP works, except it does it by file names instead. Do you know of a solution?
    – pingul
    Mar 24, 2016 at 16:38
  • Now I understand what you mean, I immediately thought of my answer because of your 'goto definition' formulation in the question title. Mar 24, 2016 at 17:09
  • So in this case I would recommend the [tagbar plugin] (github.com/majutsushi/tagbar). I use it in a very similar way: Toggle the bar with a shortcut which gives me an automatically created list of function names and other tags. Like any other buffer, I can search it and jump to the definition with enter. Mar 24, 2016 at 17:13
  • Ah! I had the plugin before but didn't use it to navigate by search. It is a little cumbersome, but will do for now. Thanks! :) Would you like to edit your answer to add this option as well so I can accept it?
    – pingul
    Mar 24, 2016 at 17:26
  • 1
    Some tips to make the workflow less 'cumbersome': Create a simple mapping to toggle the tagbar and open the search afterwards. Use the autoclose setting of the plugin. I am sure you can tweak it to your needs. Mar 24, 2016 at 17:47
5

You will have to use a program like ctags to generate a tag file. It is recommended to use exuberant ctags.

Once you installed this program, go to the root directory of you project in a shell and generate a tagfile with:

$ctags

This will create a text file which contains the lines corresponding to the definition of each of your functions. Vim is able to parse this file.

Once this file is created you'll be able to use ctrl+] and ctrl+t to jumps between the tags. You can refer to this wiki article for more details about the tag navigation.

Also I would recommend using the taglist plugin which is pretty convenient to improve tag navigation.

1
  • Thank you for your answer! Im using ctags already but it doesn't really do what I want. I've added a comment in Philipp's answer :)
    – pingul
    Mar 24, 2016 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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