Nadia Alramli
Nadia Alramli

Reputation: 115011

Disabling swap file creation in Vim

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.

Upvotes: 261

Views: 144373

Answers (10)

Brian Rasmussen
Brian Rasmussen

Reputation: 116481

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 or use :help directory within vim.

Upvotes: 3

dwc
dwc

Reputation: 24938

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

Upvotes: 308

markus_b
markus_b

Reputation: 4623

I found the answer here:

vim -n <file>

opens file without swapfile.

In addition:

set dir=/tmp

in .vimrc creates the swapfiles in /tmp.

Upvotes: 52

Evan Hu
Evan Hu

Reputation: 1017

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

Upvotes: 4

zzapper
zzapper

Reputation: 5041

create no vim swap file just for a particular file

autocmd bufenter  c:/aaa/Dropbox/TapNote/Todo.txt :set noswapfile

Upvotes: 2

valk
valk

Reputation: 9894

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).

Upvotes: 1

trusktr
trusktr

Reputation: 45504

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.

Upvotes: 184

Jez
Jez

Reputation: 30071

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

Upvotes: 12

monokrome
monokrome

Reputation: 1376

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/

Upvotes: 7

cpjolicoeur
cpjolicoeur

Reputation: 13106

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

Upvotes: 28

Related Questions