175

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:

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
Josh
  • 1,752
  • 1
  • 18
  • 20
hasen
  • 148,751
  • 62
  • 182
  • 223
  • 13
    Git will never help you recover work after a system crash. Git can ignore your *.swp files too. =D – vmassuchetto Apr 06 '12 at 23:06
  • 3
    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:46
  • 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

7 Answers7

80

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
user55400
  • 3,653
  • 1
  • 19
  • 13
  • 4
    well, I never needed those with notepad++, unless you say that vim will crash too often! – hasen Apr 15 '09 at 10:57
  • 8
    no, I wasn't implying that vim crashes too often (though I've seen it crash). But for my working environment, the network connection to some host I'm working on is quite unstable, so interrupted editing occurs. Swap files help there. – user55400 Apr 16 '09 at 08:47
  • 9
    @user55400 heard of GNU/Screen? (or tmux, or this Japanese alternative) – Elazar Leibovich Apr 26 '11 at 12:53
  • I added this to my _vimrc file on Windows, and I'm still getting file~ files. Am I not doing something correctly? – FilBot3 Aug 26 '15 at 14:48
  • @Pred: those are backup files. You need to do `set nobackup` for your problem. – crisron Dec 19 '15 at 13:36
  • 4
    @noircc I would advise against using /tmp. In case the whole system crashes and you need to reboot, your swapfiles will be gone. –  Mar 08 '16 at 21:26
  • I really wish more editors used a similar swap file system. (: – SilverWolf Jan 21 '18 at 20:06
61

Put this in your .vimrc configuration file.

set nobackup
eipark
  • 5,886
  • 3
  • 18
  • 31
Chad Birch
  • 69,230
  • 22
  • 145
  • 148
  • 6
    We've done this on Windows 8.1 with Vim. The swap files are still present. – Shaun Luttin Jun 02 '15 at 01:46
  • 2
    Shaun, you may need to add "set noswapfile" to your .vimrc as well to prevent new swap files from being created, as mentioned in the edit to the original question. – Earl Zedd Jul 18 '15 at 15:12
  • What about the `:` sitting as a prefix before the `set` keyword ? Is it required ? It does not look like a comment.. – Stephane Jul 10 '17 at 08:51
  • @Stephane a `:` prefix means its done in command mode, but you don't need a `:` when writing your vimrc – TankorSmash Feb 27 '18 at 15:28
  • That's not really a good idea. The above of letting these files sit in the temp/ folder is already far better. – MonsieurMan Aug 13 '19 at 20:13
37

; 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
Glen Solsberry
  • 10,880
  • 11
  • 64
  • 92
Sooraj
  • 387
  • 3
  • 2
11

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//
Vlad Bezden
  • 59,971
  • 18
  • 206
  • 157
10

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.

joeytwiddle
  • 24,338
  • 11
  • 107
  • 91
10

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.

wlwl2
  • 438
  • 4
  • 13
5

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
Raza
  • 1,971
  • 2
  • 23
  • 26