Get ctags in vim to go to definition, not declaration

前端 未结 10 2036
礼貌的吻别
礼貌的吻别 2020-12-07 15:25

I\'m having the problem that ctags in vim/gvim takes me to a forward declaration a lot of times instead of to the actual definition of the function.

Any way to get a

相关标签:
10条回答
  • 2020-12-07 16:23

    There are several ways to make Vim to jump to a tag directly, if there is only one tag match, otherwise present a list of tag matches.

    You can use the 'tjump' ex command. For example, the command ':tjump func1' will jump to the definition func1, if it is defined only once. If func1 is defined multiple times, a list of matching tags will be presented.

    You can position the cursor over the tag and press g Ctrl-].

    You can visually select a text and press g Ctrl-] to jump or list the matching tags.

    You can use the 'stjump' ex command. This will open the matching or selected tag from the tag list in a new window.

    You can press Ctrl-W g Ctrl-] to do a :stjump.

    Help: :tjump, g_Ctrl-], v_g_CTRL-], :stjump, Ctrl-W_g_Ctrl-]
    
    0 讨论(0)
  • 2020-12-07 16:24

    Late to the party, but for incoming vim tag googlers:

    I've found that using cscope in addition to ctags is the way to go, at least for C/C++. It's more intelligent about call trees, and you can set it to fallback to ctags if it fails. Just run "cscope -b" everytime you run ctags -R . and you'll be ready to go. If you use the settings below, you'll be able to use Ctrl-]/Ctrl-T like always, but you can also add nifty new jumps like jumping to a function declaration and showing a jumplist of function callers.

    " setup
    if has("cscope")
        set csto=0                                                                             
        set cst
        set nocsverb
        " add any database in current directory
        if filereadable("cscope.out")
        cs add cscope.out
        " else add database pointed to by environment
        elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
        endif
        set csverb
    endif
    
    " jump to a function declaration
    nmap <silent> <C-\> :cs find s <C-R>=expand("<cword>")<CR><CR>1<CR><CR>
    " show a list of where function is called
    nmap <silent> <C-_> :cs find c <C-R>=expand("<cword>")<CR><CR>
    
    0 讨论(0)
  • 2020-12-07 16:27

    You should be able to use tn and tp to jump to the various matching tags.

    • Press ^] to take you to the first match.
    • If that's not the match you want, type :tn to go to the next.
    • If you typed :tn too many times you can type :tp to return to the previous one.
    0 讨论(0)
  • 2020-12-07 16:29

    I believe Vim goes to the first tag in the tags file by default. You can select a different one if you prefer: use :tj (similar to :tselect, but auto-jump if there's only one match) or Ctrl-] followed by :tn).

    The only way of changing the default is to change the order of the tags file, but I don't believe ctags offers a command-line option to do this.

    This isn't as hard as it sounds as you basically need a script that opens the tags file, sorts it by the 'kind' of tag and writes it back out again. The 'kind' in the tag is a single character describing whether it's a function (f), a function prototype (p), a macro, a enumerated name etc etc etc. If you're using Linux, it could, in theory, be as simple as:

    #!/bin/sh
    ctags -R -f - . | tac > tags
    

    Since tac reverses the order of lines in a file, this will automatically put the definition first. However, it gets a bit more complicated as the header needs to be maintained and Vim prefers the tag file to be sorted, so it's better to go through the file and sort on the first entry (the tag name) in forward order and then the kind in reverse order. Therefore, something more complicated may be better.

    I apologise for the shameless plug, but I have written a Vim plugin that (indirectly) does what you need. It is intended for adding lots of extra highlighting groups for things like function names, macros, enums etc. However, one of the other things that this does is re-sort the tag file so that the function implementation comes before the function declaration, thereby achieving what you want (I had the same need as you). If you don't want any of the highlighting functionality, you could probably strip it all out quite easily: it's a fairly simple python program and an even simpler Vim script and is available from my site.

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