Why does Vim save files with a ~ extension?
Solution 1:
I think the better solution is to place these lines in your vimrc file
set backupdir=~/vimtmp//,.
set directory=~/vimtmp//,.
The first line is for backup files, the second line for swap files. The double slash at the end ensures that there is no conflict in case of two files having the same name, see comments (at the time of this edit this option is only honored for swap files, not yet for backup files). The ,.
allow vim
to use the current directory if the former doesn't exist.
You have to create a directory in your home directory called vimtmp for this to work. Also, check that backups are enabled in your config (add set backup
if not).
That way you get the benefit of both worlds, you don't have to see the files, but if something does get futzed you can go get your backup file from vimtmp. Don't forget to clean the directory out every now and then.
Solution 2:
The *.ext~
file is a backup file, containing the file as it was before you edited it.
The *.ext.swp
file is the swap file, which serves as a lock file and contains the undo/redo history as well as any other internal info Vim needs. In case of a crash you can re-open your file and Vim will restore its previous state from the swap file (which I find helpful, so I don't switch it off).
To switch off automatic creation of backup files, use (in your vimrc):
set nobackup
set nowritebackup
Where nowritebackup
changes the default "save" behavior of Vim, which is:
- write buffer to new file
- delete the original file
- rename the new file
and makes Vim write the buffer to the original file (resulting in the risk of destroying it in case of an I/O error). But you prevent "jumping files" on the Windows desktop with it, which is the primary reason for me to have nowritebackup
in place.
Solution 3:
To turn off those files, just add these lines to .vimrc (vim configuration file on unix based OS):
set nobackup #no backup files
set nowritebackup #only in case you don't want a backup file while editing
set noswapfile #no swap files
Solution 4:
And you can also set a different backup extension and where to save those backup (I prefer ~/.vimbackups
on linux). I used to use "versioned" backups, via:
au BufWritePre * let &bex = '-' . strftime("%Y%m%d-%H%M%S") . '.vimbackup'
This sets a dynamic backup extension (ORIGINALFILENAME-YYYYMMDD-HHMMSS.vimbackup
).
Solution 5:
:set nobackup
will turn off backups. You can also set a backupdir if you still want those backup files but in a central folder. This way your working dir is not littered with ~ files.
You find more information on backups under :he backup.