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:
- Initial Command Functionality (2:20): Lee demonstrates an existing command that deletes backup files and confirms the action with the user. He then explains his intention to modify this command.
- Code Modification for Independence (3:34): Lee refactors the code to remove dependencies, ensuring the command functions independently without relying on other Emacs Lisp functions. This involves embedding the listing of backup files directly into the deletion command.
- User Interface Design Principles (17:24): A significant portion of the video is dedicated to discussing good user interface design. Lee argues against asking for user confirmation before executing a command, advocating instead for immediate execution with an undo feature. He emphasizes "muscle memory" (18:44) for efficient user interaction.
- Moving Files Instead of Deleting (23:23): To align with the user interface design principles, Lee modifies the command to move backup files to a temporary directory rather than permanently deleting them. This provides an implicit "undo" capability by retaining the files.
- Emacs vs. VS Code Discussion (29:16): Lee briefly compares Emacs to VS Code, highlighting Emacs's lower memory usage as a significant advantage for dedicated programmers.
- Addressing Challenges and Debugging (43:54): Lee encounters and debugs several errors during the coding process, demonstrating his problem-solving approach.
- Unix Philosophy Critique (56:19): Lee critiques the "Unix philosophy" of using shell commands for file operations, arguing that Emacs provides a more integrated, visual, and cross-platform solution without external dependencies.
- Successful Command Implementation (1:09:07): Despite various digressions and debugging, Lee successfully implements the modified command, which moves backup files to a designated temporary directory without user confirmation.
- Future Considerations: Undo Functionality (1:09:35): Lee discusses the complexities of implementing a robust undo feature for file operations in Emacs, as the built-in undo is primarily for text editing. He also briefly touches on the issues with file deletion and trash functionality in Linux (1:13:50).
;; 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))))