< Blog posts

Using @prettier/plugin-ruby in spacemacs

April 14, 2020

Currently, spacemacs' Ruby layer does not support choosing a formatter, like e.g. the TypeScript layer. I love being able to type code without thinking about formatting, hit :w, and watch my file beautify itself.

I've started using @prettier/plugin-ruby at work, and wanted the experience I get with my TypeScript code, with my Ruby code. To that end, I wrote my very first elisp!

  (defun ruby-prettier ()
    (interactive)
    (when (eq major-mode 'ruby-mode)
      (shell-command-to-string (format "yarn prettier --write %s" buffer-file-name))
      (revert-buffer :ignore-auto :noconfirm)))

  (add-hook 'after-save-hook 'ruby-prettier)

For the time being, I've dumped this function into dotspacemacs/user-config and it works! There is a noticeable half-second pause while this formats my code, something I haven't noticed when using prettier with TypeScript files. I don't know why.