sponsor Vim development Vim logo Vim Book Ad

basic Tip #20: Are *.swp and *~ files littering your working directory?

 tip karma   Rating 961/341, Viewed by 23238 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   February 25, 2001 22:27      complexity:   basic
author:   [email protected]      as of Vim:   5.7

Have you ever been frustrated at swap files and backups cluttering
up your working directory?

Untidy:
  ons.txt
  ons.txt~
  README
  README~
  tester.py
  tester.py~

Here are a couple of options that can help:

  set   backupdir=./.backup,.,/tmp
  set   directory=.,./.backup,/tmp

This way, if you want your backups to be neatly grouped, just
create a directory called '.backup' in your working directory.
Vim will stash backups there. The 'directory' option controls
where swap files go. If your working directory is not writable,
Vim will put the swap file in one of the specified places.

 rate this tip  Life Changing Helpful Unfulfilling 

<< line numbers... | easy pasting to windows apps >>

Additional Notes

[email protected], December 28, 2001 2:20
Yes but remind that the swp files are very useful when many users are working on the same directory, to prevent you loosing datas...
Anonymous, July 25, 2002 8:13
I love you
Anonymous, November 25, 2002 7:24
I too, love you.
Anonymous, December 22, 2002 21:22
I donīt love you, but the tip is great. Thank you.
Anonymous, January 7, 2003 5:40
I was not able to use it in Windows. Is there some difference on how to properly write paths?
Anonymous, January 10, 2003 10:05
Yes it is. Use \\ instead of / in your path.
It should look like:

  set   backupdir=.\\.backup,.,c:\\tmp
  set   directory=.,.\\.backup,c:\\tmp
Anonymous, January 23, 2003 9:55
To use it in Windows, I replaced .backup by _backup:

set   backupdir=./_backup,.,/tmp
set   directory=.,./_backup,/tmp
[email protected], June 6, 2003 2:25
You're a legend mate! I spent freakin ages trying to find the solution for this (most of the time not knowing if it was a VIM problem or something else). No more annoying backup files... yeeeehhhaaaaaa!!
Anonymous, September 2, 2003 17:24
That's "losing" data.

(What is it with people confusing "loosing"/"losing" and "loose"/"lose" these days?  It's a fairly recent phenomenon, AFAIK.  I first started seeing it only a few years ago - mostly on Slashdot.)
Anonymous, November 3, 2003 5:46
Don't start. You're going to loose this discussion
Anonymous, November 3, 2003 5:56
Sorry, couldn't help it.
'lose' is a verb: something you do (or rather not)
'loose' is an adjective: it describes a quality of something (or lack of it)
Example: men with loose canons usually lose their lives.
Anonymous, November 12, 2003 3:40
Jeebus, stay on the darn topic, willya?! who cares!

re: tip - great one - I personally hate ~ files, as I use CVS to protect and avoid "losing" data.
[email protected], December 9, 2003 17:15
This is huge!  ~ files have been the bane of my vim existence for years I thought for sure I had found the answer here:

     http://vim.sourceforge.net/htmldoc/recover.html

... but no dice - it didn't work until I set the backupdir oprtion as explained in this tip.

Thanks!
Ethan
[email protected], January 8, 2004 19:13
I guess I'm just dense. I'm using vim 6.2, and I've tried all the settings in this thread, in the _vimrc where my gvim.exe resides, and nothing changes. Apparently I'm missing something...
Anonymous, March 24, 2004 14:05
For me, to let it work on WinXP, I needed to change to:
set backupdir=./_backup,/temp,.
set directory=./_backup,/temp,.
Okki, March 27, 2004 5:37
Love the tip.
I put this in my vimrc so that i always have the files where is can find them. And still have the chance to put a seperate _backupdir in my workingdir.
set backupdir=./_backup,$VIMRUNTIME/_files,.
set directory=./_backup,$VIMRUNTIME/_files,.

Only the backups dont seem to work, as I always hated the ~files to remain in the working dir I just put
set nobackup
in my vimrc, now I have commented that out, but my backupfiles don't seem to return, not in my working dor, nor in my backupdir.

Can anyone guess why?
Okki (ten minutes later), March 27, 2004 6:01
Would YOU know, I rebooted my WinXP box and suddenly my ~ files are in my _files dir too.
Wouldnt know what I did. But it worx. :S
Anon E. Mus, June 13, 2004 9:40
I do not want the ~ file anywhere. Moving it elsewhere does not solve the problem.
[email protected], June 21, 2004 1:35
I would like to decrypt a file, view or edit the plaintext, and encrypt the plaintext back the very moment vim is closed. I can do all this in a bat file, but there must not be any *.swp, *~ files and the like be left around (not even in trash!). Best would be to wipe the RAM as well before leaving vim. - How can I achieve this?
Anonymous, June 24, 2004 11:17
The main problem with dumping all swap files to one folder (like /tmp) is the fact that you cannot edit two files with the same name (from two different folders of course). If you are editing typical configuration files across projects then you will run into this situation quite often (build.xml Ant files for example).
Anonymous, June 28, 2004 8:32
I've been using Vim for years and never had this problem. Now with a new Linux distro, there they are. I don't just want to know how to turn them off (perhaps add this in ~/.vimrc: set nobackup; set nowritebackup). I want to know where these  files get turned on to begin with. I don't mind the swap files because they go away unless there is a problem (like a power outage), but the backup~ files are very annoying. It's too easy to leave one of these files in the wrong place. Imagine deleting a cgi script with a security flaw only to have your attacker just call up the backup~ version. I'm not a big fan of "trash can" strategies. If you want to delete a file do it. If you want to preserve a copy in an out of the way place (say a directory called "Trash"), fine. But by making me delete 2 files every time I want to delete one, I'm more inclined to start doing things like "rm backup*" and increasing my use of commands which feature "rm" and "*" will not make me less nervous about file deletions.
Anonymous, June 28, 2004 8:48
Ok, solved my own problem. If you're getting backups that you don't want and weren't explicitly turned on in your own ~/.vimrc, you can look exactly where the man page says to look:
/usr/share/vim/vimrc
I just commented out all the backup stuff and life was back to normal. Not so bad, since the default behavior seems reasonable.
Anonymous, June 30, 2004 23:06
> I want to know where these  files get turned on to begin with.

probably somewhere in your distribution's /usr/share/vim. Redhat's default vim configuration turns on the autobackups, and a few other really annoying things. It took me a while to realize that the bad defaults werent native to vim, and only appeared in vim site config files installed via redhat rpms. You can su to root and tweak /usr/share/vim, you can uninstall the vim rpm and compile/install from tgz, or  you can install slackware and never have to worry about bad defaults ever again. ;-)
Anonymous, January 10, 2005 15:09
To specify directories with spaces under Windows XP do something like:

