This is a short note on setting up Doom emacs for very fast typst previews.

typst-ts-mode provides typst-ts-watch-mode via C-c C-w, which runs typst watch in the background, as well as typst-ts-preview via C-c C-p which opens the current pdf file. Using zathura as pdf viewer provides live updates.

1
2
3
;; doom/packages.el
;; typst: https://codeberg.org/meow_king/typst-ts-mode/wiki/Installation.md
(package! typst-ts-mode :recipe (:host codeberg :repo "meow_king/typst-ts-mode"))

Since typst is so fast to compile, and near-live previews are nice, we’d like to save the file often. Since space f s is somewhat annoying to type all the time, we’ll add some auto-saving.

As suggested by jrmoulton in the comments, tinymist (gh) is an LSP that provides actual live-previews.

We save when emacs loses focus using

1
2
3
;; save on focus lost
;; https://stackoverflow.com/q/1230245
(add-hook 'focus-out-hook (lambda () (interactive) (save-some-buffers t)))

Even better, I have caps-lock mapped to escape, so I remapped escape in normal mode to save as well:

1
(define-key evil-normal-state-map (kbd "<escape>") (lambda () (interactive) (save-some-buffers t)))

It does not work super smoothly because it seems to inhibit some other things that run on escape, but it’s good enough for now1 and I use it a lot.

Another thing I tried before that is save on exiting insert mode, but this turned out not as useful as I thought: sometimes it’s annoying to auto-format-on-save the buffer while you’re making changes, and there are changes like deleting a line with dd that never enter insert mode, and so manual saving would still be necessary in that case anyway. But anyway, I had the following snippet for that:

1
2
3
;; save on exiting insert mode
;; https://www.reddit.com/r/emacs/comments/3s7d38
;(add-hook 'evil-insert-state-exit-hook (lambda () (interactive) (save-some-buffers t)))

  1. Note that I’m a complete elisp noob. Feel free to suggest improvements. ↩︎