Opening search results with Vim ONLY if they exist

2

:) In my never-ending effort to get lazier, I am looking for a way to improve my "Open Grep/Ack-results in Vim"-snippet. What I'm currently using to open found files is:

vim $(ack -il "pattern")

Using it is a blaze, but it involves one catch - Vim will open regardless if the number of files found is 0 (in other words, Vim opens with nothing loaded into the buffer). Is there any elegant way to avoid this completely insignificant problem?

Thanks in advance

krystah

Posted 2013-09-19T13:12:36.480

Reputation: 1 337

Answers

2

How about this?

files=$(ack -il "pattern") && vim $files

(Tested with grep since I don't have ack.) Note that that won't work if ack returns files with spaces in their names.

garyjohn

Posted 2013-09-19T13:12:36.480

Reputation: 29 085

Both solutions provided are fine options, setting this one as the accepted one as it is shorter :) Thanks both! – krystah – 2013-09-22T17:37:52.863

4

How about immediately quitting Vim when no arguments have been passed:

vim -c 'if !argc()|quit|endif' $(ack -il "pattern")

Ingo Karkat

Posted 2013-09-19T13:12:36.480

Reputation: 19 513