Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand code with vim?

Vim is a nice editor and I use it for many tasks.

However, when it comes to start working on a new (possibly huge) codebase, I don't feel comfortable using it to go around the code with the objective of understanding how things work.

For example, if I want to see where a C++ function is used, I can :vimgrep for that function in every **/*.cpp files and :copen the quickfix window to jump on every occurrence... of that string.

If I do the same with e.g. Eclipse (call hierarchy of a C++ method), that will not be just a string, but a C++ method defined in an object, so I will get a precise indication of the usages of that function (and not also a function with the same name defined in another class).

So the question is, how to make vim a powerful tool to analyze code?

Subquestions:

  • Are there any vim plugins designed for this?
  • Does it make sense to use vim to only analyze code? Probably external tools (e.g.: OpenGrok) can do the job?
like image 705
Vincenzo Pii Avatar asked Nov 23 '25 03:11

Vincenzo Pii


1 Answers

Vim is a text editor. What you want is almost completely orthogonal to editing text and completely outside of Vim's own abilities.

However, Vim is quite good at using external tools like ctags and cscope for navigating within a project. Supposing you have created a tags file and/or a cscope.out database, Vim has a bunch of commands you can use to "jump to definition", "jump to usage", etc.: :ts[elect] foo, <C-]> over a function name and so on… You can find all the info you need in :h ctags and :h cscope.

If you are curious, GNU GLOBAL is another alternative. Another pro of cscope is that it comes with its own TUI that you can use in your shell.

The only plugin offering a cscope (+ ctags) interface I know of is CCTree which seems to be limited to C.

There are a bunch of ctags oriented plugins like TagBar or TagList you could try but note that, while ctags is limited to definitions, cscope can also do usage and callers.

You should keep in mind that these tools are code indexers: you shouldn't expect them to "understand" your code or be even remotely as precise as IDE tools. However I love Vim, I'd suggest you use a tool better suited to that task than a text editor.

like image 56
romainl Avatar answered Nov 25 '25 20:11

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!