如何使用 VIM 搜索项目的所有文件?

有一些事情我还不理解 VIM 的方式。

其中之一就是在这样的项目中进行搜索(在 Atom 中使用 VIM) :

enter image description here

我目前使用 CtrlP 作为文件名,但是内容怎么办呢?

如何使用字符串进行搜索,然后使用 VIM 和/或 VIM 插件查看所有事件的列表?

61132 次浏览

Use :grep or :vimgrep to search file contents. The results are put onto the "location list" which you can open by typing :cw Enter.

Syntax for :grep is, by default, the same as the grep(1) command:

:grep 'my pattern.*' /path/to/dir

By default it will search the current directory (:pwd). I added set autochdir to my .vimrc so my PWD always follows the file I'm editing.

The major difference between :grep and :vimgrep is that :vimgrep (:vim for short) uses Vim-compatible regular expressions, whereas :grep uses whatever regular expressions your &grepprg uses.

You can use a custom program by setting &grepprg to something different. I personally like ack which uses Perl-compatible regex.

I've found an even better solution for this: FZF

It simply searches through everything in your project asynchronously using the :Ag command.

enter image description here

To open a file, I highlight the row (Shift-v) in the location list and hit Enter.

Apart from fzf, there are also other excellent plugins for fuzzy finding.

  • telescope.nvim (neovim only): after install, just use Telescope live_grep to search through your project.
  • Leaderf: another fuzzy finder with good performance. After install, use Leaderf rg to search through your project.