6

I use Vim to edit the output of shell commands all the time. Appending the dash in this line is second nature:

$ ./some-command | vim -

But every once in a while I get distracted and forget:

$ ./some-command | vim

This typically causes Vim to hang, pegs one core at 100% CPU, and may disrupt the terminal. Is there a way to fool-proof Vim for me?

  • 4
    Create a shell script called vim that checks for at least one argument before invoking the real vim with the arguments. – chicks Aug 21 '17 at 0:24
5

As @chicks pointed out, you can place this script in your PATH:

#!/usr/bin/env bash

if [[ "$#" -ne 0 ]] || [[ -t 0 ]]; then
    exec vim "$@"
else
    exec vim -
fi

I am using a variant of this script in ~/bin/e to open the current directory (vim .) when no argument is provided.

I guess you could also use a shell function:

v() { if [[ "$#" -ne 0 ]] || [[ -t 0 ]]; then vim "$@"; else vim -; fi; } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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