14

In vim :make makes it easy to build and fix errors, and :grep and :vimgrep behaves like :make. But why do you need these two commands? Why not just search for the pattern and just press n? What are the advantages of them over just searching?

15

As the doc say:

Vim has two ways to find matches for a pattern: Internal and external.

In a nutshell, :vimgrep uses Vim's grep mechanism to read and find in files. :grep uses an external commands to achieve the same job.

The / search is for the current buffer only, whereas :[vim]grep search through a pattern of files, allowing you to search in an entire project at once.

Have a look at :h grep for more information.

4

/? only search the current buffer but :grep and :vimgrep take arbitrary files/patterns/globs as arguments.

3

One benefit of :[vim]grep over /? is that the results get put into the quickfix list (or location list if you use :l[vim]grep), this means that you can pull up a list of all matches using :cl. You can then use a number to go directly to the result you want from the list.

It also means that you can use the same command for searching the current buffer :[vim]grep pattern % as for searching your entire code base :[vim]grep pattern path so there's some nice symmetry to it.

Further to this you can then branch out and use the Silver Surfer / Ripgrep to give you very powerful searching. These can be mapped to the :[vim]grep command.

0

There are 3 primary advantages I see

  1. Use a consistent regex engine/syntax: Using :grep/vimgrep allows you to define a grepprg, which can be defined to grep/ack/ag etc. Using this you can search the current buffer or multiple buffers/files, using a consistent regex engine. On the other hand, if you use vim’s/pattern option, you are stuck on vim’s default regex engine, which has a different syntax compared to extended regular expression syntax. So, anytime you have to search for an expression, you will have to think/remember whether you should use \) or just ) and other differences. This can be very annoying (https://stackoverflow.com/questions/3864467/whats-the-difference-between-vim-regex-and-normal-regex)

  2. vimgrep populates a quickfix/location list: using :grep/vimgrep allows you to populate a search list. (https://medium.com/@lakshmankumar12/quickfix-and-location-list-in-vim-ca0292ac894d).

    If you do want to use the location list, you can use ]q shortcut to navigate it if you have Tim-Popes unimpaired plugin (https://github.com/tpope/vim-unimpaired)

  3. complicated searches while within vim: typically if you have to do complicated searches you may have to use the command line. However, if you define the greprg properly and use a command such as [1]

    :vim /pattern1/ find . -type f

    You can do very complicated searches and display the search results in your location list or quickfix list.

[1] http://vimcasts.org/episodes/search-multiple-files-with-vimgrep/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.