36

I use gvim on windows and I want to know a way to disable the temp file(ending in ~) file creation. Also is there a problem if we do it?

rcs
  • 67,191
  • 22
  • 172
  • 153
gameover
  • 11,813
  • 16
  • 59
  • 70
  • Possible duplicate of [Why does Vim save files with a ~ extension?](http://stackoverflow.com/questions/607435/why-does-vim-save-files-with-a-extension) – törzsmókus Dec 08 '15 at 18:49

5 Answers5

39

You can disable the backup file by putting this line in your .vimrc:

set nobackup

I almost always do this, as the ~ file is more annoying that useful. There is no problem with doing this, you'll just lose the ability to revert to a backup of the file.

If you want to get rid of the temporary .swp (swap) file too, you can also set this:

set noswapfile

The swap file is created when you have a file open, and provides some backup/recovery security, in case Vim crashes while editing a file. It also can prevent multiple Vims from editing the same file. I usually just turn this off too, because I rarely have a use for it. The .swp file isn't as annoying as the ~ file, because it goes away when you close Vim, but I still just turn that feature off.

Andy White
  • 86,444
  • 48
  • 176
  • 211
  • 3
    Excepting the fact that you have no chance of recovery in case of vim crashing, or (much more likely, depending on your environment) a severed terminal. – richo Feb 04 '10 at 06:26
  • 5
    Yeah, you do lose the backup capability, but I've almost never had a problem with Vim crashing or losing a connection, so I roll the dice. – Andy White Feb 04 '10 at 06:28
  • 1
    ... which is why you save early, and often. First rule of development. – Crates Nov 10 '14 at 15:02
  • @Crates Which is great.. until saving often means you just accidentally overwrote something important. Then you realize you were halfway up the undo stack and accidentally typed a letter losing all redo-able changes. Unlikely, but it's happened to me once or twice. Though admittedly, never in Vim. (Visual Studio likes to randomly discard the undo stack sometimes.. a most heinous bug). – Dan Bechard Dec 14 '15 at 19:16
28

It's not quite what you asked for, but something that I've found works well is to redirect the swap and backup files to a seperate, dedicated folder. That way, they're still there if I need them, but they're not cluttering up the folder I'm working in.

The _vimrc file can be created in any of the following locations:

  • %HOMEPATH%\_vimrc
  • C:\Program Files (x86)\Vim\_vimrc

The following lines in the _vimrc file put backup files into a temporary directory:

set backup
set dir=%TMP%
set backupdir=%TMP%
set directory=%TMP%
set noundofile

The last line prevents the proliferation of undo files.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Warren Pena
  • 562
  • 5
  • 8
  • 1
    This is the correct solution for Windows, all the others (like 'set nobackup') had no affect at all. – Gerry Jan 15 '19 at 19:05
7

put these in your vimrc file

set nobackup
set nowritebackup
set noswapfile
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
4

From inside vim:

:e $HOME/_vimrc

and add this to the file:

set nobackup

Then, $HOME/_vimrc~ will hopefully be the last backup that vim makes!

Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
0

You can also use the _vimrc from $VIM. The _vimrc file from your home dir will be loaded later and the last setting wins. Beware when using a common dir for backup, swap or undo files. If you are on an USB or network drive and a drive letter gets reused you may run into problems.

user333869
  • 549
  • 1
  • 4
  • 13