This is another one of those pesky Vim posts I do from time to time. Today’s one is about a feature that I rarely paid much attention to, until it started driving me crazy: Swap files.
Vim’s swap files are, by default, created with the .swp extension right next to the file you opened. This is usually convenient, and, to be honest, I never paid much attention to them; just had to make sure I excluded them from source control (either manually or using .xxxignore files).
However, if Vim closes unexpectedly for whatever reason, the swap file would be left behind, which would prompt the infamous “Swap file xxx already exists” error message, and ask you what you wanted to do.
That’s only a mild annoyance most of the time. However, depending on your choices and how often it happens, Vim might start creating additional swap files with slightly different extensions, like .swo and .swn, if I remember correctly.
And then, one day, you realize you have 3 or 4 old swap files laying around cluttering your file system. Not good.
I started seeing this after my desktop machine started throwing BSODs regularly thanks to the x64 NVIDIA driver, which isn’t exactly the most reliable piece of software ever. I usually have at least one Vim instance opened, most of the time with 4 or 5 documents in it, so that meant… a lot of swap files left behind.
Finally got annoyed enough to do something about it, and after checking the help for swap-file, I realized I could easily get these of my face and someplace I didn’t care about. This is what I added to my .vimrc file:
if has("win32") || has("win64")
It just causes Vim to write swap files to my temporary directory. The unix option of using the root tmp dir instead of my own tmp dir in my home directory is probably not a good one, but I’m the only user of my linux machines, anyway, so it’s not a big deal.
Other than that, it works great and helps me keep my sanity.