Emacs: Delete Current File π
Here's emacs commands that delete the current file.
Always Make Backup Version
This version will always make a backup file, even when buffer is not a file.
(defun xah-delete-current-file-make-backup (&optional @no-backup-p) "Delete current file, makes a backup~, closes the buffer. Backup filename is ββΉnameβΊ~βΉ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 `universal-argument' is called first, don't create backup. URL `http://xahlee.info/emacs/emacs/elisp_delete-current-file.html' Version 2016-07-20" (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))))
Always Copy to kill-ring Version
This version will always copy buffer content to kill-ring (emacs's clipboard)
(defun xah-delete-current-file-copy-to-kill-ring () "Delete current buffer/file and close the buffer, push content to `kill-ring'. URL `http://xahlee.info/emacs/emacs/elisp_delete-current-file.html' Version 2016-07-20" (interactive) (progn (kill-new (buffer-string)) (message "Buffer content copied to kill-ring.") (when (buffer-file-name) (when (file-exists-p (buffer-file-name)) (progn (delete-file (buffer-file-name)) (message "Deleted file: γ%sγ." (buffer-file-name))))) (let ((buffer-offer-save nil)) (set-buffer-modified-p nil) (kill-buffer (current-buffer)))))
Smart Version
This version will create a backup file if the buffer is associated with a file, else copy content to kill-ring (emacs's clipboard).
(defun xah-delete-current-file (&optional @no-backup-p) "Delete current file. If buffer is a file, make a backup~, push content to `kill-ring' (unless buffer is greater than 1 mega bytes.), then delete it. If buffer is not associate with a file, push content to `kill-ring' (unless buffer is greater than 1 mega bytes.), then kill it. If buffer is dired, do nothing. URL `http://xahlee.info/emacs/emacs/elisp_delete-current-file.html' Version 2020-02-14 2021-08-06" (interactive "P") (if (eq major-mode 'dired-mode) (message "you in dired. nothing's done.") (let (($bstr (buffer-string))) (when (> (length $bstr) 0) (if (< (point-max) 1000000) (kill-new $bstr) (message "Content not copied. buffer size is greater than 1 megabytes."))) (if (buffer-file-name) (xah-delete-current-file-make-backup @no-backup-p) (when (buffer-file-name) (when (file-exists-p (buffer-file-name)) (progn (delete-file (buffer-file-name)) (message "Deleted file: γ%sγ." (buffer-file-name))))) (let ((buffer-offer-save nil)) (set-buffer-modified-p nil) (kill-buffer (current-buffer)))))))
These commands are useful if you code dynamic language such as Python and often create temp testing scripts.
My workflow is often to backup current version of script am working on, create a copy in new file, edit code for experiment, run it, and delete it when done.
The following related commands are useful together:
Also, it's useful when using emacs to view images. [see Emacs: View Image File]