Vim: grep's wildcard doesn't work in 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 p开发者_如何学Goroperly:
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.
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).
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.
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.)
You should try vimgrep
.
Note that :grep is not vim's internal grep (that is :vimgrep), but a wrapper for the external grep program.
If you have an alternative pattern matcher installed (i.e., MinGW's or Cygwin's grep) you can edit your vimrc file to use it instead.
set grepprg=C:/MinGW/msys/1.0/bin/egrep\ -nR\ $*\ *
The '$' is where your expression will go, the '' at the end means every file type. Grep is fast enough or my codebase is small enough that I don't care to limit the file types. Using the -n and -R flags will make it return results pretty much like vimgrep (obviously the patter expressions will be different).
The ** is expanded by the external tool specified in 'grepprg', or in the case of Windows, not expanded since grepprg is just the findstr tool.
But any arguments you pass on Vim's command line also go to the tool, so just do a recursive findstr search:
:grep /S pattern *.txt
精彩评论