Steven
Steven

Reputation: 31

VIM: How Can I Periodically (Automatically) Save a Backup Copy of the Current Working File?

I'm a relatively green vim user, and I presently use it in what is, by all indications from other vim-related posts, a fairly simplistic fashion. I'm hoping that there is an equally simple answer to my question, however I've not had much luck finding it. In any case here's my problem:

Being still somewhat new to vim there are times when, by virtue of an errant key stroke or command sequence, I tell vim to do something completely unexpected (delete a line, jump around, go into a weird menu thing, etc).
When this happens, I can usually undo or :q! my way out of trouble as needed to reassure myself that the file is safe from undesired changes.

However, last night I was editing a file containing several hundred lines of source code, and I got into one of these minor vim apoplexies, probably as a result of leaving the caps lock on by mistake.
Since I'm a habitual incremental work saver, I knew I would have done a :w very recently and so felt just fine quitting vim without saving the file again.
Problem solved, right? Wrong. When I tried to reopen the file, I got an error from the shell saying that the file could not be found, and sure enough it was gone!
Fortunately (but very unusually), I had saved my source to a duplicate file of ".copy" just moments before this happened, so in the end I lost maybe five minutes of work. Needless to say, however, I am unnerved by the possibility of this happening again.
So here is what I would like to know if you can help me with:

  1. Is there an easy way that I can set vim up to automatically, periodically save the file I'm currently working in to a separate file as a backup (say, <file>.c.copy) while continuing to work in the original open file?

(Easy to implement is the key factor for me on this one. If a suggestion for solving this would require me to divert much time or attention away from my current project to learn vim programming or implement highly esoteric cleverness, I suppose I might just do :w <file>.c.copy manually for now, until I have more time to develop my mastery of its wily ways.)

  1. Bonus Question: How the hell did I do that in the first place?! I still have no idea. Magic fingers, I guess. =)

(VIM Version 7.4.459, if that helps anyone)

Upvotes: 2

Views: 1227

Answers (2)

boobie_goodness
boobie_goodness

Reputation: 104

I solved this problem, for myself, with these undoubtedly imperfect vimrc lines

"----- backup ------------------------------------------------------
" this is a default. to be updated per file type
autocmd BufNewFile,BufNew,BufRead * let b:buDir = "/work/BACKUP/vimBackups/all/"
" this is a default, to be updated per file type
au BufNewFile,BufNew,BufRead * let b:buFilename = "some_unsaved_file"        
au            BufNew,BufRead * let b:buFilename = expand('%:t')             " ideal

" from help BufNew:
"   NOTE: When this autocommand is executed, the
"   current buffer "%" may be different from the
"   buffer being created "<afile>".

" the backup command
" currently, overwrite whatever is there: a new file will be made each second
" if necessary
autocmd VimLeavePre,BufWritePre * silent execute ":write! >> ".b:buDir."vim_bu_".strftime('%Y-%b-%d__%H:%M:%S')."_".b:buFilename.".bu.txt "

and then, in sections for individual filetypes I have

let b:buDir = "/work/BACKUP/vimBackups/email/"

and

let b:buDir = "/work/BACKUP/vimBackups/coding/"

and

let b:buDir = "/work/BACKUP/vimBackups/text/"

and ... you get the idea.

and I have a line in a script run once a day by anacron that cleans out old backups:

find $VIM_BU_DIR -not -newerat "1 month ago" -delete ;

Upvotes: 0

mMontu
mMontu

Reputation: 9273

I would try to solve your problem with another approach: undo any unintended change you done and avoid :q!.

Most of the time hitting u should undo your changes. But I assume that you sometimes hit random keys that make it difficult to use only u to undo the accidental changes (e.g.: code ok --> accidental keys undo part of the good code and then insert crap text). As vim has an undo-tree (check :h undo-branches) it is still possible to recover the correct version without using :q!. Some plugins, such as undotree, make it very easy to navigate to all versions on the undo history.

For your second question, it is possible that you entered some system command that removed the current file, such as :!rm %. But if you doesn't quit vim after undoing the mess and saves the file again (:w) you won't lose your file (unless you manage to quit vim with your accidental typing, but that is probably very unusual - and it is still possible that you can recover your file if you have 'undofile' set -- check :h persistent-undo).

Upvotes: 1

Related Questions