Emacs: Delete Current File 🚀
Here's emacs commands that delete the current file.
(defun xah-delete-current-file-make-backup () "Makes a backup~, delete current file, close the buffer. Backup filename is “‹name›~‹dateTimeStamp›~”. Overwrite existing file. If buffer is not a file, copy content to `kill-ring', delete buffer. If buffer is not a file, the backup file name starts with “xx_”. Call `xah-open-last-closed' to open the backup file. URL `http://xahlee.info/emacs/emacs/elisp_delete-current-file.html' Version: 2018-05-15 2024-04-21 2024-04-23" (interactive) (when (eq major-mode 'dired-mode) (user-error "%s: In dired. Nothing is done." real-this-command)) (let ((xfname buffer-file-name) (xbuffname (buffer-name))) (if xfname (let ((xbackupPath (format "%s~%s~" xfname (format-time-string "%Y-%m-%d_%H%M%S")))) (save-buffer xfname) (kill-buffer xbuffname) (rename-file xfname xbackupPath t) (message "File deleted. Backup at %s Call `xah-open-last-closed' to open." xbackupPath) (when (boundp 'xah-recently-closed-buffers) (push (cons nil xbackupPath) xah-recently-closed-buffers))) (progn (widen) (kill-new (buffer-string)) (kill-buffer xbuffname) (message "non-file buffer killed. buffer text copied to `kill-ring'.")))) (when (eq major-mode 'dired-mode) (revert-buffer)))
The following related commands are useful together:
Also, it's useful when using emacs to view images. 〔see Emacs: View Image File〕