When I lose power, I have a lot of maybe-useful swap files (4 vim instances running now, 103 swap files currently in ~/.cache/vim/swap). I am fine with manual recovery of swapfiles with changes - I'd just like a way to either:

  1. delete all lock-only swapfiles post-reboot or other times I know there are no running vim instances
  2. minimize the number of lock-only swapfiles to 1 per open instance of vim

I opened an issue on Github to see if vim could manage 2 without any plugins, etc, but unfortunately it was not deemed worthy of support hours.

I request a solution to either 1 or 2 which:

  • is easy to remember - short, small, easy to copy-paste between computers
  • and is not instructions for the computer to download or call a special dependency - ie, no vim plugins, Java programs, etc. The entire solution needs to be short enough to match the above criterion. So, ie, 5 lines of python/perl/awk/vimscript/etc would be great.

Attempted solutions:

  • Haven't found a vim command-line flag to tell me if a swapfile is only a lockfile instead of a recovery file
  • Haven't found a vimscript instruction to store lock and recovery files separately.
  • Have read the source to 3-4 different vimscript plugins - one removes set hidden on buffer switch, another diffs swap files, another deletes swap files without regard for if they're lock files, etc. None (so far) are simple enough to be easily portable.
  • GitHub issue
  • rm ~/.cache/vim/swap/* - destroys recovery files along with the lock files

Have not yet tried:

  • reading the swap file format to see if there's something I can hook to w/eg grep
  • installing/testing various vimscript plugins - not very portable
  • setting read-only mode by default, then setting it again when I save a buffer. I'm looking for something I do at most once per boot, not on every file access - if every file access that's why we have software.
  • as the above, but w/o set hidden by default
  • I don't think this is possible with your requirements. For your attempted solutions: 1) is not possible, 2) not possible. You can try my plugin Recover which deletes unmodified swap files by default. You just need to install it. That's about as little manual work as possible. Oct 24, 2018 at 5:31
  • @ChristianBrabandt no. Your plugin is one of those in my second bullet point of "Have not yet tried". I appreciate the effort you've made to write and maintain it, but it is unfortunately not a good fit for my desires.
    – Iiridayn
    Oct 26, 2018 at 1:33
  • Perhaps this is of help: reddit.com/r/vim/comments/9sfjgj/… Oct 30, 2018 at 8:25

2 Answers 2


I've often found myself in a similar situation and, this past weekend, wrote the beginnings of a solution. Take a look at the Go library toolman.org/file/viminfo -- with this I'll be able to write a solution for your #1 (or similar), hopefully in the next week or so.

Stay tuned.

  • Your library looks pretty cool - should be better than the dd answer (less fragile), though it does use a downloaded dependency (in this case, a Go library). I'm looking forward to the results of your investigation :).
    – Iiridayn
    Oct 30, 2018 at 21:59

In fish syntax: for file in ~/.cache/vim/swap/*; [ (cat "$file" | dd ibs=1 skip=1007 count=1 2> /dev/null) != 'U' ]; and rm "$file"; end; - this works, but seems to me fragile - ie, if the vim swap file format changes, this would fail (and possibly lead to data loss equivalent to removing all swap files).

I didn't bother with this (in the "have not tried yet" section of my question) because I feel it is too risky/fragile, until a comment angered me enough to make it worth the bother to prove that it is, in fact, possible.

I believe there exist other, better solutions. Please help me find one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.