256

Is there a way to disable the creation of .swp files in Vim? Or, at least create them in one place so that I can find and delete them easily?

I find swap files especially annoying when I copy the parent directory while editing at the same time. Of course I know that I can use find -exec to find and delete them, but I want a more practical solution.

0

10 Answers 10

301

To disable swap files from within vim, type

:set noswapfile

To disable swap files permanently, add the below to your ~/.vimrc file

set noswapfile

For more details see the Vim docs on swapfile

6
  • 12
    Swap files are good if your editor crashes though (power outage etc.), just keep in mind.
    – koonse
    Mar 3, 2015 at 2:52
  • 4
    Another option is still use swap file, but leave it in memory, so it won't keep writing hard disk. This can't protect you from losing the file during an outage, but if you open the same file in anther vim, it will tell you immediately. The command is: set directory=/dev/shm Sep 14, 2016 at 20:43
  • trusktr's answer allows you to keep the advantages of swap files while addressing the OP's concern.
    – jpaugh
    Aug 14, 2017 at 15:34
  • 1
    From inside vim you can also disable the swapfile while opening a file: :noswapfile edit /path/to/file Jul 8, 2018 at 13:19
  • My personal philosophy about data loss is DBAB and save often... Oct 30, 2019 at 19:00
183

Set the following variables in .vimrc or /etc/vimrc to make vim put swap, backup and undo files in a special location instead of the working directory of the file being edited:

set backupdir=~/.vim/backup//
set directory=~/.vim/swap//
set undodir=~/.vim/undo//

Using double trailing slashes in the path tells vim to enable a feature where it avoids name collisions. For example, if you edit a file in one location and another file in another location and both files have the same name, you don't want a name collision to occur in ~/.vim/swap/. If you specify ~/.vim/swap// with two trailing slashes vim will create swap files using the whole path of the files being edited to avoid collisions (slashes in the file's path will be replaced by percent symbol %).

For example, if you edit /path/one/foobar.txt and /path/two/foobar.txt, then you will see two swap files in ~/.vim/swap/ that are named %path%one%foobar.txt and %path%two%foobar.txt, respectively.

7
  • You could also add that some time the saved backup files might save a devs skin Sep 4, 2014 at 11:21
  • Comment: (not criticism), That seems good mainly for development because for reading files and small edits or unimportant note keeping which is a large part of vim use for a lot of people, it seems like an extra bookeeping job better left out. I personally prefer for all jobs to just have no swaps or other auto-backups. If I really need to develop something, I'd rather have a more high level framework to protect me, preferably with code version control etc.
    – j riv
    Jun 4, 2015 at 15:22
  • @LelaDax True, most of the time these files are mostly useless because I use git for all my source code. In rare occasions (like a crash) they have come in handy though. But actually, the undo history I use a lot. It's much easier to navigate that quickly than checking out git commits to view previous history. gundo makes it nice and easy to navigate the undo tree. I could live without the swap and backup files though.
    – trusktr
    Jun 4, 2015 at 18:08
  • 36
    Just FYI for people who are new (like me haha...) you do you have to do go to ~/.vim/ and make the directories yourself. It won't auto create them for you.
    – aug
    Sep 25, 2015 at 18:00
  • 1
    You could also write some code in your vimrc to automatically create the folders for you, which is what I've done in mine. So, on new systems, I just get my vimrc, and it takes care of the rest.
    – trusktr
    Oct 13, 2015 at 19:23
50

I found the answer here:

vim -n <file>

opens file without swapfile.

In addition:

set dir=/tmp

in .vimrc creates the swapfiles in /tmp.

2
  • What about the *.ext~ backup files? Is there any command line option to disable them as well? Feb 20, 2016 at 20:55
  • That's very useful for those one-off huge files! Thanks. Apr 1, 2016 at 5:56
28

here are my personal ~/.vimrc backup settings

