Guessing File Applications in Dired

One of the really great tools in Emacs is the Dired/Dired+ package. Recently, Mickey wrote a nice introduction to using Dired to run shell commands on files; I commented on that here. The other day, by happenstance, I found myself needing to do just that. I wanted to display a series of PDF files from a directory to collect some information. Since I was in Emacs already to collect the data, I used Dired to list the directory and then used 【!】 to invoke the PDF viewer.

Emacs, of course, is pretty smart about things like that and suggested that I use xpdf. That would be precisely the right answer if I was working on one of my Linux machines but this was OS X and I really needed to invoke the open command. So I had to delete the suggestion and type in open for each file. Not really much trouble but later I thought that there was undoubtedly a way to control the suggestions.

There is, of course. After grubbing around in dired.el for a bit, I found the answer. Dired uses an alist to match the default applications to file extensions but it has another—empty by default—alist, dired-guess-shell-alist-user, that it will prepend to the default list if it’s present.

So I added

(setq dired-guess-shell-alist-user      ;quess shell command by file ext
      '(("\\.pdf\\'" "open")
        ("\\.ps\\'" "open")))

to my OS X specific configuration and now I get the proper suggestion when I run a shell command on PDF or PS files. How can you not love Emacs?

This entry was posted in General and tagged . Bookmark the permalink.