set backupdir=C://docume~1//joethe~1//mydocu~1//backups

Where the actual directory is: C:\Documents and Settings\Joe The User\My Documents\Backups
[email protected], February 10, 2005 12:01
Just a note to the poster who was concerned about using a common directory for swap file:

You need to use the following syntax.

set directory=~/.backup//

Here is the explanation of this from Vim help:

        - For Unix and Win32, if a directory ends in two path separators, the
          swap file name will be built from the complete path to the file
          with all path separators substituted to percent '%' signs. This will
          ensure file name uniqueness in the preserve directory.
[email protected], April 13, 2005 17:25
Is it possible to have a .backup sub-directory in the working dir where these filese would be placed? So one doesn't end up with a cluttered dir and saves the backup files in an unobtrusive place relative to the working dir?
[email protected], April 25, 2005 7:50
Is the option to end the directory with // also available for 'backupdir' somehow?
Such that the backup files do not overwrite each other?
[email protected], July 15, 2005 11:24
Under Windows XP, finally I have a workaround I can live with.  I tried the set nobackup and set nowritebackup options and they still write this stupid ~ file.  What if you edit a 50MB logfile and now you're wasting an extra 50MB on this clutter file.  I also hated mixing my temp backup files with other clutter in the temp folder so I create a vim exclusive temp folder

So, here's my solution:

set backupdir=$VIMRUNTIME\\temp\\
set directory=$VIMRUNTIME\\temp\\
silent execute '!del "'.$VIMRUNTIME.'\temp\*~"'

It still creates the ~ file, but every time you fire up VIM it nukes all the old junk.  And if for some wierd reason you actually need the ~ file you can go back into the temp folder and rescue it before starting VIM.
[email protected], August 24, 2005 3:00
Hey mate

You don't have to get that far.

If you don't need the file just goto you vim directory
Add these line to .vimrc

set nobackup
set nowritebackup

Enjoy vimming. :)

Cheers
[email protected], August 24, 2005 3:04
Oops.. you've already tried that.. but seriously if you're looking to get job done then it's fine but the way you're dealing is a fairly heavyweight way. :P
But you've got a way! :)


Cheers
Anonymous, August 25, 2005 17:08
hey i have tried adding the following lines to _vimrc as i am using gvim 6.3 for windows.

set nobackup
set nowritebackup

but still filename~ crops up.
Anonymous, August 25, 2005 17:29
hey it worked the only difference was i added the lines after

source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
[email protected], September 1, 2005 3:31
This is a great tip, I'd like to congratulate the author! Good work, great job!
[email protected], September 2, 2005 6:58
AHH!!! Thanks!!! *Smooch* *smooch* *lick* dammit!!

It was cluttering my network directory like crazy until I saw this tip.  

Now it's neat and tidy!

Thanks again!
Anonymous, December 7, 2005 8:49
I love you
Anonymous, December 7, 2005 8:56
hi
Anonymous, December 7, 2005 9:39
hi my name is steve knapenberger
Anonymous, December 8, 2005 9:42
help me
Anonymous, December 8, 2005 9:42
help me
Anonymous, December 8, 2005 9:43
i hate u



Anonymous, December 8, 2005 9:43
i hate u



