Delete to Trash from Within Emacs

Generally you have to work hard to delete a file from within Emacs but it is possible. The most common way is probably with Dired but my favorite is with Magit. Regardless, once you get used to using Dired or Magit to delete files it can be distressingly easy to inadvertently delete a file.

The problem is that with vanilla Emacs the file is gone when you do. Most modern OS file delete operations move the file to the Trash folder where it can be retrieved but that doesn’t happen when you delete from within Emacs.

Álvaro Ramírez has a solution. It’s possible to tell Emacs to move files to the Trash folder instead of actually deleting them when asking Emacs to remove them. Ramírez’s solution is specific to macOS but the same idea mutatis mutandis should work for any OS that has a Trash folder.

I’ve known about this hack for some time but have never bothered to implement it. That’s just foolish because sooner or later I’m going to lose a file and wish I hadn’t been so lazy. Don’t be like me: tell Emacs to make your deletes provisional. That way if you inadvertently delete a file it will be easy to recover. The change is easy to implement and there really isn’t any reason not to install it.

This entry was posted in General. Bookmark the permalink.