Emacs: Backup Current File 🚀

By Xah Lee. Date: . Last updated: .

Here's a command to backup the current file. It creates a copy of the file in the current dir, with file name ending in ~.

(defun xah-make-backup ()
  "Make a backup copy of current file or dired marked files.
If in dired, backup current file or marked files.
The backup file name is in this format
 The last part is hour, minutes, seconds.
in the same dir. If such a file already exist, it's overwritten.
If the current buffer is not associated with a file, nothing's done.

URL `http://xahlee.info/emacs/emacs/elisp_make-backup.html'
Version 2018-05-15"
  (let (($fname (buffer-file-name))
        ($date-time-format "%Y-%m-%d_%H%M%S"))
    (if $fname
        (let (($backup-name
               (concat $fname "~" (format-time-string $date-time-format) "~")))
          (copy-file $fname $backup-name t)
          (message (concat "Backup saved at: " $backup-name)))
      (if (string-equal major-mode "dired-mode")
            (mapc (lambda ($x)
                    (let (($backup-name
                           (concat $x "~" (format-time-string $date-time-format) "~")))
                      (copy-file $x $backup-name t)))
            (message "marked files backed up"))
        (user-error "buffer not file nor dired")))))
(defun xah-make-backup-and-save ()
  "Backup of current file and save, or backup dired marked files.
For detail, see `xah-make-backup'.
If the current buffer is not associated with a file nor dired, nothing's done.
URL `http://xahlee.info/emacs/emacs/elisp_make-backup.html'
Version 2015-10-14"
  (if (buffer-file-name)
        (when (buffer-modified-p)

Emacs: auto save, backup

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Xah Emacs Commands