lundi 17 avril 2017

Emacs backups

The other day I really scared myself by almost losing 1 hour of uncommited work. I've mixed up some commands, redirected to the wrong file, even reloaded my buffer and emacs and my content was gone. I eventually found a version of the file displayed in another terminal, but it was closed.
 
In 2017, GNU/Linux doesn't have a filesystem with snapshots (not talking about btrfs).
 
So it turns out that emacs can do that. It normally automatically saves unsaved changes into a buffer, but as soon as you save it, you loses that history.

So now I have emacs keep some versions of the file, just it case.
Here's what it looks like. (If you don't have it set, use ls -B to hide all kind of backup files.)

-rw------- 1  8,9K mars  25 16:44 in30.md.~1~
-rw------- 1  8,9K mars  25 16:55 in30.md.~2~
-rw------- 1   12K avril 17 10:13 in30.md.~67~
-rw------- 1   12K avril 17 19:16 in30.md.~68~
-rw------- 1   12K avril 17 19:29 in30.md.~69~
-rw------- 1   12K avril 17 19:40 in30.md.~70~
-rw------- 1   12K avril 17 19:40 in30.md.~71~
-rw------- 1   12K avril 17 19:47 in30.md.~72~
-rw------- 1   12K avril 17 19:47 in30.md


And there's what I've added to my ~/.emacs.d/init.el

;; if you wish to keep all backup files into a central directory
;; (setq backup-directory-alist `(("." . "~/.saves")))

(setq vc-make-backup-files t)
(setq delete-old-versions t
      kept-new-versions 6
      kept-old-versions 2
      version-control t
      backup-by-copying-when-linked t)

(defun force-backup-of-buffer ()
  (setq buffer-backed-up nil))

(add-hook 'before-save-hook  'force-backup-of-buffer)