How to prevent vim from creating (and leaving) temporary files?
Asked Answered
H

7

202

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
Hazlip answered 13/4, 2009 at 6:7 Comment(3)
Git will never help you recover work after a system crash. Git can ignore your *.swp files too. =DSokoto
Possible duplicate of Why does Vim save files with a ~ extension?Axiomatic
Related post - Why is vim leaving temporary file versions all over the place?Myongmyopia
S
97

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
Shoshanashoshanna answered 15/4, 2009 at 7:48 Comment(8)
well, I never needed those with notepad++, unless you say that vim will crash too often!Hazlip
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.Shoshanashoshanna
@Shoshanashoshanna heard of GNU/Screen? (or tmux, or this Japanese alternative)Aarau
I added this to my _vimrc file on Windows, and I'm still getting file~ files. Am I not doing something correctly?Exploratory
@Pred: those are backup files. You need to do set nobackup for your problem.Bradbradan
@noircc I would advise against using /tmp. In case the whole system crashes and you need to reboot, your swapfiles will be gone.Neodymium
The question is not whether the OP should keep using them or not.Phrasing
I've been burned by swapfiles a lot more times than I've been saved by them, bur perhaps sneakernetting directories full of vim-edited files is bad praxis.Wastebasket
W
67

Put this in your .vimrc configuration file.

set nobackup
Wegner answered 13/4, 2009 at 6:10 Comment(5)
We've done this on Windows 8.1 with Vim. The swap files are still present.Darryldarryn
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.Sutherlan
What about the : sitting as a prefix before the set keyword ? Is it required ? It does not look like a comment..Openeyed
@Openeyed a : prefix means its done in command mode, but you don't need a : when writing your vimrcCirrostratus
That's not really a good idea. The above of letting these files sit in the temp/ folder is already far better.Braiding
R
42

; 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
Rouault answered 16/6, 2011 at 5:20 Comment(2)
I had to add set undodir=C:\WINDOWS\TempKevel
This didn't work for me - I had to use set dir=$TEMP insteadKp
L
22

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
Laellaertes answered 4/5, 2020 at 4:6 Comment(1)
The other answers are good but this one (coming in a few years later) is my favorite. It keeps things organized across machines/OSsAsh
N
15

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.

Nerves answered 17/8, 2017 at 3:8 Comment(0)
E
14

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//
Equiprobable answered 7/4, 2017 at 19:56 Comment(0)
B
12

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.

Bluebell answered 24/11, 2014 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.