Deleting reproducible files in Emacs dired

Imagine you could list the contents of a directory from a command line, and then edit the text output to make things happen. That’s sorta how Emacs dired works. It’s kind of a cross between a bash shell and the Windows File Explorer. Why would you ever want to use such a bizarre hybrid?

One reason is to avoid context switching. If you’re editing a file, you can pop over to a new buffer that is your file manager, do what you need to do, then pop back, all without ever leaving Emacs.

Another reason is that, as with everything else in Emacs, it’s all text. Everything in Emacs is just text, and so the same editing commands can be used everywhere. (More on that here.)

Even though I use Emacs daily, and even though I can make a case for why dired is great, I don’t use it that much. Or rather, I don’t use that much of what it can do. The good that I would, I do not.

I was reviewing dired‘s features and discovered something very handy: typing %& will mark files for deletion that can easily be created again. In particular, it will flag the byproducts of compiling a LaTeX file.

For example, the following is a screenshot of a dired buffer.

When I type %& it highlights the LaTeX temp files in red and marks them for deletion. (The D’s in the left column indicate files to be deleted.)

This doesn’t delete the files, but it marks them for deletion. If I then type x the files will be deleted.

In addition to the unneeded LaTeX files, it also highlighted a .bak file. However, it did not highlight the .o object file. I suppose the thought was that most people would manage C programs from a make file. I’m sure the class of files to mark is configurable, like everything else in Emacs.

More Emacs posts

3 thoughts on “Deleting reproducible files in Emacs dired

  1. I spend a lot of time in dired mode. It’s a really nice way to navigate and manipulate directory trees without having to bring up a graphical file explorer. Plus of course the flexibility of Emacs lets you do things like display multiple subdirectories of your choice in a single buffer.

    I highly recommend checking out wdired mode, which makes a dired buffer editable (under constraints), which is really handy for some tasks like batch-renaming files.

  2. Like everything else in Emacs, of course it is configurable!

    Running C-h k %& in Dired (describe-key) reveals the command bound to %& is called `dired-flag-garbage-files`, and it flags for deletion all files that match the pattern stored in `dired-garbage-files-regexp`. You can set that variable to whatever you like.

Leave a Reply

Your email address will not be published. Required fields are marked *