Jake Trent

Prettier on Spacemacs

These days, getting prettier into spacemacs is easy.

I’ve tried a few different methods, and recently I tried again and found one that worked well out of the box. There is a repo for spacemacs-prettier at praveenperera/spacemacs-prettier that is the best I’ve found so far.

Install

To install, clone the repo into ~/.emacs.d/private/:

cd ~/.emacs.d/private/
git clone [email protected]:praveenperera/spacemacs-prettier.git

Next, use SPC f e d to open your .spacemacs file. Add spacemacs-pretter to your config there:

(defun dotspacemacs/layers ()
    (dotspacemacs-configuration-layers '(spacemacs-prettier))
)

Format on Save

There are some great setups to format on commit. If you want to format with prettier every time you save within Spacemacs, you can also add some config for that.

Open your .spacemacs file again, and a hook for every mode you care to format on save in:

(defun dotspacemacs/user-config ()
    (add-hook 'js2-mode-hook 'prettier-js-mode)
    (add-hook 'web-mode-hook 'prettier-js-mode)
)

Adjust Prettier Config

And of course, you want to add the one true prettier options to your formatting:


(defun dotspacemacs/user-config ()
    (setq prettier-js-args '(
                             "--no-semi"
                             "--single-quote"
                             ))
)

:)

Now you should be set. Slap some nasty js in the buffer, SPC f s, and it’s pretty!

Do you have any other interesting prettier configurations within Spacemacs?