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?

1
  • 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, 2017 at 0:24

1 Answer 1

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; } 
0

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.