Vim: How to go to the declaration (of a class, method, function, variable, etc)

前端 未结 3 1081
不思量自难忘°
不思量自难忘° 2021-01-31 08:33

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 t

相关标签:
3条回答
  • 2021-01-31 09:14

    I use the you complete me (ycm) plugin for this, which is

    a fast, as-you-type, fuzzy-search code completion engine for Vim

    It works with a bunch of languages and is very powerful. Check this section for how to install it on your system.

    When e.g. using it with a Java Maven project, open the desired file from the folder that contains your pom.xml file and the plugin scans all your files and dependencies.

    I do have the following key mappings in my ~/.vimrc for convenient use of your requested feature:

    let g:ycm_key_list_select_completion = ['<C-n>', '<Down>']
    let g:ycm_key_list_previous_completion = ['<C-p>', '<Up>']
    let g:SuperTabDefaultCompletionType = '<C-n>'
    nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
    nnoremap <leader>ji :YcmCompleter GoToImplementation<CR>
    nnoremap <leader>jr :YcmCompleter GoToReferences<CR>
    

    Assuming your <leader> is mapped to , with let mapleader = "," as in my case the following commands work:

    • ,jd go to definition or declaration
    • ,ji go to implementation
    • ,jr go to references
    • Ctrl+n/Ctrl+p move down/up in a list that pops up for autocompletion
    0 讨论(0)
  • 2021-01-31 09:19

    Setting up tags is not so difficult, though (as most things in Vim) it's not as automatic compared to IDEs.

    1. First, you need the ctags tool. The most common today is Exuberant Ctags, found at ctags.sourceforge.net.
    2. 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 running ctags -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.
    3. 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.
    4. 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.

    0 讨论(0)
  • 2021-01-31 09:39

    You can try gd, it goes to local declaration, for more powerful 'go to definition', you might want to try tags as Ingo suggested.

    0 讨论(0)
提交回复
热议问题