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?

  • 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 '20 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 '20 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 '20 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 '20 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 at 13:51

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, privacy policy and cookie policy

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