Xah Talk Show 2026-02-21 Ep765. emacs lisp coding. Command to delete emacs backup

Video Summary (Generated by AI, Edited by Human.)

The video focuses on Emacs Lisp coding (0:08) and the creation of a command to delete Emacs backup files (0:49) within a directory, including subdirectories (1:45). The host, Xah Lee, explains that Emacs backup files are identified by a tilde (~) at the end of their name (0:59).

Key aspects of the video include:

;; before

(defun xah-list-emacs-backup (&optional InputDir)
  "List file names ending in tilde ~ , in current dir, recursively.
Display a list of file paths.
Return a list of file paths.
Created: 2023-09-13
Version: 2025-03-04"
  (interactive (list default-directory))
  (message "Searching backup ...")
  (let ((xpaths (directory-files-recursively InputDir "~\\'"))
        (xbuf (get-buffer-create "*emacs backup files*" t)))
    (if (length> xpaths 0)
        (progn
          (with-current-buffer xbuf
            (erase-buffer)
            (insert (mapconcat 'identity xpaths "\n")))
          (switch-to-buffer xbuf))
      (message "No backup file found"))
    xpaths
    ))

(defun xah-delete-emacs-backup (InputDir &optional Interactp)
  "Delete files whose name ends in ~ , in current dir, recursively.
Created: 2023-09-13
Version: 2025-02-05"
  (interactive (list default-directory t))
  (let ((xpaths (xah-list-emacs-backup InputDir)))
    (if Interactp
        (if (length> xpaths 0)
            (when (y-or-n-p "Delete these emacs backup?")
              (mapc 'delete-file xpaths)
              (message "Done delete backup files, if any."))
          (message "No backup file found"))
      (mapc 'delete-file xpaths))))
;; new

(defun xah-delete-emacs-backup (InputDir)
  "Move all emacs backup files in current dir, recursively.
backup moved to `user-emacs-directory', in a dir named xah_temp_backup
e.g. ~/.emacs.d/xah_temp_backup/

Emacs backup files are files whose name ends in TILDE ~.

Created: 2026-02-21
Version: 2026-02-21"
  (interactive (list default-directory))
  (let ((xpaths (directory-files-recursively InputDir "~\\'"))
        (xdir (file-name-as-directory (concat user-emacs-directory "xah_temp_backup/" (format-time-string "d%Y-%m-%d_%H%M%S"))))
        (xindex-name "index.txt~~~"))
    (when (not (file-exists-p xdir)) (make-directory xdir t))
    (with-temp-file (concat xdir xindex-name) (insert (mapconcat 'identity xpaths "\n")))
    (mapc (lambda (x) (rename-file x (concat xdir (file-name-nondirectory x)) t)) xpaths)
    (message "backup moved to %s" xdir)
    (find-file (concat xdir xindex-name))))