reddit / emacs

Deleting files in Emacs [OC]

zck · 2022-08-03 02:32
tonicinhibition · 2022-08-03 03:31
This is hilarious. I honestly can't tell if it's satire or not. > You risk accidentally saving the buffer again, resurrecting the file. Comedy gold. In all seriousness I almost wrote a nearly identical function once because I was typing `M-x delete-file`, and then having to select the name and kill the buffer. That very day while testing my function I fat fingered and deleted some client work - and I became enlightened.
zck · 2022-08-03 03:36
No, I'm pretty serious. When I'm deleting files (especially files in source control), I want them gone from my Emacs too.
flyin1501 · 2022-08-03 03:39
I've had a custom `delete-this-file` command (don't remember where I got it from, I don't think I wrote it myself) for so long I forgot it's not built in. (defun k/delete-this-file (&optional trash) "Delete this file. When called interactively, TRASH is t if no prefix argument is given. With a prefix argument, TRASH is nil." (interactive) (when (and (called-interactively-p 'interactive) (not current-prefix-arg)) (setq trash t)) (if-let ((file (buffer-file-name))) (when (y-or-n-p "Delete this file? ") (delete-file file trash) (kill-buffer (current-buffer))) (user-error "Current buffer is not visiting a file"))) Yes, this command should indeed be built in.
tonicinhibition · 2022-08-03 03:52
I see that by using `(interactive "bWarning Message")` Emacs gives you a buffer list and selects the current buffer by default. I have a workflow where I'm regularly deleting journals (dailies) in Org-Roam. The function `delete-file` doesn't have this behavior, so I have to scroll down through two dozen similarly named files. Your function would eliminate that problem entirely. I might use it after all. :-)
organic-bookworm · 2022-08-03 05:37
A tangential side note for Doom users. There's `doom/delete-this-file` command (`SPC f D` by default), which also updates caches and state for `projectile`, `vc`, `magit`, `recentf` and `save-place`.
nv-elisp · 2022-08-03 07:20
more succintly: (defun delete-this-file (&optional forever) "Delete the file associated with `current-buffer'. If FOREVER is non-nil, the file is deleted without being moved to trash." (interactive "P") (when-let ((file (or (buffer-file-name) (user-error "Current buffer is not visiting a file"))) ((y-or-n-p "Delete this file? "))) (delete-file file (not forever)) (kill-buffer (current-buffer))))
rockatanescu · 2022-08-03 08:28
A similar functionality is in the [crux](https://github.com/bbatsov/crux) package (see `crux-delete-file-and-buffer`).
DefiantAverage1 · 2022-08-03 13:05
A problem I had myself was that after file deletion, `better-jumper` keeps the buffer in its jump list so I kept going back to the file when navigating my jump list backwards so I wrote https://github.com/anonimitoraf/doom.d/blob/master/config-sections/misc.org#remove-jumps-from-jump-list-on-buffer-kill
zck · 2022-08-03 15:24
I wonder why delete-file doesn't update the state of built-in methods like `vc`. Seems like it would make sense to.
CulturMultur · 2022-08-03 21:33
That mostly doesn't work for me as it uses `vc-delete-file` for version-controlled files, and it complains that first I need to commit the file. So I did it using dired, which also deletes the buffer, but now will use the function from the post.
The9thHuman · 2022-08-07 06:44
They're on D+ in the UK.