Emacs creates a backup only when you save the first time. This incompatible with most people’s basic editing behavior of leaving Emacs and its buffers open all the time and constantly making changes and writing them to disk.
This code will have Emacs backup each time you save. Mix this with a separate BackupDirectory for extra whiz-bang.
(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)
See also BackupEachSave which is probably similar but has more features and is maybe more stable.
The above function force-backup-of-buffer doesn’t preserve file permissions. As an alternative, how about simply:
(defun force-backup-of-buffer () (setq buffer-backed-up nil))
Keeping the N new and N old versions of the file is kind of brain dead. Here’s a simulation in Emacs Lisp of 8 saves using lists, showing what backups are available after each save when N is 2.
(let ((saves (number-sequence 1 8)) (backups '())) (while (car saves) (setq backups (cons (car saves) backups)) (pp backups) (unless (< (length backups) (+ kept-old-versions kept-new-versions)) (setq backups (append (butlast backups (- (1+ (length backups)) kept-new-versions)) (last backups kept-old-versions)))) (setq saves (cdr saves)))) => (1) (2 1) (3 2 1) (4 3 2 1) (5 4 2 1) (6 5 2 1) (7 6 2 1) (8 7 2 1)