5

I want to open several types of files in Dired by different external applications.
For example:

  • .doc files with LibreOffice
  • .pdf files with evince
  • .jpg files with pinta
  • etc...

How do i configure this?

Drew
  • 75,699
  • 9
  • 109
  • 225
nephewtom
  • 2,219
  • 17
  • 29

3 Answers3

6

Use Dired-x (standard library dired-x.el: put (require 'dired-x) in your init file).

Customize option dired-guess-shell-alist-user. C-h v tells you:

dired-guess-shell-alist-user is a variable defined in dired-x.el.

Its value is nil

Documentation:

User-defined alist of rules for suggested commands.

These rules take precedence over the predefined rules in the variable dired-guess-shell-alist-default (to which they are prepended).

Each element of this list looks like

(REGEXP COMMAND...)

where each COMMAND can either be a string or a Lisp expression that evaluates to a string.

If this expression needs to consult the name of the file for which the shell commands are being requested, it can access that file name as the variable file.

If several COMMANDs are given, the first one will be the default and the rest will be added temporarily to the history and can be retrieved with M-x previous-history-element (M-p) .

The variable dired-guess-shell-case-fold-search controls whether REGEXP is matched case-sensitively.

You can customize this variable.

Then just use ! or & on files in Dired. Use M-n to retrieve the default value of the program to run, which will come from your entries in the option value.

Drew
  • 75,699
  • 9
  • 109
  • 225
3

Using & from dired prompts to open each file with an external application.
Searching and trying, these elisp lines made it work:

  (setq dired-guess-shell-alist-user '(("\\.pdf\\'" "evince")
                                   ("\\.doc\\'" "libreoffice")
                                   ("\\.docx\\'" "libreoffice")
                                   ("\\.ppt\\'" "libreoffice")
                                   ("\\.pptx\\'" "libreoffice")
                                   ("\\.xls\\'" "libreoffice")
                                   ("\\.xlsx\\'" "libreoffice")
                                   ("\\.jpg\\'" "pinta")
                                   ("\\.png\\'" "pinta")
                                   ("\\.java\\'" "idea")))
nephewtom
  • 2,219
  • 17
  • 29
  • 1
    Each `setq` you perform this way changes the value of the variable, so I doubt this works. You can check the final value with `C-h v dired-guess-shell-alist-user`. – JeanPierre Aug 10 '18 at 19:15
  • @JeanPierre Yes, you are right. Newbie error. I just corrected it with some values I am currently using. Tested it using use-package. – nephewtom Sep 03 '18 at 11:32
3

In 2022, and emacs 28.1, all you have to do is

  • navigate to dired file in dired
  • press W

Which runs (browse-url-of-dired-file) and seems to open the file or directory in the default application.

leze
  • 93
  • 7