Gustavo Alip
Gustavo Alip

Reputation: 411

How can I open files externally in Emacs dired mode?

I want to open a pdf with evince instead of DocView mode. Is there a possibility to open a file with a specific command like 'evince'?

Upvotes: 41

Views: 20550

Answers (8)

Jay Rajput
Jay Rajput

Reputation: 1888

Another windows OS solution using explorer.exe to open single/multiple files. To open multiple files, mark the file using m in dired and then hit o to open multiple files. To just open a single file under point hit o. I have tested it in Emacs 28.2 GUI running natively on windows (no WSL/Cygwin/Linux-on-Windows).

  (use-package dired
      :bind (:map dired-mode-map
          ("o" . jr/dired-open))
      :config
      (defun jr/dired-open ()
      (interactive)
      (if-let ((marks (dired-get-marked-files)))
        (dolist (file marks)
          (shell-command (format "explorer.exe %s" (file-name-nondirectory file))))
      (user-error "No marked files; aborting"))))

Upvotes: 1

Ramses Aldama
Ramses Aldama

Reputation: 335

(defun dired-open()
  (interactive)
  (setq file (dired-get-file-for-visit))
  (setq ext (file-name-extension file))
  (cond ((string= ext "pdf")
         ;; shell-quote-argument escapes white spaces on the file name
         (async-shell-command (concat "zathura " (shell-quote-argument file))))
        ((string= ext "epub")
         (async-shell-command (concat "zathura " (shell-quote-argument file))))
        ((string= ext "rar")
         (async-shell-command (concat "file-roller " (shell-quote-argument file))))
        ((string= ext "zip")
         (async-shell-command (concat "file-roller " (shell-quote-argument file))))
        (t (dired-find-file))))

Upvotes: 0

Shenghai.Geng
Shenghai.Geng

Reputation: 91

In Windows, I offen use ! and command "explorer" to open PDF/Word/Excel...

Upvotes: 3

Victor Deryagin
Victor Deryagin

Reputation: 12215

There is more then one way to do that. I suggest OpenWith library. Setup for your case may look like that:

(add-to-list 'load-path "/path/to/downloaded/openwith.el")
(require 'openwith)
(setq openwith-associations '(("\\.pdf\\'" "evince" (file))))
(openwith-mode t)

It sets file handler that will work from both dired and find-file.

Upvotes: 21

Iceland_jack
Iceland_jack

Reputation: 7014

Note that you can keep the process alive after exiting Emacs by using nohup [Wikipedia], so put the point on a single file in dired:

C-u ! nohup evince ? &

which creates a Persistent Processes [EmacsWiki].

Upvotes: 6

tototoshi
tototoshi

Reputation: 1156

Try this.

(defun dired-open-file ()
  "In dired, open the file named on this line."
  (interactive)
  (let* ((file (dired-get-filename nil t)))
    (message "Opening %s..." file)
    (call-process "gnome-open" nil 0 nil file)
    (message "Opening %s done" file)))

Upvotes: 11

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72745

You can use ! to open the file and then specify a command.

Upvotes: 8

Rafe Kettler
Rafe Kettler

Reputation: 76945

Yes. Use ! while in dired to run a shell command on a file.

In the case of evince, it's smarter to use &, though, which will run the command asynchronously, so emacs will still be usable while you have the PDF open.

Upvotes: 40

Related Questions