I am trying to open files in Emacs using dired. But whenever I open pdf files, the doc viewer in Emacs keeps loading the document. Is there any way to stop the pdf files being opened in Emacs buffer?
The major mode is selected automatically according to a bunch of rules, including looking at variable
auto-mode-alist. Its initial value includes:
("\\.\\(?:PDF\\|DVI\\|OD[FGPST]\\|DOCX?\\|XLSX?\\|PPTX?\\|pdf\\|djvu\\|dvi\\|od[fgpst]\\|docx?\\|xlsx?\\|pptx?\\)\\'" . doc-view-mode-maybe)
doc-view-mode-maybe for (among others) files whose name is ending in
(setq auto-mode-alist (cons '("\\.pdf" . fundamental-mode) auto-mode-alist))
Also note that you can simply use
C-c C-c to toggle between
doc-view-mode and editing the document as text (as mentionned in
I'm sure there's a proper answer to the question coming but in the meantime...
I'm assuming your question relates to wanting to open some external PDF viewer within a desktop environment, rather than trying to edit the raw text of a PDF file within Emacs.
Not quite as easy as pressing
ENTER, but pressing
! within dired prompts to run a shell command on the current file. You can then specify your PDF viewer (such as "evince") and that will be executed on the PDF file.
I had exactly the same problem for dvi, ps files, as well as pdf. The problem for me was in mailcap.el. I commented out the following 9 lines from line 165:
;; ("pdf" ;; (viewer . pdf-view-mode) ;; (type . "application/pdf") ;; (test . (and (fboundp 'pdf-view-mode) ;; (eq window-system 'x)))) ;; ("pdf" ;; (viewer . doc-view-mode) ;; (type . "application/pdf") ;; (test . (eq window-system 'x)))
I actually kept the original mailcap.el and copied it with my modifications to my-mailcap.el. Then I put the following in my ~/.emacs
(require 'my-mailcap) (autoload 'mailcap-parse-mailcaps "mailcap" nil t) (autoload 'mm-mailcap-command "mm-decode") (defadvice dired-view-file (around use-mailcap-mime-data activate) "Use an external viewer to view a file according to `mailcap-mime-data'." (unless (let ((file (dired-get-file-for-visit)) extension type command) (and (not (file-directory-p file)) (progn (mailcap-parse-mailcaps) (setq extension (file-name-extension file))) (setq type (mailcap-extension-to-mime extension)) (setq command (mailcap-mime-info type)) (setq command (mm-mailcap-command command file nil)) (prog1 t (shell-command command)))) ad-do-it))
Next I changed ~/.mailcap to bring up the application of your choice. For me, I used
application/pdf; xpdf %s application/postscript; gv %s application/x-dvi; xdvi %s
In dired, I now hit 'v' when the point is on the file I want and the appropriate application (xpdf, gv, xdvi) launches. If you still want docview then just hit return.
I found the above works with emacs-25 and emacs-26. However, do not use it for emacs-24; its mailcap.el does not need any modification.
You can find solutions to this in the elpa/melpa repositories. There are packages like
dired-open which let you customize which action to take on a filetype. You can even use alternate actions, like
enter – opening it in an external program,
f – edit the file in Emacs,
v – view the file in Emacs' doc-view mode.