Emacs Lisp: Delete Current File

, , …,

Here's a emacs lisp command that deletes the current file.

(defun xah-delete-current-file (ξno-backup-p)
  "Delete the file associated with the current buffer.

Also close the current buffer.  If no file is associated, just close buffer without prompt for save.

A backup file is created with filename appended “~‹date time stamp›~”. Existing file of the same name is overwritten.

When called with `universal-argument', don't create backup."
  (interactive "P")
  (let* (
         (fName (buffer-file-name))
        (backupName (concat fName "~" (format-time-string "%Y%m%d_%H%M%S") "~")))
    (when fName ; buffer is associated with a file
      (save-buffer fName)
      (if ξno-backup-p
          (progn )
        (copy-file fName backupName t)
      (delete-file fName)
      (message "deleted and backup created at 「%s」." backupName)
    (kill-buffer (current-buffer))
    ) )

This command is very convenient if you code {python, ruby, …} and often creates temp testing scripts. Use it together with Emacs Lisp: Execute/Compile Current File. Also, it's useful when using emacs to view images. 〔☛ Emacs 24.4: Use Emacs as Image Viewer

Here's explanation of the lisp code:

blog comments powered by Disqus