James Cherti: Configuring Emacs Eglot to auto format code on save

Wait 5 sec.

Using Eglot for code formatting reduces the number of dependencies in your Emacs configuration. Instead of installing and configuring third-party Emacs package, Eglot handles formatting directly through the active LSP server, which ensures the formatting logic matches the active language server's configuration and that the editor and the server are synchronized. While you can add eglot-format-buffer directly to the global before-save-hook, doing so will format every file you save, which can cause unexpected behavior in buffers that are not managed by Eglot. The correct method is to add the format function to a buffer-local hook, and only when Eglot is … Continue reading →