Write a date-stamped backup of the current file
From Vim Tips Wiki
Tip 853 Previous Next created January 15, 2005 · complexity basic · author Anders Thøgersen · version 5.7
I sometimes find it useful to take a backup of the file I am currently editing before continuing to edit the file. That's what this function is for.
Put this into your vimrc and press \ba while in normal mode, and the current file will be backed up.
" A mapping to make a backup of the current file.
fun! WriteBackup()
let fname = expand("%:p") . "__" . strftime("%Y_%m_%d_%H.%M.%S")
silent exe ":w " . fname
echo "Wrote " . fname
endfun
nnoremap <Leader>ba :call WriteBackup()<CR>
[edit] Comments
- Is it not better to escape filenames?
- Also, I think it is better if this backup operation doesn't modify the buffers' modified flag and
- Finally, in order to not break "openability", I prefer keeping the original extension and inserting my backup extension before that.
Which gives:
fun! WriteBackup()
let _modified = &modified
let fname = expand("%:p:r") . "." . strftime("%Y%m%d-%H%M") . "." . expand("%:e")
silent exe ":w " . fnameescape(fname)
let &modified = _modified
echo "Wrote " . fname
endfun
Is this apt to replace the function in the main section? --Awagner 12:25, 18 July 2009 (UTC)
Another trick that I use is:
:w %33
where 33 is whatever you want, to save foo.bar as foo.bar33 as a backup.
" Back up current file to date appended file name and into a specific directory c:/backup
" have also put file name last so that, saved file will open correctly in VIM (syntax etc)
fun! WriteBackup()
let fname = strftime("%Y%m%d_%H%M%S") . "_".expand("%:t")
silent exe ":%w c:/backup/" . fname
echo "Wrote c:/backup/" . fname
endfun
nnoremap ,k :call WriteBackup()<CR>
Also consider script#563 to automatically keep backups in RCS.
I use 'zs' to make a temp copy of the current file in $TMP for temporary editing, this keeps the original extension.
Example: vim file.c -'zs'-> /tmp/file.c-2005-01-17_004201.c
Put this line in vimrc to define the 'zs' macro:
:map zs :exe "sav $TMP/". substitute(expand("%:t"),strftime("-%Y-%m-%d_.*"),"","").strftime("-%Y-%m-%d_%H%M%S").".".expand("%:e")<CR>
I already have this line in my _virmrc:
set bdir=c:\\temp\\editbak " location of the backups
How can I use this value as a location of these backups ?
This should do what you ask. The trailing \\ in g:bdir is required, and you have to use let instead of set to set a global variable.
let g:bdir='c:\\temp\\editbak\' " location of the backups
fun! WriteBackup()
let fname = strftime("%Y%m%d_%H%M%S") . "_".expand("%:t")
let path_and_file = g:bdir . fname
silent exe ":w " . path_and_file
echo path_and_file
endfun
nnoremap ,k :call WriteBackup()<CR>
Thanks, works great! One remark:
>>This should do what you ask. The trailing \\ in g:bdir is required, >>and you have to use let instead of set to set a global variable. >> let g:bdir='c:\\temp\\editbak' " location of the backups Should be: let g:bdir='c:\\temp\\editbak\' " location of the backups
No?
See also VimTip694 -- for simpler solution.
See also VimTip892 (Automatic incremental backup ...)
