3

I have ctags installed on my Windows machine.

I have the following mappings in my vimrc file

set tags=./tags;/
nnoremap <F5> :!ctags -R .<CR>
nnoremap ,dd :exec("tag ".expand("<cword>"))<CR>

I'm trying to navigate a big project which has many classes. There are many functions that have the same name but defined in different classes.

It happens a lot that when trying to jump to the funtion's definition, it jumps to a function defintion but in a different class. As I'm new to the project, I may not recognize that this is not the right function's definition every time.

Is there a way to make jumping to function's definition smarter that it jumps to the function's definition in the right class?

5
  • 2
    The one LSP has, usually smarter than ctags. Btw, to jump to tag there is a mapping: C-] and to jump back C-t
    – Maxim Kim
    Feb 28, 2020 at 7:12
  • I believe Maxim Kim is suggesting you try a plugin that interacts with a Language Server Protocol server (sounds intimidating but it's relatively easy). Here are a bunch of LSP related Vim plugins: vimawesome.com/?q=tag:lsp
    – B Layer
    Feb 29, 2020 at 1:28
  • I could finally install coc.nvim. But I can't install ccls and configure it with coc.nvim. I didn't find any clear steps online how to do it. Any help would be appreciated. Mar 6, 2020 at 9:03
  • @SalahuddinAhmed Post a new question with plenty of details. That's your best shot at getting an answer.
    – B Layer
    Mar 8, 2020 at 12:38
  • You can also use :tnext/:tprev when there are multiple matching tags, or :tselect for a menu
    – D. Ben Knoble
    May 20, 2021 at 13:51

3 Answers 3

2

You didn't mention the language you're using with ctags, but in any case, you're most likely not calling ctags with the necessary flags to include namespace information. Support for each language varies, but you need to add --extras=+q in order to enable namespaces:

#LETTER NAME              ENABLED LANGUAGE    FIXED DESCRIPTION
...
q       qualified         no      NONE        no    Include an extra class-qualified tag entry for each tag

For example, you can run the following for c++

ctags -R -n --c++-kinds=+p --fields=+iaS --extras=+q *.cpp

and your tags will contain the necessary information to jump to the right class:

Car     example.cpp     2;"     c       file:
Car::brand      example.cpp     4;"     m       class:Car       typeref:typename:string file:   access:public

You might need to add the namespace separator to iskeyword (e.g. set iskeyword+=:) and then you can simply run tselect or tj to go to the correct location.

1

I recommend getting in the habit of (or changing your mapping to) :tselect instead of :tag if there are often multiple matches. The former gives you a menu from which to pick the correct tag.

0

ctags creates a static index of language objects. When several objects share the same name it jumps to the first one in the index, I guess. It doesn't have a context, like which file you are editing, the imported modules, additional bindings, so it cannot be smarter, that's why language servers are currently the best option available.

Looks like you need a language server for C or C++. I haven't tried any of these, but besides ccls, there's cquery and clangd, the latter seems to be the easier to install.

I know COC is the most popular Vim client for language servers, but I think it's overly complex. If it gets hard to configure COC I suggest you try LanguageClient. Despite its repository name it also supports Vim

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.