7

Windows 10, Emacs 25.1, Dired+

In my folder "test" I have different file:

my_dired

So I want when press Enter on file 97_things_for_programmer.pdf to open this pdf file by my default application - Adobe Reader.

When press Enter on file my_photo.jpg to open this jpg file by my default application - XnView. And so on. But the text file to open by Emacs (as usual).

2
  • "How can I open files externally in Emacs dired mode?": stackoverflow.com/questions/6845005/… "Emacs: Open File in External App": ergoemacs.org [The link to Xah Lee's website is not working, not sure why ...]
    – lawlist
    Commented Nov 25, 2017 at 18:51
  • 1
    The simplest way I've found so far is to figure out what file explorer the system uses (in your case, I believe it's explorer), and just press ! on the file in question and then type explorer.
    – wvxvw
    Commented Nov 26, 2017 at 10:03

2 Answers 2

10

Press W in dired mode (Emacs 26.1, official MS Windows built), which is bound by default to browse-url-of-dired-file. The documentation string is restrictive by saying

In Dired, ask a WWW browser to display the file named on this line.

It works with other file types, too, e.g. .pdf, .docx, .xlsx, .bat, .exe. Tested on Windows 7 with GNU Emacs 26.1 (build 1, x86_64-w64-mingw32).

5
  1. You are apparently using MS Windows. If so, see section If You Use Emacs on MS Windows of the Dired+ doc.

    It explains how to use Windows file associations in Dired. In a nutshell, loading library w32-browser.el lets you use M-RET (command dired-w32-browser) to open a file or directory in a Dired listing using its Windows-associated application. You can also use mouse-3 and choose menu item Open Associated Windows App, or use the same menu item in menu-bar menu Single. (C-RET opens Windows Explorer for the file or dir.)

    If you want to use the Windows-associated app when you hit RET then just bind dired-w32-browser to RET. I use RET for the usual Emacs behavior and M-RET for the Windows app, but you can bind the commands to any keys you like (in dired-mode-map, of course).

  2. More generally (all platforms, including MS Windows), you can use option dired-guess-shell-alist-user to define default applications for acting on files in Dired using ! and similar commands. This is not special to Dired+. It is available by loading standard library dired-x.el (which is loaded automatically by Dired+).

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

    Its value is ()

    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. This expression can access the 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.

3
  • Small example for the sake of clarity: to have dired open pdf files with evince on !, I added this to my config: (add-to-list 'dired-guess-shell-alist-user '("\\.pdf$" "evince"))
    – kotchwane
    Commented May 23, 2020 at 8:56
  • Follow-up question : I like dired+'s behaviour of M-Enter opening directly the file with Adobe Reader on Windows. Is there a way to replicate this on Linux (with evince) ?
    – kotchwane
    Commented May 23, 2020 at 8:57
  • @kotchwane: Sorry; I don't understand. I think you could write similar code for the Linux case. I use only Windows these days (unfortunately), so you would need to do that yourself. (Possibly it could be included in Dired+.)
    – Drew
    Commented May 23, 2020 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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