Define Emacs Formatter

April 09, 2024

Steve Purcell has created a package reformatter, which can define commands to format the current Emacs buffer.

The reformatter-define macro expands to code which generates xxx-buffer and xxx-region interactive commands, and a local minor mode called xxx-on-save-mode. The :args and :program expressions will be evaluated at runtime, so they can refer to variables that may have a buffer-local value. A custom variable will be generated for the mode lighter, with the supplied value becoming the default.

For example, we can use shfmt to format shell scripts.

(with-eval-after-load 'sh-script
  (reformatter-define shfmt
    :program "shfmt"
    :args `("-i" ,(number-to-string sh-basic-offset))))

Top