Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 45 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nepsilon/003dd7cfefc20ce1e894db9c94749755 to your computer and use it in GitHub Desktop.
Save nepsilon/003dd7cfefc20ce1e894db9c94749755 to your computer and use it in GitHub Desktop.
Auto-backup your configuration files with Vim — First published in fullweb.io issue #71

Auto-backup your configuration files with Vim

Not using versioning on your configuration files and editing them with Vim?

Use Vim’s backup option to automatically keep a copy of past versions. To put in your ~/.vimrc:

"Turn on backup option
set backup

"Where to store backups
set backupdir=~/.vim/backup//

"Make backup before overwriting the current buffer
set writebackup

"Overwrite the original backup file
set backupcopy=yes

"Meaningful backup name, ex: filename@2015-04-05.14:59
au BufWritePre * let &bex = '@' . strftime("%F.%H:%M")

From now on, each time you’ll save a file (hit :w), Vim will save a copy in ~/.vim/backup/ with this syntax your-file.py@2016-10-25.14:58.

This honestly saves my life about once a year 😁.

@vindex10
Copy link

Its really awesome solution but eventually one will have to manually delete the older backup. is there a way to delete the files older than 7-14 days while initialising vim?

maybe something like this could help:

silent execute '!find $HOME/.vimtmp/backup -type f -mtime +7 -delete'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment