如何在每次保存时对 Emacs 进行备份?

发布于 2024-11-27 16:42:33 字数 1005 浏览 1 评论 0原文

我有时会删除不该删除的文件,最糟糕的是我自己编写的文件。因此我多次被Emacs的备份功能救了。

但我的问题是,Emacs 仅在您第一次保存缓冲区时进行备份。有没有办法让 Emacs 在我每次按 Cx Cs 时执行此操作?

这就是我的 .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)

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):

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝礼 2024-12-04 16:42:33

阅读完此内容后: 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.

狂之美人 2024-12-04 16:42:33

如果您想自己执行此操作,可以从这里开始:

(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

当然,您可以调整它以将其存储在单独的备份文件夹中或添加其他“调整”。

If you want to do it on your own, here's a start:

(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)
  )

It saves a copy as originalfilename_timestamp in connection with a timestamp.

You might of course adjust it to store it in a separate backup folder or add other "tweaks".

禾厶谷欠 2024-12-04 16:42:33

这是我使用的。它将备份放在目录的子目录中
文件位于其中。每次保存文件时,它还会保存备份。

(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
  )

Here is what I use. It puts backups in a subdirectory of the directory
the file is in. It also saves a backup each time the file is saved.

(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
  )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文