3

How do I do a grep in netrw in Vim? ctrl-p can be used in this way, is there a similar plugin for grep?

E.g., when I'm in a directory, I want a list of files containing this or that keyword.

Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57

2 Answers2

6

Netrw does have "that feature"; see :help netrw-star. You can do for instance :Explore */filepath or more generally use one of the patterns below:

*/filepath   files in current directory which satisfy filepath
**/filepath  files in current directory or below which satisfy the
            file pattern
*//pattern  files in the current directory which contain the
            pattern (vimgrep is used)
**//pattern files in the current directory or below which contain
            the pattern (vimgrep is used)

Use :Pexplore and :Nexplore to go to the previous/next matching file (and @: to repeat the chosen command); hit <cr> to enter the file.

CervEd
  • 3,306
  • 28
  • 25
user21497
  • 1,061
  • 8
  • 9
2

Netrw doesn't have that feature.

You can use the built-in :vimgrep:

:vim foo * | cw

or :grep, that uses your external grep by default or whatever alternative program (like ack or ag) via the grepprg option:

:grep foo * | cw

See :help :grep, :help :vimgrep and :help :cwindow.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • Won't that search in my pwd instead of the netrw dir I'm in? – Olle Härstedt Sep 22 '14 at 14:31
  • `let netrw_keepdir = 0` keeps netrw's browsing dir and Vim's current dir in sync. You can also keep it at its default value and press `c`. See `:help netrw-c`. – romainl Sep 22 '14 at 15:27
  • 1
    You can search a directory with `:vimgrep` like so: `:vim foo dir/*` and recursively like: `:vim foo dir/**`. I also suggest you take a look at [Ack](http://beyondgrep.com/) , [Ag the Silver Surfer](https://github.com/ggreer/the_silver_searcher), or [git grep](https://www.kernel.org/pub/software/scm/git/docs/git-grep.html) as they are typically more performant than regular grep and certainly `:vimgrep` – Peter Rincker Sep 22 '14 at 16:28