I want to create a backup of the file I'm editing before each write. For example, if I am editing some file.txt, when I go to write the file with :w, I want vim to first save a backup of file.txt within a directory (i.e. ~/.vim_backups/) as file_2013-01-01_01-01-01.txt, where this represents YYYY-MM-DD_HH-MM-SS.txt. I am aware of alternatives such as version control, but I'm curious if this is possible in vim.


This vim wikia article titled: Keep incremental backups of edited files, sounds like what you're looking for. There's this method which attaches a save routine so that it's mapped to the Esc key.

With the following mapping:

   fun! InitBex()
     let myvar = strftime("(%y%m%d)[%Hh%M]")
     let myvar = "set backupext=_". myvar
     execute myvar
     echo myvar
    map <Esc> :call InitBex()<CR>

You can easily refresh the backupextension time everytime you hit the Esc key. So you can get backups every minute, every hour, every day and so on.

There are several other methods discussed in the Wiki topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.