14

In vim, how to I find all occurrences of a variable in files under a certain directory?

I know vimgrep works sometimes, but it looks for text only and doesn't work if other classes have variables of the same name and I only want the variable under a specific class.

What should I do? Or should I get an IDE instead?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
neuron
  • 1,896
  • 1
  • 19
  • 24
  • 1
    Yes, use an IDE. Vim only uses basic mechanisms like regular expressions or binary search: it doesn't understand your code the way an IDE does. – romainl Mar 08 '12 at 10:37
  • What language(s) do you work with? The `cscope` suggestions are good for C-like languages, but not so much for others AFAIK. – David Pope Mar 09 '12 at 00:10

2 Answers2

18

Why would you want to use another IDE when you already have one? Vim is an IDE that is configurable and usable for different languages..

You could use cscope to build a database of your code. This database

  • Allows searching code for:
    • all references to a symbol
    • global definitions
    • functions called by a function
    • functions calling a function
    • text string
    • regular expression pattern
    • a file
    • files including a file

Further features of Cscope:

  • Curses based (text screen)
  • An information database is generated for faster searches and later reference
  • The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized 'grep database' (use it to browse large text documents!)
  • Has a command line mode for inclusion in scripts or as a backend to a GUI/frontend
  • Runs on all flavors of Unix, plus most monopoly-controlled operating systems.

Once your database is created, you could browse through the usages of your variables, functions, etc.


Edit (slightly off-topic):
another cool thing that's quite handy when working with Vim on code is the taglist plugin that uses Ctags:

The "Tag List" plugin is a source code browser plugin for Vim and provides an overview of the structure of source code files and allows you to efficiently browse through source code files for different programming languages.

Dan Bechard
  • 5,104
  • 3
  • 34
  • 51
eckes
  • 64,417
  • 29
  • 168
  • 201
  • +1 for cscope being awesome. I usually just have a terminal tab open for cscope full-time. – alesplin Mar 08 '12 at 17:33
  • Now that I've spent a bit of time with cscope, it seems to me that cscope does not differentiate variables of the same name that belong to different classes. class_b::var may be returned even though I'm only looking for class_a::var. I'm using C++. Am I doing anything wrong? – neuron Mar 09 '12 at 11:06
  • For example, my cursor's on a variable name and when I press "ctrl-\ g" hundreds of options get generated... that's apparently wrong. It seems that the script I use only sends in the without any other context information. – neuron Mar 09 '12 at 11:18
  • 1
    Although vim is/can be thought of as an IDE, the learning curve to get it set up to behave like an IDE is very steep. One actually needs to understand both vimscript and the `.vim` folder and how they are both used – puk Mar 09 '12 at 21:20
  • @neuron: yes, I did. As the docs say: cscope **supports** C but is also **useful** for C++... You're doing nothing wrong. – eckes Mar 12 '12 at 05:53
  • @eckes Code in C has the same problem. When searching for a field in a structure, fields in other structures with the same name will be displayed. – Claudiu Apr 03 '18 at 06:40
  • Also if you use vim's internal `:make` command if there's a warning or error you can do `:cnext` to go to that warning/error. It will show you what the problem is and iirc even take you to the the right place on the line. Of course you'll want a Makefile built but those aren't hard to make though maybe you have to learn how. But even so once you know you know. – Pryftan Dec 09 '19 at 16:18
  • thku,Benefit a lot – linjiejun May 29 '22 at 14:09
  • vim 项目浏览插件cscope,taglist,ctags – linjiejun May 29 '22 at 14:15
10

cscope step by step example

Go to the base directory of your project, and run:

cscope -Rb

This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.

Note that cscope is not designed to work with other languages other than C. Sometimes it does work for other C-like syntax languages like Python, and you can force it to recognize those files with hacks such as cscope -Rb -s * and others mentioned at: Using cscope to browse Python code with VIM? but it won't work as well as for C.

Open vim, and run:

:cs add cscope.out
:cs find s my_func

s is a mnemonic for symbol. The other cscope provided queries are also possible.

The cscope interface (ouside Vim) also has a variable assignment query (subset of symbol occurrences) which Vim does not seem to offer (?)

This adds a list of the callers to the quickfix list, which you can open with:

:copen

Go to the line that interests you and hit enter to jump there.

See also:

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • `cscope -Rb` does not work, you should use `cscope -Rb -s *` – minerals Apr 22 '20 at 11:57
  • @minerals can you give a minimal example of a project where not using `-s` does not work? I'm not sure what the option is meant to do. – Ciro Santilli OurBigBook.com Apr 22 '20 at 13:35
  • In any of Python based project dirs. Go to the dir and run `cscope -Rb`, you should see "cscope: no source files found" message, `cscope.out` is not created. Run `cscope -Rb -s *` instead and the `cscope.out` file will be created. – minerals Apr 22 '20 at 15:03
  • 1
    @minerals this says cscope is not designed to work with Python: https://stackoverflow.com/questions/3718868/using-cscope-to-browse-python-code-with-vim/3779871#3779871 I've added a clarification – Ciro Santilli OurBigBook.com Apr 22 '20 at 15:34
  • Language Servers offer much better support of all these features out of the box, so the answers on this post are largely obsolete – Waqar Oct 27 '20 at 18:54