3

I'm not sure it's even possible, but I'd like to force vim to quit if reading from stdin gave empty data (0 bytes).

The reason is when I'm running commands that may output something containing filesystem paths (e.g. grep(1) or find(1)) I redirect the output to | view - so be able to quickly jump to a particular file with gF. Now when the content is empty it make sense to not to start vim at all. How can I do that?

An alternative solution would be to put a mediator program that would connect its stdin and stdout when the content is non-empty, and that would exit otherwise, thus interrupting the pipeline.

1 Answer 1

4

Here is a "vim only" solution:

echo '' | vim - +'exe !search(".")?"quit!":""'

It is not perfect because a) doesn't distinguish between empty (0 bytes) and newline only b) the screen flash and c) the unavoidable message Vim: Reading from stdin.... The "intermediary" solution is a bit cleaner. Assuming you have the utility ifne (provided by moreutils):

echo -n '' | ifne vim -

echo -n '' just simulates the output of your command for this demo- replace with your actual work.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.