Simple Version Control

About once every six months or so I spend the morning happily overwriting an existing piece of work. Either that or I unwittingly delete whole sections, only to discover my mistake later that day or worse, later that week or even month.

That’s were Emacs simple version control comes in handy. Add the following to your .emacs file: (courtesy of the Emacs Wiki)

(setq version-control t ;; Use version numbers for backups
       kept-new-versions 16 ;; Number of newest versions to keep
       kept-old-versions 2 ;; Number of oldest versions to keep
       delete-old-versions t ;; Ask to delete excess backup versions?
       backup-by-copying-when-linked t) ;; Copy linked files, don't rename.
 (defun force-backup-of-buffer ()
   (let ((buffer-backed-up nil))
     (backup-buffer)))
 (add-hook 'before-save-hook  'force-backup-of-buffer)

You can probably deduce from the comments what the above does, but as an illustration, here’s a sample of what the file I’m currently writing looks like under version control:

-rw-rw-r--  1 XXXX XXXX 29816 Aug 11  2014 EmacsWritingTips.org.~24~
.rw-rw-r--  1 XXXX XXXX 29816 Aug 11  2014 EmacsWritingTips.org.~25~
-rw-rw-r--  1 XXXX XXXX 28844 Aug 11  2014 EmacsWritingTips.org.~26~
-rw-rw-r--  1 XXXX XXXX 29322 Aug 11  2014 EmacsWritingTips.org.~27~
http://www.emacswiki.org/emacs/ForceBackups

The last 16 versions of the file are kept, the versions indicated by ~ ver ~

Having all of those versions can sometimes make it difficult to find files under dired. Fortunately, dired-x has a mode that omits uninteresting files. Add the following to .emacs

(require 'dired-x)
(setq dired-omit-mode t)

Now just hit C-x M-o (or M-o on older versions of Emacs) to omit those files.

16 past versions combined with my regular backup routine have proven enough for me to find the accidentally deleted text.  For a more in depth discussion  see http://www.emacswiki.org/emacs/ForceBackups

4 thoughts on “Simple Version Control

      1. I assume such VC backups can be centrally stored in one folder, the same as the emacs auto-backup features.

        It is a good habit to enable this but I am annoyed with so many ~ files since from time to time I also use terminal or file GUI to navigate. Perhaps I need to be heavily ass kicked one day to start using it :).

        Good post!

Leave a Reply

Your email address will not be published. Required fields are marked *