Delete file and buffer in Emacs

Here’s a handy function that kills the current buffer and removes the file it is connected to.

(defun delete-this-buffer-and-file ()
  "Removes file connected to current buffer and kills buffer."
  (interactive)
  (let ((filename (buffer-file-name))
        (buffer (current-buffer))
        (name (buffer-name)))
    (if (not (and filename (file-exists-p filename)))
        (error "Buffer '%s' is not visiting a file!" name)
      (when (yes-or-no-p "Are you sure you want to remove this file? ")
        (delete-file filename)
        (kill-buffer buffer)
        (message "File '%s' successfully removed" filename)))))

I bound C-c k to the function:

(global-set-key (kbd "C-c k") 'delete-this-buffer-and-file)

blog comments powered by Disqus Back to Top

Fork me on GitHub