Mac – VIM – leaves .swp files after crash

buffermacvimvim

I use MacVim (snapshot 51) to edit a the Python/HTML/etc files in moderately sized project. Every few months my system locks up or crashes, and Vim leaves a plethora of .*.swp files around. When I go to re-open Vim after the crash and edit any of those files, I am told "Swap file ''.xyz.swp'' already exists!" with the options of [Abort] [Quit] [Delete it] [Recover] [Edit anyway] and [Open Read-Only].

This recovery option would make sense if there were changes to the Vim buffer that hadn't been saved at the time of the crash. However, at the time of the crash these files were opened in the background of Vim, and unchanged.

If possible, how can I configure Vim to not ask this superfluous question when opening files for which there are no changes between the .swp file and the actual file, e.g. by one of:

  1. Automatically comparing the changes in the .swp file to the file, and if there are no changes then just opening the file without prompting;

  2. Not keeping a .swp file for files that have no changes.

  3. Any other means of systematically avoiding this pointless prompt (but I'd prefer not to avoid the prompt in cases where, of course, there were unsaved changes).

I have over fifty .swp files to clean up at the moment, and the pointless prompt is a frustration I'd be grateful to avoid.

Thank you for reading.

Brian

Best Answer

I haven't tested it, but what about this solution presented in vim's official wiki?

Swap file "..." already exists! - so diff it

Also in the situation where the recovered swapfile turns out to be identical to the real file, there is no need for the diffing.

#!/bin/bash
# Expects variables realfile, swapfile, recoveryfile.
vim -r "$swapfile" -c ":wq! $recoveryfile" && rm "$swapfile"
if cmp "$recoveryfile" "$realfile"
then rm "$recoveryfile"
else vimdiff "$recoveryfile" "$realfile"
fi

There's a script for this problem as well in Stackoverflow.

This will remove any swap files that are up-to-date with the real files. Any that don't match are brought up in a vimdiff window so I can merge in my unsaved changes.

Related Question