7

When I use ! on a *.tar.bz2 file in Dired, I'm presented with the following prompt:

! on WANem_3.0_Beta2.tar.bz2: {3 guesses} [bunzip2 -c * | tar xvf -] -!-

How can I add similar functionality for other file types?

7

TL;DR:

This functionality comes from dired-x, not Dired. Use (require 'dired-x) in your init file and then customize dired-guess-shell-alist-user.


We can see though where Dired plugs into dired-x:

(defun dired-read-shell-command (prompt arg files)
  "Read a dired shell command.
PROMPT should be a format string with one \"%s\" format sequence,
which is replaced by the value returned by `dired-mark-prompt',
with ARG and FILES as its arguments.  FILES should be a list of
file names.  The result is used as the prompt.

This normally reads using `read-shell-command', but if the
`dired-x' package is loaded, use `dired-guess-shell-command' to
offer a smarter default choice of shell command."
  (minibuffer-with-setup-hook
      (lambda ()
    (set (make-local-variable 'minibuffer-default-add-function)
         'minibuffer-default-add-dired-shell-commands))
    (setq prompt (format prompt (dired-mark-prompt arg files)))
    (if (functionp 'dired-guess-shell-command) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<
    (dired-mark-pop-up nil 'shell files
               'dired-guess-shell-command prompt files)
      (dired-mark-pop-up nil 'shell files
             'read-shell-command prompt nil nil))))

After reading the dired-x manual (info "dired-x"), you learn of the variable dired-guess-shell-alist-user, which you can customize.

Here is an example of changing dired-guess-shell-alist-user

(setq dired-guess-shell-alist-user
      '(("\\.e?ps$" "gv" "xloadimage" "lpr")
        ("\\.chm$" "xchm")
        ("\\.rar$" "unrar x")
        ("\\.e?ps\\.g?z$" "gunzip -qc * | gv -")
        ("\\.pdf$" "zathura")
        ("\\.flv$" "mplayer")
        ("\\.mov$" "mplayer")
        ("\\.3gp$" "mplayer")
        ("\\.png$" "feh")
        ("\\.jpg$" "feh")
        ("\\.JPG$" "feh")
        ("\\.avi$" "mplayer")))
  • 2
    In sum: (require 'dired-x) and customize dired-guess-shell-alist-user. ;-) – Drew Nov 18 '14 at 16:39
  • @Drew I'd feel bad about posting such a short answer though! XD I'll add your TL;DR :) – Sean Allred Nov 18 '14 at 18:25
  • I didn't at all mean that comment as a substitute for your more complete answer. – Drew Nov 18 '14 at 20:29
  • @Drew :) But you make a good point -- it wasn't straightforward to get 'the answer' from my answer. I need some practice my writing; that's one of the reasons I'm here :) – Sean Allred Nov 18 '14 at 20:37
  • 1
    Good. Wanting to communicate better shows how much you are trying to help people. – Drew Nov 18 '14 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.