Have you ever been frustrated at swap files and backups cluttering up your working directory?
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.
TODO: See VimTip1393
Added on 24th Oct 2011: Windows specific:
If you want all the swap(.swp), backup(~) files in a separate folder say
And you also want to avoid file collisions (i.e. working with files of same name in different directories) I have a working solution for gVim 7.3 on XP SP3 (should work with later versions as well):
" vim's vimrc_example and msvim already sets up backup and swap " set swap(.swp),backup(~),undo(.udf) directory to vim installation " Please make sure the directory exists otherwise current directory will be used set dir=D:\\soft\\installed\\Vim\\swap//,.,c:\\tmp,c:\\temp set backupdir=D:\\soft\\installed\\Vim\\backup//,.,c:\\tmp,c:\\temp " Remember the undo history for file, doesn't work with file collisions " set udf " set udir=D:\\soft\\installed\\Vim\\undo//,.,c:\\tmp,c:\\temp
If you want to be able to create those directories as well, please use the suggestions given below
However you need the swp files when multiple users are working on the same directory, to avoid losing data.
Use \\ instead of / in your path. You may want to replace .backup with _backup:
set backupdir=.\_backup,.,c:\temp set directory=.,.\_backup,c:\temp
To specify directories with spaces, do something like:
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).
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:
Just a note to the poster who was concerned about using a common directory for swap file:
You need to use the following syntax.
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.
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.
You can add the following lines to _vimrc. Do this after any source statements because the sourced scripts may change your settings.
set nobackup set nowritebackup
Put this in the _vimrc file to create whatever directory you want:
silent execute '!mkdir _backupdir'
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).
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
To create a "_backup" dir under Windows in the same directory as the current file:
if filewritable(expand('%:p:h')."\.") && !filewritable(expand('%:p:h').'\_backup') silent execute '!mkdir "'.expand('%:p:h').'\_backup"' endif
Under Windows Vista using GVIM 7.2, I added the following bit to my vimrc:
silent execute '!mkdir "'.$VIMRUNTIME.'/temp"' silent execute '!del "'.$VIMRUNTIME.'/temp/*~"' set backupdir=$VIMRUNTIME/temp// set directory=$VIMRUNTIME/temp//
This combines a number of the previous tips:
- The mkdir makes sure the folder always exists (this is probably less important if you're using VIMRUNTIME like I am here, but if you're using a temp folder that may get purged regularly, it's a good thing to have).
- The del clears out old backups on each restart.
- The 'directory' uses path qualifiers on swap files so you can edit multiple files with the same name without vim freaking out (e.g., c%%Vim%vim72%vimrc.vim.swp).
- The 'backupdir' sets the path to store backups. Unlike 'directory', this does not add path qualifiers even with the double slash, so you will get overwriting if you're working on files with the same name.
But to add a new tip: Vim 7.2 on Vista at least supports the '/' for folder separators (I'm not sure if it's Vista or 7.2 that makes the difference; I'll try this with 7.2 on XP later to see if it works there). The path qualifiers on backupdir do not work with double (really quadruple) backslashes, i.e. this doesn't work:
set directory-=$HOME/tmp set directory^=$HOME/tmp// set backupdir-=$HOME/tmp set backupdir^=$HOME/tmp//