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:
1 2 3 4 5 6 7 8 9 10 11 (use-package format-all :preface (defun ian/format-code () "Auto-format whole buffer." (interactive) (if (derived-mode-p 'prolog-mode) (prolog-indent-buffer) (format-all-buffer))) :config (global-set-key (kbd "M-F") #'ian/format-code) (add-hook 'prog-mode-hook #'format-all-ensure-formatter))
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.