2

Using dired in emacs, i would to open (ie; a .png) any file with a list of viewers (selectable by typing) as 'open-with' way...

How can i do that?

Thank you,

Steve,

Steve
  • 21
  • 2
  • I use a custom setup: Enter key on a file type not normally edited (e.g., a *.pdf) (or when I select multiple similar type files), I get a list of choices -- e.g., `[a]dobe | [s]kim | [p]review | [o]mnipage`. If I want something special instead, rather than using the enter key, I hit a user-defined keyboard shortcut, and the dired buffer opens the applications folder -- in `recursive-edit` -- then when I hit the enter key on the desired application, it will open the file(s) previously selected -- a `start-process . . .` is activated with the selected application and the file(s) open(s). – lawlist Apr 12 '14 at 15:51
  • Here is a link to a Github example described above: https://github.com/lawlist/dired-read-file-name/blob/master/dired-read-file-name.el – lawlist Apr 12 '14 at 16:01

3 Answers3

1

You should use & to run the command in async: ! will freeze Emacs while the command is running.

Customize dired-guess-shell-alist-user as a guess list for common extensions:

(setq dired-guess-shell-alist-user
      '(("\\.pdf\\'" "evince" "okular")
        ("\\.eps\\'" "evince")
        ("\\.jpe?g\\'" "eog")
        ("\\.png\\'" "eog")
        ("\\.gif\\'" "eog")
        ("\\.xpm\\'" "eog")
        ("\\.csv\\'" "libreoffice")
        ("\\.tex\\'" "pdflatex" "latex")
        ("\\.\\(?:mp4\\|mkv\\|avi\\|flv\\|ogv\\)\\'" "vlc")
        ("\\.\\(?:mp3\\|flac\\)\\'" "rhythmbox")
        ("\\.html?\\'" "firefox")
        ("\\.cue?\\'" "audacious")))

The first item on the list will be the default choice, e.g. evince over okular. You can navigate to the other choices with M-n/M-p.

If you're on Linux, you can try the command that I'm using for this task:

(defvar dired-filelist-cmd
  '(("vlc" "-L")))

(defun dired-start-process (cmd &optional file-list)
  (interactive
   (let ((files (dired-get-marked-files t current-prefix-arg)))
     (list
      (dired-read-shell-command "& on %s: " current-prefix-arg files)
      files)))
  (apply
   #'start-process
   (list cmd nil shell-file-name shell-command-switch
         (format "nohup 1>/dev/null 2>/dev/null %s \"%s\""
                 (if (> (length file-list) 1)
                     (format "%s %s"
                             cmd
                             (cadr (assoc cmd dired-filelist-cmd)))
                   cmd)
                 (mapconcat #'expand-file-name file-list "\" \"")))))

It's better than dired-do-async-shell-command that's bound to &, because the opened files will persist even if you close Emacs that opened them. I tend to close Emacs more than usual because often I'm testing stuff and it's faster to restart than to reset to the default state.

abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • No, this will not work with a list of files that have different extensions: `...("\\.jpe?g\\'" "eog") ("\\.png\\'" "eog") ("\\.gif\\'" "eog") ("\\.xpm\\'" "eog")...`. See [Emacs bug #17251](http://debbugs.gnu.org/cgi/bugreport.cgi?bug=17251). You must combind those regexps and use a single entry for command "eog". – Drew Apr 12 '14 at 16:30
  • Thanks. I needed to load `dired-x` and `dired-aux` for this to work. – rvf0068 Apr 12 '14 at 20:30
0

If you can do without a list of viewers, you can hit ! while point is over a file in dired, and you can type the name of a command (with tab completion if your Emacs is new enough). dired will run that command, with the name of the file added at the end.

If the file name shouldn't be at the end of the command, add * wherever it should be, and dired will put it there instead.

For example, hitting ! over foo.png and typing just bar will run bar foo.png, while typing bar * --baz will run bar foo.png --baz.

legoscia
  • 39,593
  • 22
  • 116
  • 167
0

For most GNU/linux desktops you can use the mediator package which uses the Freedesktop mime-type specifications to automatically present a list of suitable programs for opening a file of some specific extension.

dalanicolai
  • 335
  • 2
  • 13