Sem
Sem

Reputation: 4669

How do I search in all files of my project using VIM?

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?

Upvotes: 92

Views: 103827

Answers (5)

amphetamachine
amphetamachine

Reputation: 30623

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 (PCRE). Other grep-esque programs are available, it's up to you.

" .vimrc
" To get ack, use:
" $ curl https://beyondgrep.com/ack-v3.7.0 > ~/bin/ack && chmod +x ~/bin/ack
if executable('ack')
    " Use ack over grep (automatically ignores *~)
    set grepprg=ack\ --nogroup\ --nocolor
endif

Upvotes: 45

Eliphaz Bouye
Eliphaz Bouye

Reputation: 21

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

Upvotes: 1

jdhao
jdhao

Reputation: 28459

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.

Upvotes: 11

Varun Muriyanat
Varun Muriyanat

Reputation: 41

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

Upvotes: 1

Sem
Sem

Reputation: 4669

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

Upvotes: 106

Related Questions