3

Whenever I delete a file that I no longer need in emacs using delete-file command, the buffer associated with the file is still open in emacs. Is there a way I can change this behaviour so that whenever I delete a file, the buffer associated with it is also automatically deleted.

4

I wrote this method, which differs slightly from the one in the other answer:

(defun delete-file-visited-by-buffer (buffername)
  "Delete the file visited by the buffer named BUFFERNAME."
  (interactive "b")
  (let* ((buffer (get-buffer buffername))
         (filename (buffer-file-name buffer)))
    (when filename
      (delete-file filename)
      (kill-buffer-ask buffer))))

If you have the buffer open in Emacs, I find it easier to specify the buffer to delete, mainly because Emacs defaults to the current buffer.

So if point is in the buffer you want to delete, you can just hit enter, rather than having to type in the filename you want to delete.

2

You could use this function instead of delete-file

(defun delete-file-kill-buffer (filename)
  "Delete the file buffer is visiting and kill the buffer."
  (interactive "fFile name: ")
  (progn
    (delete-file filename)
    (message "Deleted file %s" filename)
    (when (find-buffer-visiting filename)
      (kill-buffer (find-buffer-visiting filename)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.