Auto-revert PDFs in Emacs

January 2024

When I'm looking at pdf files in Emacs, I use =doc-view mode=.

And usually I'm looking at pdf files because I'm writing a screenplay in Fountain mode. So I'm making changes to the source file a lot, and the pdf is constantly changing.

But the pdf in Emacs isn't changing!

That is, Emacs has read the file into memory, and displays that memory as a pdf, no matter what has happened to the underlying file. When I change the screenplay, Emacs still shows the pdf as it was when I opened it.

But we can change that.

(add-hook 'doc-view-mode-hook
          (lambda () (when (and buffer-file-name
                                (string-suffix-p ".pdf"
                                                 buffer-file-name))
                       (auto-revert-mode nil))))
  

Here, we use the hook for doc-view-mode, so that whenever we turn on doc-view mode for a pdf, we enable auto-revert-mode. This mode makes Emacs update the displayed pdf whenever the file changes.

If you want to hear when I publish more, sign up for my mailing list!