I made a plugin called "noswapsuck" that only enables the swapfile when the buffer contains unsaved changes. Once changes have been saved, the swapfile is cleared. Hence, swapfiles which contain the same content as the file on disk will be removed.
Get it here: noswapsuck.vim
It has been working well for me, but I have never publicised it before, so I would welcome feedback.
Advantages:
- The only swapfiles that remain on your disk will be important swapfiles that actually differ from the file!
Disadvantages:
If the buffer has a swapfile, it will not be detected when the file is first opened. It will only be detected when swapfile
is enabled, which is when you start to edit the buffer. That is annoyingly late, and will interrupt you. (Solved: We now check for a pre-existing swapfile when a buffer is opened, by temporarily turning the swapfile
option on again.)
If you are working in an environment where you want to minimise disk-writes (e.g. low power, or files mounted over a network, or editing a huge file) then it is not ideal to keep removing and re-creating the swap file on every save and edit. In such situations, you can do:
:let g:NoSwapSuck_CloseSwapfileOnWrite = 0
which will keep the swapfile after a write, but will still remove it when the buffer loses focus.
By the way, I have another little plugin :DiffAgainstFileOnDisk
which can be pretty useful after hitting (r)ecover
, to check if the buffer you recovered is newer or older than the existing file, or identical to it.