Vim Tips Wiki
(Uploaded by JohnBot from a locally edited file)
(yet another approach)
(12 intermediate revisions by 7 users not shown)
Line 4: Line 4:
 
|previous=19
 
|previous=19
 
|next=21
 
|next=21
|created=February 25, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
 
|author=jean
 
|author=jean
|version=5.7
+
|version=6.0
 
|rating=733/269
 
|rating=733/269
  +
|category1=Swapfiles
  +
|category2=
 
}}
 
}}
 
Have you ever been frustrated at swap files and backups cluttering up your working directory?
 
Have you ever been frustrated at swap files and backups cluttering up your working directory?
   
 
Untidy:
 
Untidy:
  +
<pre>
ons.txt
 
ons.txt~
+
ons.txt
 
ons.txt~
README
 
README~
+
README
 
README~
tester.py
 
tester.py~
+
tester.py
 
tester.py~
  +
</pre>
   
 
Here are a couple of options that can help:
 
Here are a couple of options that can help:
  +
<pre>
set backupdir=./.backup,.,/tmp
 
set directory=.,./.backup,/tmp
+
set backupdir=./.backup,.,/tmp
 
set directory=.,./.backup,/tmp
  +
</pre>
   
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.
+
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==
 
==Comments==
Line 30: Line 36:
   
 
----
 
----
  +
Added on 24th Oct 2011: Windows specific:
However you need the swp files when multiple users are working on the same directory, to avoid losing data.
 
   
  +
If you want all the swap(.swp), backup(~) files in a separate folder say
  +
<pre>
  +
D:\soft\installed\Vim\swap
  +
D:\soft\installed\Vim\backup
  +
</pre>
  +
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):
  +
<pre>
  +
" 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
  +
</pre>
  +
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.
In Windows:<br>
 
TODO: Is the \\ stuff necessary??
 
   
  +
----
Use \\ instead of / in your path.<br>
 
 
In Windows:
You may want to replace .backup with _backup:
 
   
 
Use \\ instead of / in your path. You may want to replace .backup with _backup:
set backupdir=.\\_backup,.,c:\\tmp
 
  +
<pre>
set directory=.,.\\_backup,c:\\tmp
 
 
set backupdir=.\_backup,.,c:\temp
 
 
set directory=.,.\_backup,c:\temp
You would need to create the C:\tmp directory.
 
  +
</pre>
   
 
To specify directories with spaces, do something like:
 
To specify directories with spaces, do something like:
  +
<pre>
 
set backupdir=C://docume~1//joethe~1//mydocu~1//backups
+
set backupdir=C:/docume~1/joethe~1/mydocu~1/backups
  +
</pre>
   
 
I use:
 
I use:
  +
<pre>
set backupdir=$TEMP,$TMP,.
+
set backupdir=$TEMP,$TMP,.
  +
</pre>
  +
:$[[TMPDIR]]? [[User:Nievim|Nievim]] ([[User talk:Nievim|talk]]) 23:25, August 2, 2012 (UTC)
   
 
----
 
----
Line 55: Line 82:
   
 
----
 
----
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:<br>
+
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: <code>/usr/share/vim/vimrc</code>
/usr/share/vim/vimrc
 
   
 
----
 
----
Line 66: Line 92:
   
 
Here is the explanation of this from Vim help:
 
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.
 
- 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:
 
Here's my solution:
  +
<pre>
 
set backupdir=$VIMRUNTIME\\temp\\
+
set backupdir=$VIMRUNTIME\\temp\\
set directory=$VIMRUNTIME\\temp\\
+
set directory=$VIMRUNTIME\\temp\\
silent execute '!del "'.$VIMRUNTIME.'\temp\*~"'
+
silent execute '!del "'.$VIMRUNTIME.'\temp\*~"'
  +
</pre>
   
 
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.
 
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.<br>
+
You can add the following lines to _vimrc. Do this ''after'' any source statements because the sourced scripts may change your settings.
  +
<pre>
Do this ''after'' any source statements because the sourced scripts may change your settings.
 
  +
set nobackup
 
set nobackup
+
set nowritebackup
  +
</pre>
set nowritebackup
 
   
 
----
 
----
 
Put this in the _vimrc file to create whatever directory you want:
 
Put this in the _vimrc file to create whatever directory you want:
  +
<pre>
silent execute '!mkdir _backupdir'
+
silent execute '!mkdir _backupdir'
  +
</pre>
   
 
----
 
----
Line 96: Line 121:
   
 
Place the following in your Unix "$HOME/.vimrc" as a work around.
 
Place the following in your Unix "$HOME/.vimrc" as a work around.
  +
<pre>
 
" Automatically create .backup directory, writable by the group.
+
" Automatically create .backup directory, writable by the group.
if filewritable(".") &amp;&amp; ! filewritable(".backup")
+
if filewritable(".") &amp;&amp; ! filewritable(".backup")
 
silent execute '!umask 002; mkdir .backup'
 
silent execute '!umask 002; mkdir .backup'
endif
+
endif
  +
</pre>
  +
----
  +
To create a "_backup" dir under Windows in the same directory as the current file:
  +
<pre>
  +
if filewritable(expand('%:p:h')."\.") && !filewritable(expand('%:p:h').'\_backup')
  +
silent execute '!mkdir "'.expand('%:p:h').'\_backup"'
  +
endif
  +
</pre>
  +
----
  +
Under Windows Vista using GVIM 7.2, I added the following bit to my vimrc:
  +
<pre>
  +
silent execute '!mkdir "'.$VIMRUNTIME.'/temp"'
  +
silent execute '!del "'.$VIMRUNTIME.'/temp/*~"'
  +
set backupdir=$VIMRUNTIME/temp//
  +
set directory=$VIMRUNTIME/temp//
  +
</pre>
  +
  +
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 backupdir 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 directory sets the path to store backups. Unlike backupdir, 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:
  +
<pre>
  +
set backupdir=$VIMRUNTIME\\temp\\\\
  +
</pre>
   
 
----
 
----
  +
  +
<pre>
  +
set directory-=$HOME/tmp
  +
set directory^=$HOME/tmp//
  +
set backupdir-=$HOME/tmp
  +
set backupdir^=$HOME/tmp//
  +
</pre>

Revision as of 01:57, 4 August 2012

Tip 20 Printable Monobook Previous Next

created 2001 · complexity basic · author jean · version 6.0


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


Added on 24th Oct 2011: Windows specific:

If you want all the swap(.swp), backup(~) files in a separate folder say

D:\soft\installed\Vim\swap
D:\soft\installed\Vim\backup

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.


In Windows:

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:

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

I use:

set backupdir=$TEMP,$TMP,.
$TMPDIR? Nievim (talk) 23:25, August 2, 2012 (UTC)

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

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 backupdir 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 directory sets the path to store backups. Unlike backupdir, 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 backupdir=$VIMRUNTIME\\temp\\\\

set directory-=$HOME/tmp
set directory^=$HOME/tmp//
set backupdir-=$HOME/tmp
set backupdir^=$HOME/tmp//