Jump to function definition in vim

后端 未结 11 2436
独厮守ぢ
独厮守ぢ 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:28

    After generating ctags, you can also use the following in vim:

    :tag <f_name>
    

    Above will take you to function definition.

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

    g* does a decent job without ctags being set up.

    That is, type g,* (or just * - see below) to search for the word under the cursor (in this case, the function name). Then press n to go to the next (or Shift-n for previous) occurrence.

    It doesn't jump directly to the definition, given that this command just searches for the word under the cursor, but if you don't want to deal with setting up ctags at the moment, you can at least save yourself from having to re-type the function name to search for its definition.

    --Edit-- Although I've been using g* for a long time, I've recently discovered two shortcuts for these shortcuts!

    (a) * will jump to the next occurrence of the word under the cursor. (No need to type the g, the 'goto' command in vi).

    (b) # goes to the previous occurrence, in similar fashion.

    N and n still work, but '#' is often very useful to start the search initially in the reverse direction, for example, when looking for the declaration of a variable under the cursor.

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

    Use ctags. Generate a tags file, and tell vim where it is using the :tags command. Then you can just jump to the function definition using Ctrl-]

    There are more tags tricks and tips in this question.

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

    1- install exuberant ctags. If you're using osx, this article shows a little trick: http://www.runtime-era.com/2012/05/exuberant-ctags-in-osx-107.html

    2- If you only wish to include the ctags for the files in your directory only, run this command in your directory:

    ctags -R
    

    This will create a "tags" file for you.

    3- If you're using Ruby and wish to include the ctags for your gems (this has been really helpful for me with RubyMotion and local gems that I have developed), do the following:

    ctags --exclude=.git --exclude='*.log' -R * `bundle show --paths`
    

    credit: https://coderwall.com/p/lv1qww (Note that I omitted the -e option which generates tags for emacs instead of vim)

    4- Add the following line to your ~/.vimrc

    set autochdir 
    set tags+=./tags;
    

    (Why the semi colon: http://vim.wikia.com/wiki/Single_tags_file_for_a_source_tree )

    5- Go to the word you'd like to follow and hit ctrl + ] ; if you'd like to go back, use ctrl+o (source: https://stackoverflow.com/a/53929/226255)

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

    Another common technique is to place the function name in the first column. This allows the definition to be found with a simple search.

    int
    main(int argc, char *argv[])
    {
        ...
    }
    

    The above function could then be found with /^main inside the file or with :grep -r '^main' *.c in a directory. As long as code is properly indented the only time the identifier will occur at the beginning of a line is at the function definition.

    Of course, if you aren't using ctags from this point on you should be ashamed of yourself! However, I find this coding standard a helpful addition as well.

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

    If everything is contained in one file, there's the command gd (as in 'goto definition'), which will take you to the first occurrence in the file of the word under the cursor, which is often the definition.

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