Reasons for swap files
Swap files store changes you've made to the buffer. If Vim or your computer crashes, they allow you to recover those changes.
Swap files also provide a way to avoid multiple instances of Vim from editing the same file. This can be useful on multi-user systems or just to know if you have another Vim already editing a file.
Disabling swap files
If you want to disable creating swap files entirely (which I wouldn't recommend), you can add set noswapfile
to your .vimrc
. This sets the global value of the option. You can then override that for specific buffers by using :setlocal swapfile
while running Vim.
Organizing swap files
If your issue is more about the extra files spread around your filesystem, then you could aggregate all the swap files in one place. For example, create a ~/.vim/tmp
directory and then add
set directory^=$HOME/.vim/tmp//
to your .vimrc
. The 'directory'
option contains a list of directories which Vim will use to try and store the swap file.
The ^=
syntax for :set
prepends the directory name to the head of the list, so Vim will check that directory first.
The //
at the end of the directory name tells Vim to use the absolute path to the file to create the swap file so there aren't collisions between files of the same name from different directories.
Note that this will prevent Vim from noticing when there are multiple users trying to edit the same file. Since the swap file isn't in the same directory as the original file, the other user's Vim won't know about the swap file and won't warn them that it's already being edited.
noswapfile
as suggested below. I eventually gave in and turned that off, though, because apparently the benefits outweigh the annoyances--buuuuuut I don't actually remember what convinced me to restore swap file usage. Probably my computer crashed and I was unable to restore some files that I'd lost, which would have been trivial with swap files turned on.