Just_Mad
Just_Mad

Reputation: 4077

Vim search text in folder

Is there a way to search for some text in a folder and to display search results in a separate buffer in Vim? (Like Sublime Text Ctrl + Shift + F results)

Upvotes: 11

Views: 8471

Answers (3)

romainl
romainl

Reputation: 196546

No, you don't need any plugin. The default :vimgrep (or :vim) is all you need.

Search for foo in every file in the current directory:

:vim foo * | cw

Search for foo in every JavaScript file in the current directory:

:vim foo *.js | cw

Search for foo in every JavaScript file in the current directory recursively:

:vim foo **/*.js | cw

Search for the current word in every file in the current directory:

:vim <C-r><C-w> * | cw
:vim <cword> * | cw

(edit: use :cw[indow] instead of :copen)

Upvotes: 28

Ingo Karkat
Ingo Karkat

Reputation: 172550

The :grep Vim command delegates the searching to the external grep tool (or a compatible alternative like ack, set via 'grepprg'). Alternatively, you can use :vimgrep, which performs the searching inside Vim. This allows use to use the same Vim-style regular expressions and glob patterns (like **/), but it usually is slower because each file is read into a Vim buffer.

Both commands display the results in the quickfix window, from which you can navigate to the matches.

You don't need any plugins for this, though there are several available that try to make the handling simpler or support different search commands (e.g. the already mentioned ack.vim).

Upvotes: 5

K Z
K Z

Reputation: 30453

Sounds like you need ack.vim:

This plugin is a front for the Perl module App::Ack. Ack can be used as a replacement for 99% of the uses of grep. This plugin will allow you to run ack from vim, and shows the results in a split window.

Usage:

:Ack [options] {pattern} [{directory}]
Search recursively in {directory} (which defaults to the current directory) for the {pattern}.

Files containing the search term will be listed in the split window, along with the line number of the occurrence, once for each occurrence. [Enter] on a line in this window will open the file, and place the cursor on the matching line.

Upvotes: 9

Related Questions