views:

302

answers:

2

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)

+3  A: 

Well, this works for me:

:args `grep -Rl "hello" *`

Running vim 7.0.305.

chaos
thanks, I tried using backticks, but vim doesn't recognized them - instead it interprets "`grep" as a filename. Have you got your answer to work for you? If so, I wonder why it doesn't work for me.
13ren
Yeah, worked fine for me. Maybe a version thing. I'm using 7.0.305.
chaos
hmmm, :version gives me:VIM - Vi IMproved 7.0 (2006 May 7, compiled Jan 31 2007 18:15:57)Included patches: 1-122
13ren
OK, I upgraded to version 7.1.314, but same problem. I'm cut-and-pasting your code, so it's not a typo. Maybe it's a compiler option, or a setting. But there's so many, it would be hard to know which one.
13ren
I'm not finding anything linking backtick expansion to compile options. There is something about backtick expansion not working in restricted mode (-Z option at startup), but I don't know why you'd be running under that.
chaos
Restricted mode doesn't produce the same results you're getting anyway; it generates a not-allowed error.
chaos
I guess the question has to be asked: are you sure you're sending backticks? Could your client be screwing you up, sending something else -- those obnoxious Microsoft-beloved left quote characters or something?
chaos
thanks for that checking - much appreciated! :-). Experimenting a bit, it seems that it might be the quotes around hello. This works: :args `grep -Rl hello *`--- though it turns out it's best not done from one's home dir (what with that -R :-)
13ren
YES. Escaping the quotes also works: :args `grep -Rl \"hello\" *` so I think that was definitely the problem. Yay, thanks so much, it is really cool to get this working :D
13ren
" starts a comment in Vimscript, which makes using it in Vim commands a bit tricky...
ephemient
Ahh, interesting. Wonder why it messes up for 13ren but not for me.
chaos
+1  A: 

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
Bill Karwin
thanks, I tried this, but "grep" is interpreted as a filename...
13ren
It's version 7, but not the very latest. I'm checking for a new version now.
13ren
I just upgraded to version 7.1.314, but same problem. I wonder what the problem is.
13ren
Hi again, it seems it was the quotes around hello - it works fine without them, or if they are escaped. :-)
13ren
Ok, I'm glad you found the issue! Cheers.
Bill Karwin