Vim Tips Wiki
(Move categories to tip template)
(Remove html character entities)
Line 100: Line 100:
   
 
" Automatically create .backup directory, writable by the group.
 
" Automatically create .backup directory, writable by the group.
if filewritable(".") && ! filewritable(".backup")
+
if filewritable(".") && ! filewritable(".backup")
 
silent execute '!umask 002; mkdir .backup'
 
silent execute '!umask 002; mkdir .backup'
 
endif
 
endif

Revision as of 08:19, 28 September 2008

Tip 20 Printable Monobook Previous Next

created February 25, 2001 · complexity basic · author jean · version 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.

Comments

TODO: See VimTip1393


However you need the swp files when multiple users are working on the same directory, to avoid losing data.


In Windows:
TODO: Is the \\ stuff necessary??

Use \\ instead of / in your path.
You may want to replace .backup with _backup:

set backupdir=.\\_backup,.,c:\\tmp
set directory=.,.\\_backup,c:\\tmp

You would need to create the C:\tmp directory.

To specify directories with spaces, do something like:

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

I use:

 set backupdir=$TEMP,$TMP,.

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:
/usr/share/vim/vimrc


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.

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