我每次保存时如何进行Emacs备份?(How can I make Emacs backup every time I save?)

我不时删除我不应该写的文件,而最糟糕的是我自己写的文件。 因此,我有很多次被Emacs的备份功能保存。

但我的问题是,Emacs只是在您第一次保存缓冲区时才进行备份。 每次按Cx Cs时,有没有办法让Emacs做到这一点?

这就是我的.emacs当前的样子(只有处理备份的部分):

*剪辑*

;; =====备份=====

;; 启用备份文件。 (setq make-backup-files t)

;; 将所有备份文件保存在此目录中。 (setq backup-directory-alist(quote((“。*”。“〜/ .emacs_backups /”))))

;; 始终通过复制备份(最安全,但最慢) (setq备份复制t)

;; 在保存备份时追加〜1〜(和增加数字)到文件结尾 (setq版本控制t)

;; 定义要保留多少个旧版本的文件(从文件开始) ;; 最近的,倒计时的 (setq保持新版本100)

*剪辑*

From time to time I delete files that I shouldn't and worst is files that I've been writing myself. Therefore I have many times been saved by the backup feature of Emacs.

But my problem is that Emacs only makes a backup the very first time you save a buffer. Is there a way to make Emacs do it every time I press C-x C-s?

This is what my .emacs look like currently (only the part that deals with backups):

* snip *

;; ===== 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)

* snip *

最满意答案

阅读本文后: EmacsWiki:强制备份

我将这些行添加到我的.emacs中:

(defun force-backup-of-buffer () (setq buffer-backed-up nil)) (add-hook 'before-save-hook 'force-backup-of-buffer)

它使用标准备份/版本控制,但在保存之前重置指示缓冲区已在此会话中备份的标志。

前两行定义了一个函数,用于重置指示在此会话期间备份缓冲区的标志。

最后一行添加一个事件挂钩,在保存之前执行该函数。

这正是我想要的。

After reading this: EmacsWiki: Force Backups

I added these lines to my .emacs:

(defun force-backup-of-buffer () (setq buffer-backed-up nil)) (add-hook 'before-save-hook 'force-backup-of-buffer)

It utilizes the standard back up/version control but resets the flag that indicates wether or not the buffer has been backed up this session before a save.

First two rows define a function that resets the flag that indicates wether the buffer was backed up during this session.

Last row adds an event hook that executes the function before a save.

This does exactly what I wanted.

更多推荐