2.2 Starting Eglot

The most common way to start Eglot is to simply visit a source file of a given language and use the command M-x eglot. This starts the language server suitable for the visited file’s major-mode, and attempts to connect to it. If the connection to the language server is successful, you will see the [eglot:project] indicator on the mode line which reflects the server that was started. If the server program couldn’t be started or connection to it failed, you will see an error message; in that case, try to troubleshoot the problem as described in Troubleshooting Eglot. Once a language server was successfully started and Eglot connected to it, you can immediately start using the Emacs features supported by Eglot, as described in Eglot Features.

A single Eglot session for a certain major-mode usually serves all the buffers under that mode which visit files from the same project, so you don’t need to invoke M-x eglot again when you visit another file from the same project which is edited using the same major-mode. This is because Eglot uses the Emacs project infrastructure, as described in Buffers, Projects, and Eglot, and this knows about files that belong to the same project. Thus, after starting an Eglot session for some buffer, that session is automatically reused when visiting files in the same project with the same major-mode.

Alternatively, you could configure Eglot to start automatically for one or more major-modes from the respective mode hooks. Here’s an example for a hypothetical foo-mode:

 (add-hook 'foo-mode-hook 'eglot-ensure)

The function eglot-ensure will start an Eglot session for each buffer in which foo-mode is turned on, if there isn’t already an Eglot session that handles the buffer. Note that this variant of starting an Eglot session is non-interactive, so it should be used only when you are confident that Eglot can be started reliably for any file which may be visited with the major-mode in question.

When Eglot connects to a language server for the first time in an Emacs session, it runs the hook eglot-connect-hook (see Eglot Variables).