80

There are a couple of things I do not yet understand the VIM way.

One of these is searching in a project like so (using VIM in Atom):

enter image description here

I use CtrlP currently for file names, but what about the contents?

How can I search with a string, and then look through a list of all occurrences using VIM and/or VIM plugins?

Sem
  • 4,477
  • 4
  • 33
  • 52

5 Answers5

103

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

Sem
  • 4,477
  • 4
  • 33
  • 52
  • 12
    Note: this requires silver_searcher to be installed as well https://github.com/ggreer/the_silver_searcher – Miguel Mota Feb 16 '18 at 02:27
  • For a less pretty solution that comes with VIM, https://stackoverflow.com/questions/11975174/how-do-i-search-the-open-buffers-in-vim – Nathan majicvr.com Feb 13 '19 at 16:08
  • @Sem I installed silver_searcher, fzf vim-plug package. Can you tell me how to do it... – jian Dec 09 '21 at 08:12
  • @jian in your vim config add this line `nnoremap :Ag`, this means call Ag whenever you press ctrl + s – ezio Nov 01 '22 at 07:50
39

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.

amphetamachine
  • 27,620
  • 12
  • 60
  • 72
  • Yes, and what about the list of occurrences? – Sem Jul 15 '16 at 14:36
  • @Sem I've expanded my answer to be as thorough as I can without getting too deep into it. – amphetamachine Jul 15 '16 at 14:42
  • `:cw` is just what I need! You sir, are the best. – Sem Jul 15 '16 at 14:49
  • Hi. I find this super useful. I see I am able to navigate the list up and down. Is there any way to open a file directly by clicking `Enter` on the underlined file in the "location list"? I do a similar thing with NerdTree. Or maybe there is a "vimer" way to do this, hehe (still learning) – vabada Jul 27 '17 at 10:45
  • This is the slowest way. – calbertts Jul 12 '18 at 11:59
5

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.
jdhao
  • 24,001
  • 18
  • 134
  • 273
1

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

1

If you're using Neovim, a better solution is to use telescope.nvim with BurntSushi/ripgrep Suggested Dependencies you can use live_grep , grep_string and also find_files

Eliphaz Bouye
  • 21
  • 1
  • 4