224

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?

Ross
  • 46,186
  • 39
  • 120
  • 173
  • Do you have any file open simultaneously in more than one place, they will create a `.ext.swp`. – dirkgently Mar 03 '09 at 18:00
  • 2
    dirkgently: No, on here I can open a file in the only vim window and it will create the .swp file. I'm not too fussed about that as it's removed when I save/close the window. – Ross Mar 03 '09 at 18:02
  • Related post - [Why is vim leaving temporary file versions all over the place?](https://superuser.com/q/730145/374397) – RBT Mar 20 '19 at 10:33

9 Answers9

255

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.

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76
user183135
  • 3,095
  • 2
  • 18
  • 7
  • 27
    +1 I actually have mine set to ~/.vim/tmp, but either way, this has saved my ass on more than one occasion. – J.C. Yamokoski Jun 06 '11 at 19:01
  • 12
    Why do you need ,. at the end of each line? – shin Jan 30 '14 at 01:07
  • 10
    @shin Vim will use the first available dir, so in this case if ~/vimtmp doesn't exist it will use the current working directory – xixixao Jan 30 '14 at 21:58
  • This should be the default geez such a huge security hole when editing CMS config files. – Samuel Katz Mar 30 '14 at 13:53
  • 3
    @SalmanPK In an ideal world you should not be editing configuration files directly on publicly accessible servers – Score_Under Aug 20 '14 at 02:31
  • 4
    To also remove .un~ files you can add: "set undodir=~/vimtmp,." – Josh Apr 08 '15 at 00:20
  • 2
    What happens if I edit `/foo/bar/a.txt` and `/foo/baz/a.txt`? As the basename is the same, will vim write two different backup file contents in the same backup file like `~/vimtmp/a.txt~`? – Armen Michaeli May 19 '15 at 09:16
  • 12
    Use two slashes at the end of the path. For instance, set backupdir=~/.vim/.backup// - the "//" at the end of the directory means that file names will be built from the complete path to the file with all path separators substituted to percent "%" sign. This will ensure file name uniqueness in the preserve directory. – Hacknightly Jun 12 '16 at 17:58
  • 2
    @Hacknightly, your answer should be included in the above. – therealjumbo Jul 14 '16 at 20:58
  • 2
    @Hacknightly there is a known [issue](https://stackoverflow.com/questions/26742313/why-vim-backup-filenames-are-not-correct-backupdir-option-not-performing-as-e) that currently prevents this solution from working. – NougatRillettes Jul 15 '16 at 03:59
  • And do not forget to also add `set backup` into your vimrc in order to get backups ON. – Hudson Santos Apr 20 '17 at 00:00
  • 1
    I prefer using the $TMP or $TEMP directories in the case where you're setting up a new environment, then you know that those directories exist already usually as /tmp. – solstice333 Sep 17 '17 at 03:03
245

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.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 9
    +1 For `you prevent "jumping files" on the Windows desktop`. Googled and found this answer – Merlyn Morgan-Graham Mar 28 '11 at 22:16
  • 2
    Actually - do I have to have both `nobackup` and `nowritebackup`? Is there a way to write a copy of the file as a backup, but not do this write/delete/rename stuff? – Merlyn Morgan-Graham Mar 28 '11 at 22:22
  • @Merlyn: If you have `backup` (as opposed to `nobackup`), then Vim will create a backup (the `*.ext~` file). This is completely unrelated to `nowritebackup`. – Tomalak Mar 28 '11 at 22:26
  • 6
    There is a table describing different behavior between combination of those switches: `help backup-table`. It turns out setting neither `nobackup` nor `nowritebackup`, but instead setting `backupcopy=yes` also solves the "jumping" problem. This may hurt perf, though, so to each their own – Merlyn Morgan-Graham Mar 28 '11 at 22:34
  • 34
    Instead of turning off , backup to a directory `set backupdir=~/.vim/backup` in .vimrc or _vimrc on windows – tsukimi Dec 05 '13 at 01:10
  • edit: make sure you have removed all instances of `set backup` in your vimrc or this wont work – luxon Mar 02 '17 at 07:05
28

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
rogeriopvl
  • 51,659
  • 8
  • 55
  • 58
15

Put this line into your vimrc:

set nobk nowb noswf noudf " nobackup nowritebackup noswapfile noundofile


In 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 as well :-)

  • Thank you so much for this! "noundofile" was the option that *finally* got rid of that god-awful litter. (And I agree 100% with everything else you've written in your post!) – pwmusic Sep 23 '19 at 09:32
  • @pwmusic This undo file must be a recent thing. On older systems with older versions of Vim, I never saw any *.un~ files, but on my Windows 10 system I had to add `noundofile` to my _vimrc to get rid of the *.un~ files. – pacoverflow Sep 13 '21 at 07:49
  • Works for me. Also "noundofile" is key that few others mention. – Penghe Geng Oct 06 '22 at 01:55
14
: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.

f3lix
  • 29,500
  • 10
  • 66
  • 86
14

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

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
  • 3
    you might want to include how to set up the back up directory, i.e. putting `set backupdir=~/.vimbackups` in your ~/.vimrc – rampion Mar 03 '09 at 22:30
  • 1
    The vim help fully describes this technique. See `:help backupext` – netjeff Jun 09 '11 at 19:05
6

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

dwc
  • 24,196
  • 7
  • 44
  • 55
5

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)

Logan
  • 126
  • 1
  • 6
  • This finally worked for me. Why isn't this mentioned in other answers? This removes the: `..un~` files while the other options only remove the `~` files. – JLT Jan 09 '20 at 14:42
0

I had to add set noundofile to ~_gvimrc

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

Josh
  • 2,142
  • 2
  • 23
  • 20