Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: grep's wildcard doesn't work in windows

Tags:

grep

vim

windows

I try to use vim's internal grep with '**' wildcard as in the following command:

grep "test" **\*.txt

vim gives the following error:

FINDSTR: Cannot open **\*.txt

When I remove the '**' wildcard, the command works properly:

grep "test" *.txt

I changed the backslashes to forward slashes, but it didn't help neither:

grep "test" **\*.txt

This gives the above error again.

What might be the reason?

Note: I use GVim 7.2 on Microsoft Windows XP.

like image 944
Mert Nuhoglu Avatar asked Jun 24 '10 09:06

Mert Nuhoglu


4 Answers

Doing a ":grep" in Vim under XP does not use "grep.exe" by default. By default "FINDSTR" is used which is part of the Windows installation. "FINDSTR" is not compatible to grep. Due to this you get the error message

FINDSTR: Cannot open **\*.txt

See ":help grepprg".

If you want to use a Windows port of grep you have to install it since grep is neither part of Windows nor of the Vim installation.

But since 7.0 Vim has an internal grep called vimgrep. See ":help vimgrep" for details.

You have to set 'grepprg' accordingly so that either grep or vimgrep is used (instead of the default FINDSTR).

like image 155
Habi Avatar answered Nov 18 '22 14:11

Habi


On Windows, if you want to hook into the faster findstr you can use

set grepprg=findstr\ /n\ /s

In your .vimrc and now

:vimgrep methodname **/*.py is equivalent to

:grep methodname *.py

Both return a list of results you can navigate through the quickfix window (:copen, :cn/:cp, :cclose)

In my experience findstr is about 30 times faster than vimgrep, however it might not matter with your project size and vimgrep's regex are far nicer to use.

like image 2
Conrad.Dean Avatar answered Nov 18 '22 13:11

Conrad.Dean


Install cygwin, mingw, or unxutils to get grep (I use cygwin). Add the bin directory to your PATH.

And like Habi said, add to your vimrc:

set grepprg=grep\ -nH

(This is what grep on *nix uses by default.)

Also, if you :help grep, you'll get a description of the differences between grep and vimgrep. (Speed vs. portability and flexibility.)

like image 2
idbrii Avatar answered Nov 18 '22 13:11

idbrii


You should try vimgrep.

like image 1
Tassos Avatar answered Nov 18 '22 12:11

Tassos