104

When I edit files, Vim tends to create files named .filename.swp, which I find annoying. From vim -h, I see that the -n option suppresses the creation of swap files.

Is there a vimrc directive that I can use to disable swap files by default?

What is the purpose of swap files, and what functionality would I be missing out on by suppressing them?

1
  • FWIW, I find swap files annoying as well, and for a while I tried using 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. Oct 16, 2015 at 19:01

2 Answers 2

126

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.

7
  • 5
    What the benefit of swap files over persistent-undo now that it has been in vim for a while?
    – alpha_989
    Feb 1, 2018 at 3:48
  • I use vim-auto-save. Doesn't this deprecate the need for swap files?
    – Kevin
    Dec 6, 2018 at 20:45
  • @Kevin It depends. If the plugin is causing a save in every scenario that Vim would write out the swapfile, then probably. Otherwise there will still be periods where the buffer isn't saved and a swapfile can help.
    – jamessan
    Dec 6, 2018 at 21:23
  • 1
    @alpha_989 They're complementary. A swapfile allows you to recover unsaved changes. The persistent-undo file allows you to traverse undo history across Vim sessions, but the file is only written when you save.
    – jamessan
    Dec 6, 2018 at 21:24
  • 1
    Any method of setting vim no swap for one instance only? e.g. vim temp.txt --some-command-that-disable-swap ? My use case is some watcher will keep watching for any change in the directory, so only by then I would want to disable swap to prevent collision. Oct 14, 2019 at 2:59
2

One great way to avoid creating annoying swap files is to open files you don't intend to change with the -R flag. This makes the file read-only and has a nice side effect of not creating a swap file. If you later decide to edit the file, you can just :set noreadonly.

Interestingly, if you turn off readonly, the swap file then isn't actually created until you then make a change.

1
  • 2
    Most (but not all) installations of vim come with view which is precisely equivalent to vim -R (and it's less to type)
    – D. Ben Knoble
    Mar 17, 2022 at 17:52

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.