我有时会删除不该删除的文件,最糟糕的是我自己编写的文件。所以很多次我都被Emacs的备份功能救了。
但我的问题是,Emacs 仅在您第一次保存缓冲区时进行备份。有没有办法让 Emacs 在我每次按 C-x C-s 时执行此操作?
这就是我的 .emacs 目前的样子(仅处理备份的部分):
;; ===== Backups =====
;; Enable backup files.
(setq make-backup-files t)
;; Save all backup file in this directory.
(setq backup-directory-alist (quote ((".*" . "~/.emacs_backups/"))))
;; Always backup by copying (safest, but slowest)
(setq backup-by-copying t)
;; Append .~1~ (and increasing numbers) to end of file when saving backup
(setq version-control t)
;; Defining how many old versions of a file to keep (starting from the
;; most recent and counting backward
(setq kept-new-versions 100)
阅读完本文后:EmacsWiki:强制备份
我将这些行添加到我的 .emacs 中:
(defun force-backup-of-buffer ()
(setq buffer-backed-up nil))
(add-hook 'before-save-hook 'force-backup-of-buffer)
它利用标准备份/版本控制,但会重置指示在保存之前缓冲区是否已备份此会话的标志。
前两行定义一个函数,用于重置指示缓冲区在此会话期间是否已备份的标志。
最后一行添加了一个事件挂钩,该事件挂钩在保存之前执行该函数。
这正是我想要的。
如果您想自己做,请从这里开始:
(defun backup-and-save ()
(interactive)
(setq filename (buffer-file-name))
(write-file (concat filename (format-time-string "_" "%Y%m%d%H%M%S")))
(write-file filename)
)
它将副本保存为与时间戳相关的
originalfilename_timestamp
。
您当然可以调整它以将其存储在单独的备份文件夹中或添加其他“调整”。
这是我用的。它将备份放在目录的子目录中 文件在。每次保存文件时,它还会保存备份。
(setq make-backup-files t ; backup of a file the first time it is saved.
backup-by-copying t ; don't clobber symlinks
version-control t ; version numbers for backup files
delete-old-versions t ; delete excess backup files silently
delete-by-moving-to-trash t
kept-old-versions 6 ; oldest versions to keep when a new numbered
; backup is made (default: 2)
kept-new-versions 9 ; newest versions to keep when a new numbered
; backup is made (default: 2)
auto-save-default t ; auto-save every buffer that visits a file
auto-save-timeout 20 ; number of seconds idle time before auto-save
; (default: 30)
auto-save-interval 200 ; number of keystrokes between auto-saves
; (default: 300)
)
;; make backup to a designated dir, mirroring the full path
;; backup to dir hung on source dir...
(add-hook 'write-file-hooks 'setBackUp)
(defun setBackUp ()
"Called by before-save-hook to set up backup location"
(defvar backupdirname "BackUps~") ; you can chose the sub dir name here
(if (not (file-exists-p backupdirname))
(make-directory backupdirname t))
(setq backup-directory-alist `(("." . ,backupdirname)))
(setq buffer-backed-up nil) ; force backup every save
nil
)