Emacs Lisp: Delete Current File
Here's a emacs command that deletes the current file.
(defun xah-delete-current-file () "Delete the file associated with the current buffer and close the buffer. Also push file content to `kill-ring'. If buffer is not file, just close it, and push file content to `kill-ring'. URL `http://ergoemacs.org/emacs/elisp_delete-current-file.html' Version 2015-08-12" (interactive) (progn (kill-new (buffer-string)) (when (buffer-file-name) (when (file-exists-p (buffer-file-name)) (progn (delete-file (buffer-file-name)) (message "Deleted: 「%s」." (buffer-file-name))))) (let ((buffer-offer-save nil)) (set-buffer-modified-p nil) (kill-buffer (current-buffer)))))
This command is very convenient if you code dynamic language such as Python and often creates temp testing scripts.
Use it together with Emacs Lisp: Run Current File.
Also, you might want a command to make backup. Emacs Lisp: Make Backup of Current File
Also, it's useful when using emacs to view images. 〔➤see Emacs: View Image Files in Emacs〕
Here's another version. It makes a backup first, and does not push to emacs's clipboard.
(defun xah-delete-current-file-make-backup (&optional φno-backup-p) "Delete the file associated with the current buffer (also closes the buffer). A backup file is created with filename appended “~‹date time stamp›~”. Existing file of the same name is overwritten. If the file is not associated with buffer, the backup file name starts with “xx_”. When called with `universal-argument', don't create backup. URL `http://ergoemacs.org/emacs/elisp_delete-current-file.html' Version 2015-05-26" (interactive "P") (let* ( (ξfname (buffer-file-name)) (ξbuffer-is-file-p ξfname) (ξbackup-suffix (concat "~" (format-time-string "%Y%m%dT%H%M%S") "~"))) (if ξbuffer-is-file-p (progn (save-buffer ξfname) (when (not φno-backup-p) (copy-file ξfname (concat ξfname ξbackup-suffix) t)) (delete-file ξfname) (message "Deleted. Backup created at 「%s」." (concat ξfname ξbackup-suffix))) (when (not φno-backup-p) (widen) (write-region (point-min) (point-max) (concat "xx" ξbackup-suffix)) (message "Backup created at 「%s」." (concat "xx" ξbackup-suffix)))) (kill-buffer (current-buffer))))