Search multiple files with :vimgrep

#44

Run time:
7:25

Mar 1, '13

Mar 1, 2013

vimgrep is Vim’s built-in command for searching across multiple files. It’s not so fast as external tools like ack and git-grep, but it has its uses. vimgrep uses Vim’s built-in regex engine, so you can reuse the patterns that work with Vim’s standard search command.

Download

Have you ever wished that Vim would report your position as you navigate through search matches? For example, by saying: ‘match 2 out of 5’.

That’s not a feature provided by Vim’s search command, but it is a feature supplied by the quickfix list. We can use the :vimgrep command to populate the quickfix list with search results from the current file:

The special ## symbol is expanded to represent each of the files from the arglist. It’s really handy in this context. Suppose that we wanted to search the same set of files for one pattern, then another pattern, and so on. It would be a drag to have to specify the same set of files again and again:

Not having to think about which files you’re dealing with frees you up to focus on the more interesting part of the vimgrep command: the pattern.

For more ideas on how to specify the set of files for vimgrep to look inside, check out these articles on ack -f and git ls-files.

Navitating the quickfix list

The results from the vimgrep command are collected in the quickfix list. We can traverse them with the following commands:

Ex command

unimpaired map

effect

:cprev[ious]

[q

reverse through quickfix list

:cnext

]q

advance through quickfix list

:cfirst

[Q

go to start of quickfix list

:clast

]Q

go to end of quickfix list

The square-bracket mappings are not built-in to Vim, but are added by Tim Pope’s unimpaired plugin (which I recommend).

Vimgrep: pros, cons, and alternatives

I reach for vimgrep when I want to use Vim’s built-in regex engine. I love being able to test my regular expression in one buffer using the search command, then use the same pattern without modification to search across the entire project. For me, that convenience is the killer feature of vimgrep.

On the downside, vimgrep isn’t as quick as some of the alternatives out there. If you want to search a large codebase and get results fast, then you’d be better off using an external tool like git-grep, ack or the silver searcher. All of these external tools integrate with the quickfix list, and you can find Vim plugins for each of them:

Browse similar content

Level-up your Vim

Training

Boost your productivity with a Vim training class.
Join a public class,
or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient.
Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim.
This lets you edit Ruby code at a higher level of abstraction.
Available to buy from thoughtbot..