Disabling swap files creation in vim

VimEditor

Vim Problem Overview


Is there a way to disable .swp files creation in vim? or at least create them all in one place so I can find and delete them easily.

I find them 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.

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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.

Solution 3 - Vim

I found the answer here:

vim -n <file>

opens file without swapfile.

In addition:

set dir=/tmp

in .vimrc creates the swapfiles in /tmp.

Solution 4 - Vim

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

Solution 5 - Vim

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

Solution 6 - Vim

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/

Solution 7 - Vim

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

Solution 8 - Vim

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.

Solution 9 - Vim

create no vim swap file just for a particular file

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

Solution 10 - Vim

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNadia AlramliView Question on Stackoverflow
Solution 1 - VimdwcView Answer on Stackoverflow
Solution 2 - VimtrusktrView Answer on Stackoverflow
Solution 3 - Vimmarkus_bView Answer on Stackoverflow
Solution 4 - VimcpjolicoeurView Answer on Stackoverflow
Solution 5 - VimJezView Answer on Stackoverflow
Solution 6 - VimmonokromeView Answer on Stackoverflow
Solution 7 - VimEvan HuView Answer on Stackoverflow
Solution 8 - VimBrian RasmussenView Answer on Stackoverflow
Solution 9 - VimzzapperView Answer on Stackoverflow
Solution 10 - VimvalkView Answer on Stackoverflow