Recently I am indulging in Emacs. I tried to use it as my LaTeX tool, so I installed pdf-tools. If I add


in my init file, it will take about 2 seconds to start Emacs. So I want to write a function such that "pdf-tools-install" will run after I open a pdf file. But "add-hook" only works when a major mode is recognized, and we can recognize pdf only after pdf-tools is installed. I think it may relate to regex, but I don't know much about it. Could you help me with this function? Thanks!

  • 2
    Try (add-hook 'doc-view-mode-hook #'pdf-tools-install)
    – politza
    Commented Jan 19, 2017 at 22:58
  • @politza Sadly it does not work, pdf file is still recognized as Fundamental mode. Commented Jan 20, 2017 at 5:02
  • 2
    I suggest either install pdf tools in an idle timer, or autoload it and use an eval-after-load. Or both. (Sorry for not providing code I'm on a mobile device)
    – YoungFrog
    Commented Jan 20, 2017 at 6:42
  • 1
    When I hit C-c C-c in my documents it switches between pdfview and Docview so you could see if pdf-tools is initialize when the pdf is opened after applying politza answer.
    – Sparx
    Commented Jan 20, 2017 at 8:46
  • 1
    @Manan Mehta But it should be in doc-view-mode.
    – politza
    Commented Jan 20, 2017 at 21:28

1 Answer 1


Got it to work! Thanks guys!

I added

(add-to-list 'auto-mode-alist '("\\.pdf\\'" . doc-view-mode))


(add-hook 'doc-view-mode-hook #'pdf-tools-install)

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.