Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
    
Previous version of my answer was similar emacs.stackexchange.com/posts/26278/revisions, but your is better. – Konstantin Morenko yesterday

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)))))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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