writebackup : Write backups of current file with date file extension.
| script karma
Downloaded by 2495
Comments, bugs, improvements
script versions (upload new version)
This is a poor man's revision control system, a primitive alternative to CVS,
RCS, Subversion, etc., which works with no additional software and almost any
The :WriteBackup command writes subsequent backups of the current file with a
'current date + counter' file extension (format '.YYYYMMDD[a-z]'). The first
backup of a day has letter 'a' appended, the next 'b', and so on. (So that a
file can be backed up up to 26 times on any given day.)
By default, backups are created in the same directory as the original file,
but they can also be placed in a directory relative to the original file, or
in one common backup directory for all files (similar to Vim's 'backupdir'
option), or even in a file-specific location that is determined via a
user-provided callback function.
- The writebackupVersionControl.vim plugin (vimscript #1829) complements
this script with additional commands and enhances the :WriteBackup command
with more checks, but is not required.
- The writebackupToAdjacentDir.vim plugin (vimscript #3107) implements a
WriteBackup-dynamic-backupdir configuration that puts the backup files in
an adjacent backup directory if one exists. This helps where the backups
cannot be placed into the same directory.
- The writebackupAutomator.vim plugin (vimscript #3940) automatically writes
a backup on a day's first write of a file that was backed up in the past,
but not yet today. It can be your safety net when you forget to make a
Write the whole current buffer to the next available
backup file with a '.YYYYMMDD[a-z]' file extension.
If the last backup is identical with the current
buffer contents, no (redundant) backup is written.
With [!], creation of a new backup file is forced:
- even if the last backup is identical
- even when no more backup versions (for this day) are
available (the last '.YYYYMMDDz' backup gets
overwritten, even if it is readonly)
PS: In addition to this Vim plugin, I also provide the basic writebackup
functionality outside of Vim as VBScript and Bash Shell script versions at
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vba.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
To uninstall, use the :RmVimball command.
- Requires Vim 7.0 or higher.
- The writebackupVersionControl plugin (vimscript #1829) complements this
script with addtional commands and enhances the :WriteBackup command with
additional checks, but is not required.
For a permanent configuration, put the following commands into your vimrc file
(see :help vimrc). Note: With version 1.30, the variable was renamed from
g:writebackup_BackupDir. Please update your configuration!
To put backups into another directory, specify a backup directory via
let g:WriteBackup_BackupDir = 'D:\backups'
A directory starting with './' or '../' (or the backslashed-variants '.' for
MS-DOS et al.) puts the backup file relative to where the backed-up file is.
The leading '.' is replaced with the path name of the current file:
let g:WriteBackup_BackupDir = './backups'
If you want to automatically create a non-existing backup directory,
dynamically determine the backup directory based on the current filespec or
any other changing circumstances, you can set a custom callback function:
function MyResolveBackupDir(originalFilespec, isQueryOnly) ...
let g:WriteBackup_BackupDir = function('MyResolveBackupDir')
You can override this global setting for specific buffers via a buffer-scoped
variable, which can be set by an autocmd, ftplugin, or manually:
let b:WriteBackup_BackupDir = 'X:\special\backup\folder'
If the writebackupVersionControl plugin is installed, no backup is written if
there is an identical predecessor, so you don't need to remember whether
you've already backed up the current file; no redundant backups will be
If you don't like this check, turn it off via:
let g:WriteBackup_AvoidIdenticalBackups = 0
It occasionally happens that an identical backup is kept lying around, e.g.
when reverting to the backup without removing it. Since that backup would
misleadingly date the next change much earlier than it actually happened,
writebackup automatically renames the earlier backup if it would be identical
to the first backup created today.
If you don't want such an automatic rename and instead get the "is already
backed up" error, turn off redate via:
let g:WriteBackup_AvoidIdenticalBackups = 1
This reinstates the old behavior of writebackup versions < 3.00.
In case you already have other custom Vim commands starting with W, you can
define a shorter command alias ':W' in your vimrc to save some keystrokes. I
like the parallelism between ':w' for a normal write and ':W' for a backup
command -bar -bang W :WriteBackup<bang>
Click on the package to download.
ip used for rating: 18.104.22.168
||- ENH: New default "redate" for g:WriteBackup_AvoidIdenticalBackups that renames an identical backup from an earlier date to be the first backup of today.
- Using :keepalt instead of a temporary :set cpo-=A.
||Replaced simple filespec escaping with built-in fnameescape() function (or
emulation for Vim 7.0 / 7.1) via escapings.vim wrapper.
||- Using separate autoload script to help speed up VIM startup. This is an
incompatible change that also requires the corresponding
writebackupVersionControl plugin version. *** PLEASE UPDATE
writebackupVersionControl (vimscript #1829), too, if you're using it ***
- ENH: Disallowing backup of backup file if the writebackupVersionControl
plugin is installed.
- ENH: No backup is written if there is an identical previous backup. This
requires the writebackupVersionControl plugin and can be configured via
||Split off documentation into separate help file. Now packaging as VimBall.
||ENH: The backup directory can now be determined dynamically through a callback function.
Renamed configuration variable from g:writebackup_BackupDir to g:WriteBackup_BackupDir. *** PLEASE UPDATE YOUR CONFIGURATION ***
BF: Now also allowing relative backup dir in an upper directory (i.e. g:WriteBackup_BackupDir starting with '../'.
BF: Unnamed buffers were backed up as '.YYYYMMDDa'.
Now setting v:errmsg on errors and using ErrorMsg instead of Error highlight group.
||Minor change: Added -bar to :WriteBackup, so that commands can be chained together.
||ENH: Added support for writing backup files into a different directory (either one static backup dir or relative to the original file) via g:writebackup_BackupDir configuration, as suggested by Vincent DiCarlo.
Now requiring VIM 7.0 or later, because it's using lists.
BF: Special ex command characters ' \%#' must be escaped for ':w' command.