Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active February 24, 2022 15:21
Show Gist options
  • Save nepsilon/1c998cd95907ef5d2d29 to your computer and use it in GitHub Desktop.
Save nepsilon/1c998cd95907ef5d2d29 to your computer and use it in GitHub Desktop.
Using auto backup with Vim — First published in fullweb.io issue #3

Using auto backup 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")
Copy link

ghost commented Mar 11, 2019

This will also work as a sort of 'incremental backup'... Wow... Thank you very much!

@antonyjohnson-js
Copy link

antonyjohnson-js commented Nov 2, 2019

Hi @nepsilon
if i want to backup a file before editing after the editing with the unique name .. how can i achive that? :| can you please help me on this..
and this is not working for me (au BufWritePre * let &bex = '@' . strftime("%F.%H:%M")) :|
Looking forward your responce...

@Maurits2014
Copy link

Hi @nepsilon
if i want to backup a file before editing after the editing with the unique name .. how can i achive that? :| can you please help me on this..
and this is not working for me (au BufWritePre * let &bex = '@' . strftime("%F.%H:%M")) :|
Looking forward your responce...

Try using seconds in your filename. Simply add "%S" at the end of your vimscript, and the filename will be virtually guaranteed to be unique. Maybe it is even possible to add microseconds for 100% uniqueness.

@antonyjohnson-js
Copy link

antonyjohnson-js commented Nov 4, 2019

@Maurits2014 Thanks for your reply!!!

is this support to vi editor? i have added these line in my (/etc/virc)

"Turn on backup option
set backup

"Where to store backups
set backupdir=~/.vi/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:%S")

i am testing it in vi editor only... i need to take a file backup before/after changes with the unique name for example:
if a file name is httpd.conf
before editing httpd.conf-date-before
after editing httpd.conf-date-after

is this possible?

@cig0
Copy link

cig0 commented Dec 31, 2020

When I open my vimrc file after adding this snippet, I get:

Error detected while processing modelines:
line   40:
E518: Unknown option: filename@2015-04-05.14

Note: I'm using SpaceVim

@ZYZ64738
Copy link

... its because the colon in front of "filename@2015-04-05.14". Anyway strange behavior for a "comment"
Remove it oder mask it with backslash.

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