Stop vi Complaining

Whenever you try and open a file that’s already open in vi you receive the interminable message that ends:

Swap file “.la.swp” already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

You have to scroll to the end of the message and then choose an option. And I always choose to abort.

I wrote this little shell script that doesn’t force you to choose one of the options but just aborts with a short helpful message.

if [ -e ".$1.swp" ]
then
  echo "A swapfile for $1 exists.  Already open?"
else
  vi "$1"
fi

Then just alias vi or vim or whatever to the shell script and no more annoying message.

(You can do this within vim with a setting but I have found that to be less reliable and less useful. Plus this way you can use vi and get the old behaviour should you need it.)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.