Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all occurrences of a variable in Vim?

Tags:

vim

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?

like image 204
neuron Avatar asked Mar 08 '12 10:03

neuron


People also ask

How do I count the number of occurrences of a string in vim?

This makes it easy to count the number of occurrences of the word under the cursor: first press * to search for the current word, then enter :%s///gn to count all occurrences of that word.

How do we use grep to search for a pattern in multiple files in vim?

From the root of your project, you can search through all your files recursively from the current directory like so: grep -R '. ad' . The -R flag is telling grep to search recursively.

How do we search for old and replace it with new vim?

It's simple to search and then decide if you want to keep or replace each result in a file: Execute a regular search with / . Use the keystroke cgn on the first result to replace it. Type n or N to go to the next result.


2 Answers

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.

like image 160
eckes Avatar answered Oct 16 '22 19:10

eckes


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:

  • automatically add the nearest database (parent directories) when you enter a file: how to auto load cscope.out in vim
  • for function calls: How to find the callers and callee of a function in C code in vi/vim?