Search in multiple files with Vim

Vim has a built-in feature to search over multiple files in a directory (even recursively across the sub-directories).

Vim can use external applications (e.g. grep) to do the search, but as I’m using currently a portable version of Gvim, I’m more-or-less forced to use the built in search functionality. The general command look like:

:vim[grep][!] /{pattern}/[g][j] {file}

The [g] option tells vim to count all matches in a line, not just the first one. If the [j] option is given vim won’t jump to the first match in the file.

For a recursive search use **.

:vimgrep /solve/j **/*.gms

Vim puts all matches to the “quickfix list” that can be opened with the command

:cw

This command splits the buffer to two windows (viewports). Below you will find the quickfix list with file names and the lines where a match was found. Just hit RET and the selected file will be loaded in the upper window.

Advertisement
This entry was posted in geeky, notes and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s