Posts Formatting Code in Emacs with Format-All

Formatting Code in Emacs with Format-All

Format-all is a simple yet awesome Emacs package that allows you to “format source code in different programming languages using the same command, instead of learning a different Emacs package and formatting command for each language” (Reference). The author chose great default formatters for each programming language, so the package is almost plug-and-play. For instance, format-all uses clang-format for C/C++, prettier for JavaScript/Typescript, and black or yapf for Python. You can find the list of supported languages here. On top of that, format-all provides an Emacs minor mode, format-all-mode, that enables format on save. Like most third-party Emacs packages, it’s recommended to install format-all from MELPA.

After installation, there’s little configuration to be done. You have to make sure that you have the external formatter programs (e.g. clang-format, prettier, etc.) installed and put on your PATH. I define a custom function to extend the format-all-buffer command to operate on Prolog buffers as well, since format-all currently does not support the Prolog language. With inspiration taken from Visual Studio Code, I bind Alt-Shift-f to invoke my custom function and auto-format the current buffer. Lastly, I make sure the default formatter is used whenever I enter a coding buffer of a recognized programming language.

Putting everything together, this is all the configuration I have for format-all:

(use-package format-all
  (defun ian/format-code ()
    "Auto-format whole buffer."
    (if (derived-mode-p 'prolog-mode)
  (global-set-key (kbd "M-F") #'ian/format-code)
  (add-hook 'prog-mode-hook #'format-all-ensure-formatter))

Here, M-F is synonymous with Alt-Shift-f because Emacs understands Alt as the Meta key. I didn’t enable “format-on-save” because I’m more used to manually invoking the formatting function.

If you’re interested in more customization options for format-all, such as setting specific formatters for a project (via .dir-locals.el), you can read this section of the README. If you are unfamiliar with use-package, you can read my quick-start guide here.

That’s all for today, have a good one.

This post is licensed under CC BY 4.0 by the author.