2009-05-15

When you delete files and directories in Emacs 23 (say, with dired), instead
of losing the files until the end of times (or at least until the
singularity), you can move them to the 'trashcan', by whatever name that rose
comes in your system, 'Trash' or 'Recycle Bin'…

To enable this, put the following in your .emacs:

(setq delete-by-moving-to-trash t)

There is one problem - a bug? I am using Ubuntu 9.04, which follows the
Freedesktop Trash Spec; it moves delete file in ~/.local/share/Trash,
together with some metadata, so it can restore the file to their original
location. However, emacs follows some older convention, ie. to move the file
to ~/.Trash, and without any metadata.

You can partially fix this by making ~/.Trash a symlink to
~/.local/share/Trash/files/, but of course that does not get you the
metadata.

Hmm. Using this can cause some problems. Files outside your home directory are untrashable. In particular, files in /tmp/emacs1000/. If you use emacsclient, emacs will be unable to trash the server socket on exit, and next time you start emacs, emaceclient will not work.

To fix this, we need a trash-or-rm command:

$ cat > ~/bin/trash-or-rm.sh#!/bin/sh

## This attempts to trash a file (or files), and then attempts to remove it if it still exists.

trash "$@" >/dev/null 2>&1rm -f "$@"[Control-D]

I hope this blog didn't munge any characters in that script, but I think you get the idea. Then,