Searching within Multiple Files in Vim

So, Vim is one of the most advanced programmers’ editors around. One of the features I was missing for a long time was the ability to list all TODO and FIXME lines in my code. Well, turns out you can do this pretty easily even without a special plugin.

The command that does it is lvimgrep. It uses the Vim’s internal grep functionality and does a regex-enabled search within your files. To search multiple files at once, you can type this:

:lv /FIXME:/ **

It will go directly to the first match. But that’s not exactly the TODO list, right? Because… well, it’s not a list. So, to get a list, the full command would be:

:noautocmd silent lv /FIXME:/gj ** | lopen 20

This loads 20 file locations containing FIXME: and loads it in a location list. The result looks like this:

Screenshot of the FIXME list in gVim

Of course, typing all that is not very convenient. So, you can add this to your .vimrc:

:command! -nargs=+ Fgrep execute "noautocmd silent lvimgrep /<args>/gj **" | lopen 20

This adds a new command that you can use: Fgrep. It’s a shortcut of the above commands, and it accepts any search terms you like so:

Fgrep TODO:

or

Fgrep FIXME:

will get you your TODO or FIXME lists. The location list that opens can be navigated with the usual HJKL, and you can press Enter on a filename to open it. Fgrep command is not limited to TODO or FIXME lists. You can search just about anything you can imagine.