Hacker News new | past | comments | ask | show | jobs | submit login

I use this, in my .vimrc:

  :set backup
  :set backupdir=/home/kaz/.vimbackup
  :let &g:backupext=("." . strftime("%y-%m-%d.%H:%M:%S"))
That's it. No messing around with git; protects files that are not in git, and can save you even in he face of "rm -rf .* *".



There is also the `undofile` option, which stores your undo-history permanently on disk. You can also go to an earlier version of the file from 4 hours ago with `:earlier 4h`.


How good is that for boo-boo situations that you caused outside of the editor entirly? Like deleting a file?


I suppose you know the answer, but in case anyone wonders: It won't help you. You can of course use it together with the `backup` option.


It's impossible not to note that emacs does this well too. Setting

   (setq backup-directory-alist `(("." "/home/user/some/dir/you/chose/backup/))
         version-control t
         kept-new-versions 50
         kept-old-versions 50)
sets up a directory with many versions of every file ever edited. Additionally

   (setq auto-save-file-name-transforms `((".*" "/home/user/some/dir/you/chose/auto-save/" t)))
does the same for even unsaved changes.


Vim doesn't do it "well" with the settings above, just "good enough". The time stamp suffix is calculated just once when you start Vim, set in the global backupext option. If we :e edit files in an existing Vim session, they all get backups with that time stamp. It needs to be calculated as a buffer-specific value of that option, if there is such a thing, on every new edit.


I just found that the Vim manual recommends, right in the doc for backupext, the use of the BufWritePre hook instead, so:

  :au BufWritePre * let &backupext=("." . strftime("%y-%m-%d.%H:%M:%S"))
This has the right effect that every time you save with :w, a new backup is made of the previous contents.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: