in vim, how to set “args” to the result of a “grep -l”?

为君一笑 提交于 2020-02-19 09:46:53

问题


To illustrate, here's how to do it from the command-line:

vim `grep "hello" * -Rl`

This opens vim with all the files that have "hello" in them (-l gives the filenames alone). I want to do the same thing, but from within vim. Conceptually, something like this (which doesn't work):

:args !grep "hello" * -Rl

I'm open to completely different approaches to achieve this; I'd just like it to be on one line (so it's easy to edit and redo).


The answer is to simply use backticks - but with a key proviso! The below does not work for me, because of the quotes around hello:

:args `grep "hello" * -Rl`

But it works if I remove them or escape the quotes:

:args `grep hello * -Rl`
:args `grep \"hello\" * -Rl`

(this was buried in the comments after chaos' answer - I added them here to make them more visible, in case anyone else had this problem)


回答1:


Well, this works for me:

:args `grep -Rl "hello" *`

Running vim 7.0.305.




回答2:


Try the args command:

:ar[gs] `grep -Rl "hello" .`

If the backticks aren't working for you, are you use you're using a current version of vim?

:version


来源:https://stackoverflow.com/questions/1033524/in-vim-how-to-set-args-to-the-result-of-a-grep-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!