grep如何递归目录并在指定类型文件中查找?[已解决]

Vim、Emacs配置和使用
回复
beyond93
帖子: 39
注册时间: 2005-05-06 0:46

grep如何递归目录并在指定类型文件中查找?[已解决]

#1

帖子 beyond93 » 2009-04-09 21:59

grep -r “hello” ./查找是递归在当前目录及其子目录下任何文件中查找hello
如果想指定在当前目录及其子目录下的c文件中查找hello应该如果执行命令?
grep -r "hello" *.c会提示*.c文件不存在
上次由 beyond93 在 2009-04-13 23:05,总共编辑 1 次。
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
联系:

Re: grep如何递归目录并在指定类型文件中查找?

#2

帖子 xiooli » 2009-04-09 22:11

代码: 全选

find . -iname "*.c" -exec grep "hello" {} \;
beyond93
帖子: 39
注册时间: 2005-05-06 0:46

Re: grep如何递归目录并在指定类型文件中查找?

#3

帖子 beyond93 » 2009-04-09 22:36

非常感谢!
在vim有没有插件可以很方便完成这个事情?
grep.vim和easygrep.vim都不行。
sarrow
帖子: 403
注册时间: 2007-10-27 1:04

Re: grep如何递归目录并在指定类型文件中查找?

#4

帖子 sarrow » 2009-04-10 15:20

grep.vim和easygrep.vim都不行
你没有看help。

当前目录查找:

:Grep key_word *.c *.h

递归查找:

:Rgrep key_word *.c *.h
beyond93
帖子: 39
注册时间: 2005-05-06 0:46

Re: grep如何递归目录并在指定类型文件中查找?

#5

帖子 beyond93 » 2009-04-10 19:44

grep.vim在windows下Rgrep总是不行,Grep倒是可以,
不知道为啥,已经按照手册说的安装并设置windows版的findutils和grep,
使用cygwin倒是可以,但在cygwin下vim太慢了。
还是想用windows版的gvim(没有办法,在公司的是用windows)
不知道各位朋友知道如果折腾么?
sarrow
帖子: 403
注册时间: 2007-10-27 1:04

Re: grep如何递归目录并在指定类型文件中查找?

#6

帖子 sarrow » 2009-04-11 22:57

已经设置了find,xargs,grep的路径了吧?

再在_vimrc中添加:

let Grep_Shell_Quote_Char = '"'

即可。windows下面比较诡异一点。
beyond93
帖子: 39
注册时间: 2005-05-06 0:46

Re: grep如何递归目录并在指定类型文件中查找?

#7

帖子 beyond93 » 2009-04-12 9:52

sarrow 写了:已经设置了find,xargs,grep的路径了吧?

再在_vimrc中添加:

let Grep_Shell_Quote_Char = '"'

即可。windows下面比较诡异一点。
还是不行,find,xargs,grep的路径都OK的,直接在console中都可以调用的。
按照你的的做法还是不行,能不能把你在windows下_vimrc中grep.vim的配置贴上来一下,谢谢!
sarrow
帖子: 403
注册时间: 2007-10-27 1:04

Re: grep如何递归目录并在指定类型文件中查找?

#8

帖子 sarrow » 2009-04-12 11:25

还是不行,find,xargs,grep的路径都OK的,直接在console中都可以调用的。
你很强大。console里竟然能成功调用!

windows cmd内建了find指令,除非你给全find的绝对路径,不然调用的只能是cmd.find

附grep.vim相关设置:
if MySys() == 'windows'
let Grep_Path = 'C:\mingw\bin\grep.exe'
let Fgrep_Path = Grep_Path.' -F'
let Egrep_Path = Grep_Path.' -E'

let Grep_Find_Path = 'C:\mingw\bin\find.exe'
let Grep_Xargs_Path = 'C:\mingw\bin\xargs.exe'

let Grep_Default_Filelist = '*.c *.cpp *.hpp *.h'
let Grep_Shell_Quote_Char = '"'
endif

另,你的find,xargs,grep工具所在目录的路径包含空格否?——包含的话,请加上双引号再试试?

我这里是没有问题的。
beyond93
帖子: 39
注册时间: 2005-05-06 0:46

Re: grep如何递归目录并在指定类型文件中查找?

#9

帖子 beyond93 » 2009-04-12 19:01

sarrow 写了:
还是不行,find,xargs,grep的路径都OK的,直接在console中都可以调用的。
你很强大。console里竟然能成功调用!

windows cmd内建了find指令,除非你给全find的绝对路径,不然调用的只能是cmd.find

附grep.vim相关设置:
if MySys() == 'windows'
let Grep_Path = 'C:\mingw\bin\grep.exe'
let Fgrep_Path = Grep_Path.' -F'
let Egrep_Path = Grep_Path.' -E'

let Grep_Find_Path = 'C:\mingw\bin\find.exe'
let Grep_Xargs_Path = 'C:\mingw\bin\xargs.exe'

let Grep_Default_Filelist = '*.c *.cpp *.hpp *.h'
let Grep_Shell_Quote_Char = '"'
endif

另,你的find,xargs,grep工具所在目录的路径包含空格否?——包含的话,请加上双引号再试试?

我这里是没有问题的。
我是安装http://gnuwin32.sourceforge.net/packages/grep.htm
http://gnuwin32.sourceforge.net/packages/findutils.htm
这两个工具,_vimrc中是这样设置:
let Grep_Path = 'C:\GnuWin32\bin\grep.exe'
let Fgrep_Path = 'C:\GnuWin32\bin\fgrep.exe'
let Fgrep_Path = 'C:\GnuWin32\bin\fgrep.exe'
let Agrep_Path = 'C:\GnuWin32\bin\agrep.exe'
let Agrep_Path = 'C:\GnuWin32\bin\agrep.exe'
let Grep_Xargs_Path = 'C:\GnuWin32\bin\xargs.exe'
let Grep_Default_Options = '-rin'
let Grep_Shell_Quote_Char = '"'
nnoremap <silent> <F3> :Grep<CR>
nnoremap <silent> <F4> :Rgrep<CR>
你的mingw的bin中怎么有grep.exe,find.exe和xargs.exe?我安装了mingw后没有那些工具
谢谢!
beyond93
帖子: 39
注册时间: 2005-05-06 0:46

Re: grep如何递归目录并在指定类型文件中查找?

#10

帖子 beyond93 » 2009-04-13 23:03

终于找到问题了,我装了cygwin,设置了环境变量,这样在cmd中可以直接调用cygwin的工具,
结果就是对于GnuWin32里边的tool来说,不是纯windows cmd环境,对于grep.vim来说,
有不是纯粹的cygwin环境,结果就是Rgrep不能用。
现在把cygwin的环境变量干掉,同时使用windows版本的cscope和ctags即可
:em04
回复