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://ergoemacs.org/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://ergoemacs.org/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)))))
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 buffer/file. If buffer is a file, makes a backup~, else, push file content to `kill-ring'. If buffer is dired, go up a dir and mark it for delete (by `dired-flag-file-deletion'). (press 【x】 to call `dired-do-flagged-delete' to actually delete it) This commands calls `xah-delete-current-file-make-backup' or `xah-delete-current-file-copy-to-kill-ring'. If next buffer is dired, refresh it. URL `http://ergoemacs.org/emacs/elisp_delete-current-file.html' Version 2017-08-27" (interactive "P") (if (eq major-mode 'dired-mode) (progn (dired-up-directory) (dired-flag-file-deletion 1) (revert-buffer)) (progn (if (buffer-file-name) (xah-delete-current-file-make-backup @no-backup-p) (xah-delete-current-file-copy-to-kill-ring))) (when (eq major-mode 'dired-mode) (revert-buffer))))
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 commands are useful for this.
A command to make backup on demand, see Emacs: Backup Current File.
Also, it's useful when using emacs to view images. [see Emacs: View Image File]