VIM - leaves .swp files after crash

Solution 1:

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.

Solution 2:

While not exactly what you want, this can mitigate the issue:

" Setup backup location and enable
set backup
set backupdir=$HOME/temp/vim_backups/       "where to put those backups
set directory=$HOME/temp/vim_swp/           "this is for swp files      

Create those directories, and vim with write the ~ and .swp files to a central location. Much easier to clean up this way!

Also, check out recover.vim - if a .swp file is detected, it will prompt to run a diff on the on-disk version and the swp, so you can see what's changed. Much more helpful this way IMO.