来自Vim内的Gre((带管道)

gdw*_*dw2 5 vim grep

我想创建一个'quickfix'列表(请参阅参考资料:help quickfix),其中包含所有包含"abc"但不包含"xyz"的行的文件.我希望我可以运行以下vim ex命令:

:grep -nHr abc * | grep -v xyz
Run Code Online (Sandbox Code Playgroud)

不幸的是,vim不喜欢"管道",命令失败.在vim中执行此操作的最佳方法是什么?

小智 5

很多年过去了,但我找到了更直接的替代方案。cexprvim 中的命令。它几乎实现了 @jwd 和 @skeept 的答案,并且更加灵活,因为它接受列表。

下面是我自己使用管道的示例。我在工作笔记中为所有问题添加前缀,以便Q:快速找到它们。我想使用quickfix list和vim来浏览问题,首先查看最近的问题。

:cexpr system("grep -R --line-number Q: --exclude '*~' ~/_Notes/work-log/ \| sort \| tac")
Run Code Online (Sandbox Code Playgroud)

相关摘自vim帮助。

:cex[pr][!] {expr}  Create a quickfix list using the result of {expr} and
            jump to the first error.
            If {expr} is a String, then each new-line terminated
            line in the String is processed using the global value
            of 'errorformat' and the result is added to the
            quickfix list.
            If {expr} is a List, then each String item in the list
            is processed and added to the quickfix list.  Non
            String items in the List are ignored.
            See |:cc| for [!].
            Examples: >
                :cexpr system('grep -n xyz *')

Run Code Online (Sandbox Code Playgroud)


jwd*_*jwd 4

出于某种原因,我不能放过这个!

怎么样使用:!grep ... > filename后跟:cf filename,这将打开输出作为快速修复列表。