Jump to function definition in vim

后端 未结 11 2437
独厮守ぢ
独厮守ぢ 2020-11-27 08:53

How can I jump to to a function definition using vim? For example with Visual Assist, I can type Alt+g under a function and it opens a context menu lis

相关标签:
11条回答
  • 2020-11-27 09:44

    To second Paul's response: yes, ctags (especially exuberant-ctags (http://ctags.sourceforge.net/)) is great. I have also added this to my vimrc, so I can use one tags file for an entire project:

    set tags=tags;/
    
    0 讨论(0)
  • 2020-11-27 09:44

    Install cscope. It works very much like ctags but more powerful. To go to definition, instead of Ctrl + ], do Ctrl + \ + g. Of course you may use both concurrently. But with a big project (say Linux kernel), cscope is miles ahead.

    0 讨论(0)
  • 2020-11-27 09:46

    TL;DR:

    Modern way is to use COC for intellisense-like completion and one or more language servers (LS) for jump-to-definition (and way way more). For even more functionality (but it's not needed for jump-to-definition) you can install one or more debuggers and get a full blown IDE experience.

    Quick-start:

    1. install vim-plug to manage your VIM plug-ins
    2. add COC and (optionally) Vimspector at the top of ~/.vimrc:
      call plug#begin()
      Plug 'neoclide/coc.nvim', {'branch': 'release'}
      Plug 'puremourning/vimspector'
      call plug#end()
      
    3. call :source $MYVIMRC | PlugInstall to reload VIM config and download plug-ins
    4. restart vim and call :CocInstall coc-marketplace to get easy access to COC extensions
    5. call :CocList marketplace and search for language servers, e.g.:
    • type python to find coc-jedi,
    • type php to find coc-phpls, etc.
    1. (optionally) see install_gadget.py --help for available debuggers, e.g.:
    • ./install_gadget.py --enable-python,
    • ./install_gadget.py --force-enable-php, etc.

    Full answer:

    Language server (LS) is a separate standalone application (one for each programming language) that runs in the background and analyses your whole project in real time exposing extra capabilities to your editor (any editor, not only vim). You get things like:

    • namespace aware tag completion
    • jump to definition
    • jump to next / previous error
    • find all references to an object
    • find all interface implementations
    • rename across a whole project
    • documentation on hover
    • snippets, code actions, formatting, linting and more...

    Communication with language servers takes place via Language Server Protocol (LSP). Both nvim and vim8 (or higher) support LSP through plug-ins, the most popular being Conquer of Completion (COC).

    List of actively developed language servers and their capabilities is available on Lang Server website. Not all of those are provided by COC extensions. If you want to use one of those you can either write a COC extension yourself or install LS manually and use the combo of following VIM plug-ins as alternative to COC:

    • LanguageClient - handles LSP
    • deoplete - triggers completion as you type

    Communication with debuggers takes place via Debug Adapter Protocol (DAP). The most popular DAP plug-in for VIM is Vimspector.

    Language Server Protocol (LSP) was created by Microsoft for Visual Studio Code and released as an open source project with a permissive MIT license (standardized by collaboration with Red Hat and Codenvy). Later on Microsoft released Debug Adapter Protocol (DAP) as well. Any language supported by VSCode is supported in VIM.

    I personally recommend using COC + language servers provided by COC extensions + ALE for extra linting (but with LSP support disabled to avoid conflicts with COC) + Vimspector + debuggers provided by Vimspector (called "gadgets") + following VIM plug-ins:

    call plug#begin()
    Plug 'neoclide/coc.nvim'
    Plug 'dense-analysis/ale'
    Plug 'puremourning/vimspector'
    Plug 'scrooloose/nerdtree'
    Plug 'scrooloose/nerdcommenter'
    Plug 'sheerun/vim-polyglot'
    Plug 'yggdroot/indentline'
    Plug 'tpope/vim-surround'
    Plug 'kana/vim-textobj-user'
      \| Plug 'glts/vim-textobj-comment'
    Plug 'janko/vim-test'
    Plug 'vim-scripts/vcscommand.vim'
    Plug 'mhinz/vim-signify'
    call plug#end()
    

    You can google each to see what they do.

    Also, pipe character | separates VIM commands put in one line which makes it perfect to set up plug-in dependencies, i.e. vim-textobj-comment doesn't work without vim-textobj-user so if installation of vim-textobj-user fails the rest of the line isn't executed. Here pipe is escaped with backslash \ because it's in a new line but for VIM it's still a one-liner.

    0 讨论(0)
  • 2020-11-27 09:49

    As Paul Tomblin mentioned you have to use ctags. You could also consider using plugins to select appropriate one or to preview the definition of the function under cursor. Without plugins you will have a headache trying to select one of the hundreds overloaded 'doAction' methods as built in ctags support doesn't take in account the context - just a name.

    Also you can use cscope and its 'find global symbol' function. But your vim have to be compiled with +cscope support which isn't default one option of build.

    If you know that the function is defined in the current file, you can use 'gD' keystrokes in a normal mode to jump to definition of the symbol under cursor.

    Here is the most downloaded plugin for navigation
    http://www.vim.org/scripts/script.php?script_id=273

    Here is one I've written to select context while jump to tag
    http://www.vim.org/scripts/script.php?script_id=2507

    0 讨论(0)
  • 2020-11-27 09:54

    Use gd or gD while placing the cursor on any variable in your program.

    1. gd will take you to the local declaration.
    2. gD will take you to the global declaration.

    more navigation options can be found in here.

    Use cscope for cross referencing large project such as the linux kernel.

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