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.

Kyle F Hartzenberg
  • 2,567
  • 3
  • 6
  • 24
Nadia Alramli
  • 111,714
  • 37
  • 173
  • 152

10 Answers10

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

puk
  • 16,318
  • 29
  • 119
  • 199
dwc
  • 24,196
  • 7
  • 44
  • 55
  • 12
    Swap files are good if your editor crashes though (power outage etc.), just keep in mind. – koonse Mar 03 '15 at 02: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 – webbertiger Sep 14 '16 at 20:43
  • [trusktr's answer](https://stackoverflow.com/a/15317146/712526) allows you to keep the advantages of swap files while addressing the OP's concern. – jpaugh Aug 14 '17 at 15:34
  • 1
    From inside vim you can also disable the swapfile while opening a file: ``:noswapfile edit /path/to/file`` – Torkel Bjørnson-Langen Jul 08 '18 at 13:19
  • My personal philosophy about data loss is DBAB and save often... – Samie Bencherif Oct 30 '19 at 19:00
  • if you are looking for a `lua` way of doing for your neovim config: put a `vim.cmd("noswapfile")` in any of your configs. – rtviii Dec 25 '21 at 21:54
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.

Florian Sesser
  • 5,972
  • 1
  • 25
  • 26
trusktr
  • 44,284
  • 53
  • 191
  • 263
  • You could also add that some time the saved backup files might save a devs skin – Bartłomiej Skwira Sep 04 '14 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 04 '15 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 04 '15 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 '15 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 '15 at 19:23
  • @ChieltenBrinke Sure, something like this: https://github.com/trusktr/.vimrc/blob/41ffd5005f3ea8fe2de282835eab825181e4ea84/.vimrc#L28-L41 – trusktr Aug 19 '20 at 22:20
  • @trusktr Thank you, now the .swp files are being stored in that given directory. However, I still keep getting the ".swp file exists" error every time I open a file that has crashed. Is there a way to let the .swp files be created (useful in a sudden crash) BUT to get that warning only once? – Sandun Dec 23 '20 at 12:18
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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
markus_b
  • 4,433
  • 2
  • 19
  • 15
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
cpjolicoeur
  • 12,766
  • 7
  • 48
  • 59
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
Jez
  • 27,951
  • 32
  • 136
  • 233
  • 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) – Domingo Ignacio Nov 15 '12 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. – joeytwiddle Jun 15 '13 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. – leetNightshade Apr 02 '14 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 '16 at 21:16
  • @joeytwiddle Vim's swapfile has helped me recover data multiple times. I do not remember any times I have been unable to recover the data. Could the recovery process be more user friendly? Probably, but I think using words like "awful" and "suck terribly" is unfounded. – Torkel Bjørnson-Langen Jul 08 '18 at 13: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](https://github.com/joeytwiddle/rc_files/blob/master/.vim/plugin/noswapsuck.vim) – joeytwiddle Jul 08 '18 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/
monokrome
  • 1,377
  • 14
  • 21
  • 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. – Samuel O'Malley Jun 18 '15 at 06:36
  • Thanks, @SamuelO'Malley. What does the // do? – monokrome Dec 14 '19 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`. – Samuel O'Malley Dec 15 '19 at 21:54
4

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

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Evan Hu
  • 977
  • 1
  • 13
  • 18
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.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
2

create no vim swap file just for a particular file

autocmd bufenter  c:/aaa/Dropbox/TapNote/Todo.txt :set noswapfile
zzapper
  • 4,743
  • 5
  • 48
  • 45
  • 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 03 '17 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).

valk
  • 9,363
  • 12
  • 59
  • 79