" backup to ~/.tmp 
set backup 
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp 
set backupskip=/tmp/*,/private/tmp/* 
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp 
set writebackup
13

I agree with those who question why vim needs all this 'disaster recovery' stuff when no other text editors bother with it. I don't want vim creating ANY extra files in the edited file's directory when I'm editing it, thank you very much. To that end, I have this in my _vimrc to disable swap files, and move irritating 'backup' files to the Temp dir:

" Uncomment below to prevent 'tilde backup files' (eg. myfile.txt~) from being created
"set nobackup

" Uncomment below to cause 'tilde backup files' to be created in a different dir so as not to clutter up the current file's directory (probably a better idea than disabling them altogether)
set backupdir=C:\Windows\Temp

" Uncomment below to disable 'swap files' (eg. .myfile.txt.swp) from being created
set noswapfile
6
  • 10
    I think the fact that other editors don't bother with it reflects poorly on the other editors more than it reflects poorly upon vi(m). The choice to make the-same-directory the default place to put these files is a bit unfortunate, though at least marginally defensible. (eg, In a multiserver, highly cross-mounted environment, it may not be obvious where to find a recovery file in the moment when you need it most urgently) Nov 15, 2012 at 17:43
  • 4
    Vim's swapfile handling is just awful. I have used it to recover lost data maybe 5 times. But I have been hassled to recover a swapfile about 9,000 times, which is invariably identical to the file anyway, or worse an out-of-date version. It might be a good feature if the recovery process didn't suck so terribly. recover.vim helps a little, but is still too interactive. Jun 15, 2013 at 21:13
  • 3
    Sublime Text has disaster recovery. I love it! I don't even have to save a file, when I resume any changes I made, saved or not, are pulled back up. Apr 2, 2014 at 22:27
  • 1
    I have a simple autocommand that writes all changes as soon as they are made, making both swapfiles and backups useless.
    – tbodt
    Dec 30, 2016 at 21:16
  • 2
    @TorkelBjørnson-Langen It's probably my fault for rebooting my machine without closing all my Vim sessions, and then being paranoid about which recover files are actually valid. Anyway I tried to get the best of both worlds, by writing a plugin that only maintains a swapfile when there are unsaved edits: vim-swap-is-awesome.vim Jul 8, 2018 at 17:36
7

You can set backupdir and directory to null in order to completely disable your swap files, but it is generally recommended to simply put them in a centralized directory. Vim takes care of making sure that there aren't name collissions or anything like that; so, this is a completely safe alternative:

set backupdir=~/.vim/backup/
set directory=~/.vim/backup/
3
  • 3
    You should add a double slash // to the end of the paths so that you can modify two files with the same name at the same time. Jun 18, 2015 at 6:36
  • Thanks, @SamuelO'Malley. What does the // do?
    – monokrome
    Dec 14, 2019 at 15:08
  • Have a look at :help directory if you want to read the docs for this. Double slash tells vim to encode the full path of the file into the swap file's name, e.g. %home%sam%dotfiles%.gitignore.swp, rather than just .gitignore.swp. Dec 15, 2019 at 21:54
4

If you are using git, you can add *.swp to .gitignore.

1
  • I think that's what the most users need! Turning off of swap files is bad practice. May 13, 2016 at 13:25
3

If you put set directory="" in your exrc file, you will turn off the swap file. However, doing so will disable recovery.

More info here.

2

create no vim swap file just for a particular file

autocmd bufenter  c:/aaa/Dropbox/TapNote/Todo.txt :set noswapfile
1
  • 1
    Or for all files in a particular directory, the following seems to work (only brief testing): autocmd bufenter c:/aaa/Dropbox/TapNote/* :set noswapfile
    – scottkosty
    Apr 3, 2017 at 18:18
1

For anyone trying to set this for Rails projects, add

set directory=tmp,/tmp

into your

~/.vimrc

So the .swp files will be in their natural location - the tmp directory (per project).

Your Answer

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

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