Why does Vim save files with a ~ extension?

Vim

Vim Problem Overview


I've found that while using Vim on Windows Vim saves the file, a .ext.swp file that's deleted on closing the Vim window and a .ext~ file.

I assume the .ext.swp file is a session backup in case Vim crashes. What's the purpose of the .ext~ file however? Is this a permanent backup file? It's annoying as I'd like to copy all the files I'm working on to my host, without these duplicates. How can I turn this off or, if it's there for a good reason, hide the files?

Vim Solutions


Solution 1 - Vim

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 - Vim

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:

  1. write buffer to new file
  2. delete the original file
  3. 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 - Vim

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 - Vim

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 - Vim

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

Solution 6 - Vim

Put this line into your vimrc:

set nobk nowb noswf noudf " nobackup nowritebackup noswapfile noundofile


In [tag:windows] that would be the:

C:\Program Files (x86)\vim\_vimrc

file for system-wide vim configuration for all users.

Setting the last one noundofile is important in Windows to prevent the creation of *~ tilda files after editing.


I wish Vim had that line included by default. Nobody likes ugly directories.

Let the user choose if and how she wants to enable advanced backup/undo file features first.

This is the most annoying part of Vim.

The next step might be setting up:

set noeb vb t_vb= " errorbells visualbell

to disable beeping in [tag:vim] as well :-)

Solution 7 - Vim

You're correct that the .swp file is used by vim for locking and as a recovery file.

Try putting set nobackup in your vimrc if you don't want these files. See the Vim docs for various backup related options if you want the whole scoop, or want to have .bak files instead...

Solution 8 - Vim

The only option that worked for me was to put this line in my ~/.vimrc file

set noundofile

The other options referring to backup files did not prevent the creation of the temp files ending in ~ (tilde)

Solution 9 - Vim

I had to add set noundofile to ~_gvimrc

The "~" directory can be identified by changing the directory with the cd ~ command

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
QuestionRossView Question on Stackoverflow
Solution 1 - Vimuser183135View Answer on Stackoverflow
Solution 2 - VimTomalakView Answer on Stackoverflow
Solution 3 - VimrogeriopvlView Answer on Stackoverflow
Solution 4 - VimZsolt BotykaiView Answer on Stackoverflow
Solution 5 - Vimf3lixView Answer on Stackoverflow
Solution 6 - Vimuser4104817View Answer on Stackoverflow
Solution 7 - VimdwcView Answer on Stackoverflow
Solution 8 - VimLoganView Answer on Stackoverflow
Solution 9 - VimJoshView Answer on Stackoverflow