Vim:自定义grep查找文件但不打开它们

Vim:自定义grep查找文件但不打开它们,vim,grep,Vim,Grep,我的grep程序在vimrc中定义如下: set grepprg=grep\ -R\ --exclude=*~\ --exclude=*.swp\ --exclude-dir=vendor\ --exclude-dir=node_modules\ --exclude=tags\ --exclude=tags.vendor 当我搜索时,它会显示一个结果列表,但当我搜索时,尽管它会列出文件,但我无法打开它们。快速修复/位置列表由:grep根据'grepformat'中指定的逗号分隔格式填充 'gr

我的grep程序在vimrc中定义如下:

set grepprg=grep\ -R\ --exclude=*~\ --exclude=*.swp\ --exclude-dir=vendor\ --exclude-dir=node_modules\ --exclude=tags\ --exclude=tags.vendor

当我搜索时,它会显示一个结果列表,但当我搜索时,尽管它会列出文件,但我无法打开它们。

快速修复/位置列表由
:grep
根据
'grepformat'
中指定的逗号分隔格式填充

'grepformat'
默认为
%f:%l:%m,%f:%l%m,%f%l%m
。这里,
%f
是文件名,
%l
是行,
%m
是实际匹配的行。grep输出的每一行都与每种格式匹配,直到一行匹配成功

如果没有匹配的格式,该行将按原样添加到快速修复列表中,就像您的情况一样。它将只是文本,Vim不知道如何处理该行

默认情况下,我的BSD grep版本会为
grep-R
返回类似
file:message
的行。因此,采用第一种格式的
%f:%l:%m
,行号丢失。查看手册页,这就是
-n
标志的作用

因此,请尝试以下方法:

set grepprg=grep\ -nR\ --exclude=*~\ --exclude=*.swp\ --exclude-dir=vendor\ --exclude-dir=node_modules\ --exclude=tags\ --exclude=tags.vendor
之后,
:沉默的grepfoo.|copen
应按预期工作


(Psssh,
-n
标志也在
:h'grepprg'
中提到:)

工作得很好!