How to prevent vim from creating (and leaving) temporary files?

VimText EditorTemporary Files

Vim Problem Overview


Why does vim create <filename>~ files? Is there a way to disable that?

If it's for backup (or something), I use git for that.

Also, these .<filename.with.path.hints>.swp files too.

How do I tell vim not to create those, or at the least to cleanup after itself?

EDIT

whoops, duplicate: > https://stackoverflow.com/questions/607435/why-does-vim-save-files-with-a-extension

I adopted rogeriopvl's answer from there.

verbatim copy:

set nobackup       "no backup files
set nowritebackup  "only in case you don't want a backup file while editing
set noswapfile     "no swap files

Vim Solutions


Solution 1 - Vim

I'd strongly recommend to keep working with swap files (in case Vim crashes).

You can set the directory where the swap files are stored, so they don't clutter your normal directories:

set swapfile
set dir=~/tmp

See also

:help swap-file

Solution 2 - Vim

Put this in your .vimrc configuration file.

set nobackup

Solution 3 - Vim

; For Windows Users to back to temp directory

set backup
set backupdir=C:\WINDOWS\Temp
set backupskip=C:\WINDOWS\Temp\*
set directory=C:\WINDOWS\Temp
set writebackup

Solution 4 - Vim

On Windows add following lines to _vimrc

" store backup, undo, and swap files in temp directory
set directory=$HOME/temp//
set backupdir=$HOME/temp//
set undodir=$HOME/temp//

Solution 5 - Vim

This answer applies to using gVim on Windows 10. I cannot guarantee the same results for other operating systems.

Add:

set nobackup
set noswapfile
set noundofile

To your _vimrc file.

Note: This is the direct answer to the question (for Windows 10) and probably not the safest thing to do (read the other answers), but this is the fastest solution in my case.

Solution 6 - Vim

I made a plugin called "noswapsuck" that only enables the swapfile when the buffer contains unsaved changes. Once changes have been saved, the swapfile is cleared. Hence, swapfiles which contain the same content as the file on disk will be removed.

Get it here: noswapsuck.vim

It has been working well for me, but I have never publicised it before, so I would welcome feedback.

Advantages:

  • The only swapfiles that remain on your disk will be important swapfiles that actually differ from the file!

Disadvantages:

  • If the buffer has a swapfile, it will not be detected when the file is first opened. It will only be detected when swapfile is enabled, which is when you start to edit the buffer. That is annoyingly late, and will interrupt you. (Solved: We now check for a pre-existing swapfile when a buffer is opened, by temporarily turning the swapfile option on again.)

  • If you are working in an environment where you want to minimise disk-writes (e.g. low power, or files mounted over a network, or editing a huge file) then it is not ideal to keep removing and re-creating the swap file on every save and edit. In such situations, you can do:

      :let g:NoSwapSuck_CloseSwapfileOnWrite = 0
    

    which will keep the swapfile after a write, but will still remove it when the buffer loses focus.

By the way, I have another little plugin :DiffAgainstFileOnDisk which can be pretty useful after hitting (r)ecover, to check if the buffer you recovered is newer or older than the existing file, or identical to it.

Solution 7 - Vim

I have this setup in my Ubuntu .vimrc. I don't see any swap files in my project files.

set undofile
set undolevels=1000         " How many undos
set undoreload=10000        " number of lines to save for undo

set backup                        " enable backups
set swapfile                      " enable swaps
set undodir=$HOME/.vim/tmp/undo     " undo files
set backupdir=$HOME/.vim/tmp/backup " backups
set directory=$HOME/.vim/tmp/swap   " swap files

" Make those folders automatically if they don't already exist.
if !isdirectory(expand(&undodir))
	call mkdir(expand(&undodir), "p")
endif
if !isdirectory(expand(&backupdir))
	call mkdir(expand(&backupdir), "p")
endif
if !isdirectory(expand(&directory))
	call mkdir(expand(&directory), "p")
endif

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
QuestionhasenView Question on Stackoverflow
Solution 1 - Vimuser55400View Answer on Stackoverflow
Solution 2 - VimChad BirchView Answer on Stackoverflow
Solution 3 - VimSoorajView Answer on Stackoverflow
Solution 4 - VimVlad BezdenView Answer on Stackoverflow
Solution 5 - Vimwlwl2View Answer on Stackoverflow
Solution 6 - VimjoeytwiddleView Answer on Stackoverflow
Solution 7 - VimRazaView Answer on Stackoverflow