问题
Right now I am working on a file which uses many classes, methods, functions, variables, etc. Is it possible to go to the declaration of all of them? Please, take into account that some of those declarations are in the same file but others are in other files (which may not be opened and you do not know where the declarations are but they do exist). What would happen if the declaration is one level up in the directory? and what about if it is one level down?
Is this done in a different way according to the programming language we are talking about or the procedure to find the declarations is the same regardless of the language?
I have been reading and it seems the solution is related to tags. However, I would like to know how this can be achieved (step by step), especially taking into account that we are talking, in some cases, of definitions in other files.
I know this can be done with IDEs but I would like to know how much different this can be achieved with vim.
I only have a fresh install of Vim. I have not installed any plugins yet but willing to do if it is necessary. Perhaps this can be done without and with plugins.
Thanks in advance!
回答1:
Setting up tags is not so difficult, though (as most things in Vim) it's not as automatic compared to IDEs.
- First, you need the
ctags
tool. The most common today is Exuberant Ctags, found at ctags.sourceforge.net. - Next, you need to create a tags database (a file names
tags
) for all the files in your project(s). This is usually done by runningctags -R .
from your project root (also from within Vim via:!ctags ...
). Exuberant Ctags support 41 languages, and you can even extend it via regular expressions. - Finally, Vim needs to be configured to pick up the tags database. With
:set tags=./tags;
, it will search in the file's directory upwards to the root directory. If you have certain global include directories, you can add those. - With that, you can start using Vim's tag functionality like
<C-]>
and:tag
.
You need to periodically update the tags database; there are plugins (like easytags.vim) that can do that automatically for you.
回答2:
You can try gd
, it goes to local declaration, for more powerful 'go to definition', you might want to try tags
as Ingo suggested.
来源:https://stackoverflow.com/questions/19934060/vim-how-to-go-to-the-declaration-of-a-class-method-function-variable-etc