How to find all occurrences of a variable in Vim?

…衆ロ難τιáo~ 提交于 2020-05-24 21:37:41

问题


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?


回答1:


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.




回答2:


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?


来源:https://stackoverflow.com/questions/9616144/how-to-find-all-occurrences-of-a-variable-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!