less than 1 minute read

Air is an R formatter and language server written in Rust.

It is very fast and opiniated. It integrates with VSCode and Positron, RStudio (and soon with Zed).

Maybe there is a better way of integrating it with Emacs and ESS but for the time being, I wrote this short snippet that uses its command line interface to reformat the current buffer on save:

;; use Air to format the content of the file
(defun run-air-on-r-save ()
  "Run Air after saving .R files and refresh buffer."
  (when (and (stringp buffer-file-name)
             (string-match "\\.R$" buffer-file-name))
    (let ((current-buffer (current-buffer)))
      (shell-command (concat "air format " buffer-file-name))
      ;; Refresh buffer from disk
      (with-current-buffer current-buffer
        (revert-buffer nil t t)))))

(add-hook 'after-save-hook 'run-air-on-r-save)

From my limited testing, it works well enough for now.

Tags: ,

Categories:

Updated:

Comments