2

I have (setq trash-directory "~/.Trash") and (setq delete-by-moving-to-trash t) in .emacs to be able to have files moved to ~/.Trash via d and then x in dired-mode. (This is in macOS.)

But if I want to delete a file (= move to the trash) that already exists in ~/.Trash, then I get error file-already-exists: File already exists: /Users/<myuser>/.Trash/<myfile>.

How can I specify in .emacs to (force-)overwrite files in ~/.Trash if they already exist in ~/.Trash?

Alternatively, how can I get Dired d-then-x to create a ~1, ~2 etc. version in Trash, every time a new file of the same base-name is moved there?

Drew
  • 75,699
  • 9
  • 109
  • 225
Marius Hofert
  • 349
  • 1
  • 9
  • It all should work as you request. what exact version of emacs have you got? Also the only thing I have different is I don't set trash-directory and just leave it as nil – mmmmmm Jan 03 '22 at 18:14
  • Hi, thanks for your help. I have `GNU Emacs 26.3 (build 1, x86_64-apple-darwin18.2.0, NS appkit-1671.20 Version 10.14.3 (Build 18D109)) of 2019-09-02`. I also tried it without `(setq trash-directory "~/.Trash")`. I can then indeed delete a file, but I don't see it in ~/.Trash (so also not overwriting one that's already in ~/.Trash). I perhaps simply removes the file then or uses some other trash I'm not aware of. – Marius Hofert Jan 03 '22 at 18:28
  • The function that should be called is move-file-to-trash SO try running atht and see if it does what you want – mmmmmm Jan 03 '22 at 18:32
  • Ah, this failed (instead of the current file at point or selected, it tries to delete the whole folder the current file resides in, which fails due to missing permissions). But I saw from the error message that the 'trash' it that's used is `~/.local/share/Trash/files` instead of `~/.Trash`. So just the wrong folder so to say (but then changing that with `trash-directory` creates the original problem – Marius Hofert Jan 03 '22 at 18:36
  • Then I think you have set something special - does this happen with `emacs -Q` – mmmmmm Jan 03 '22 at 18:49
  • Ah, I need to select the file again when I do M-x move-file-to-trash, okay, that clarifies that. But then both with my .emacs and with emacs -Q, the behavior remains the same: The file is moved to the wrong trash (and deleting ~/.local/share/Trash just recreates it) – Marius Hofert Jan 03 '22 at 19:09
  • Does this answer your question? [Delete files to Trash on OS X](https://emacs.stackexchange.com/questions/15009/delete-files-to-trash-on-os-x). Had you mentioned that you're on OSX, the fact that this is a duplicate question might have been discovered sooner. – Drew Jan 03 '22 at 19:41
  • Thanks, Drew, yes (see my answer). I didn't update my post since macOS as OS was clear from my second comment above (or so I thought). – Marius Hofert Jan 03 '22 at 20:20

1 Answers1

0

Thanks to user mmmmmm, I found the solution. I saw here that it's a macOS problem. Installing osx-trash (via brew install osx-trash or via MELPA) and then using

(when (eq system-type 'darwin)
  (osx-trash-setup))
(setq delete-by-moving-to-trash t)

in .emacs solved the problem (it now moves files to ~/.Trash and creates temporary names in ~/.Trash in case there is already a file with the same name).

Marius Hofert
  • 349
  • 1
  • 9