Anonymous, December 8, 2005 9:43
i hate u



[email protected], January 7, 2006 10:30
Does anyone know how to make it so that if it doesn't find the folder it will create the folder instead of moving to the next option?  IE. i want backup files to always go into a folder called "backup".  if it doesn't exist, I'd like vim to just make it.

Thanks
Anonymous, January 25, 2006 12:15
I use backupdir=$TEMP,$TMP,. on Windows.
Anonymous, January 26, 2006 13:54
changing the $VIM\_gvimrc file for vim63 on windows worked for me.  
[email protected], February 13, 2006 8:17
I think the reason why people were having problems with these options:

set nobackup
set nowritebackup

was that they added to the top of C:\Program Files\Vim\_vimrc and a few lines later, the other vimrc files get sourced overwriting those values. If you just add it to the bottom, it will work fine.
Anonymous, February 17, 2006 13:28
Awesome tip, thanks!!!
[email protected], March 11, 2006 9:11
Swap files don't delete once done editing a file.

In Vim, isn't it usually the case that when you edit a file and then save and complete editing it, that the swap files generated get deleted?

Not so in my case. How would I go about making it so that while I am editing a file a swap file is generated and persists until I am done editing a file and save and exit vim? (just in case I have a power outage or something and can still recover from the vim swap file)

I'm running Windows XP.
Anonymous, March 26, 2006 14:49
To address the very original problem, all i did was:

set nobackup

in my vimrc_example file in the /vim64 directory that is called by the _vimrc at the root of the program directory.  This is probably not good for various reasons, but I can't stand the numerous backup files.  This is how it was eliminated completely.
anonymous, March 30, 2006 8:53
How about

set nobackup writebackup


This creates a swapfile and removes it  while quitting/saving. No ~ files.
Anonymous, May 14, 2006 19:43
im a bit lost also as to why people arent just using 'set nobackup'. the .swp files are only around temp'ly so i dont get why you'd want to get rid of them anyways..
[email protected], May 20, 2006 9:29
This is the reason why I don't want the .swp files or any extra files but the file I'm working on:

If I'm working on a gentoo ebuild i have to run a script that md5sum's, RMD160's, and SHA's, all the files in the current working directory . I can't have .swp files floating around because they aren't supposed to be in the ebuild manifest file. I tried "set nobackup" and "set nowritebackup" and it doesn't work in ~/.vimrc or ~/.gvimrc with vim-7 .
[email protected], May 20, 2006 9:52
here's the command everyone's looking for:

set noswapfile

no swap files are created.
[email protected], June 12, 2006 11:54
For a reason beyond my understanding, "set nobackup" has no effect when it's placed in the first line of vimrc, but does work when you put it in the last line.
This is true on my XP system.
[email protected], June 22, 2006 6:05
For the simple reason that, somewhere in the rest of the file, your set backup is called. ( could be a sourced file ).
End of file works ( you set it after the original setting). Beginning of file does not work ( the file resets your value ).
No surprises!!!
[email protected], July 12, 2006 6:08
in windowsXP i only added one line,

set backupdir=d://vimbkup

an it worked. the backup files are now in vimbkup.
so why does it have to be this long like this?

  set   backupdir=.\\.backup,.,c:\\tmp
  set   directory=.,.\\.backup,c:\\tmp
[email protected], July 28, 2006 15:47
It does not have to be that long, it is just an escape sequence - if vim cannot write to the specified directory, it has the posibility to write to a different place. A good idea is the working directory followed by the tmp directory.
Anonymous, August 31, 2006 12:56
backupdir=(...) requires backup directory to exist (it doesn't create it).

So best way to override this is adding:
silent execute '!mkdir _backupdir' to the _vimrc file
Anonymous, October 4, 2006 21:52
thanks guys. was searching for an option to get rid off the swap files. could achive it by setting set nobackup in vimrc.
Anonymous, October 24, 2006 14:23
If the ".backup" directory is automatically created (on Unix) as suggested earlier,
it can create a nasty problem when editing files in another persons directory.
The .backup directory is created without write permission for group (and others).
This may be good for security, but it prevents the other person from cleaning up
his own directory tree (below the .backup directory)...  Ouch !

Place the following in your Unix "$HOME/.vimrc" as a work around...

" Automatically create .backup directory, writable by the group.
if filewritable(".") && ! filewritable(".backup")
    silent execute '!umask 002; mkdir .backup'
endif

This may work on windoze, but I don't really care ;^)
Randy
Anonymous, November 3, 2006 13:02
How do I access the path variable in windows XP??

Based on all the posts setting no backup in the .vimrc is the best option, right?

How do I access this file?
Anonymous, November 22, 2006 8:54
To automate creating the directories and initializing the settings, refer to vimtip #1393.  this is especially useful when you are plopping your vimrc on a new machine.
[email protected], December 12, 2006 10:32
I love you so much
[email protected], December 18, 2006 3:07
Thanks for this tip, it rules.. no more swapfile junk littering my HD!
Anonymous, January 18, 2007 11:29
i love everyone here.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo