3.3 Eglot Commands

This section provides a reference for the most commonly used Eglot commands:

M-x eglot

This command adds the current buffer and the file it visits to the group of buffers and files managed by Eglot on behalf of a suitable language server. If a language server for the buffer’s major-mode (see Major Modes in GNU Emacs Manual) is not yet running, it will be started; otherwise the buffer and its file will be added to those managed by an existing server session.

The command attempts to figure out the buffer’s major mode and the suitable language server; in case it fails, it might prompt for the major mode to use and for the server program to start. If invoked with C-u, it always prompts for the server program, and if invoked with C-u C-u, it also prompts for the major mode.

If the language server is successfully started and contacted, this command arranges for any other buffers belonging to the same project and using the same major mode to use the same language-server session. That includes any buffers created by visiting files after this command succeeds to connect to a language server.

All the Emacs features that are capable of using Eglot services (see Eglot Features) are automatically configured by this command to start using the language server via Eglot. To customize which Emacs features will be configured to use Eglot, use the eglot-stay-out-of option (see Customizing Eglot).

M-x eglot-reconnect

This command shuts down the current connection to the language server and immediately restarts it using the same options used originally. This can sometimes be useful to unclog a partially malfunctioning server connection.

M-x eglot-shutdown

This command shuts down a language server. It prompts for a language server to shut down (unless there’s only one server session, and it manages the current buffer). Then the command shuts down the server and stops managing the buffers the server was used for. Emacs features (see Eglot Features) that Eglot configured to work with the language server are restored back to their original configuration.

Normally, this command kills the buffers used for communicating with the language server, but if invoked with a prefix argument C-u, the command doesn’t kill those buffers, allowing them to be used for diagnostics and problem reporting (see Troubleshooting Eglot).

M-x eglot-shutdown-all

This command shuts down all the language servers active in the current Emacs session. As with eglot-shutdown, invoking this command with a prefix argument avoids killing the buffers used for communications with the language servers.

M-x eglot-rename

This command renames the program symbol (a.k.a. identifier) at point to another name. It prompts for the new name of the symbol, and then modifies all the files in the project which are managed by the language server of the current buffer to implement the renaming.

M-x eglot-format

This command reformats the active region according to the language-server rules. If no region is active, it reformats the entire current buffer.

M-x eglot-format-buffer

This command reformats the current buffer, in the same manner as eglot-format does.

M-x eglot-code-actions
M-x eglot-code-action-organize-imports
M-x eglot-code-action-quickfix
M-x eglot-code-action-extract
M-x eglot-code-action-inline
M-x eglot-code-action-rewrite

These commands allow you to invoke the so-called code actions: requests for the language server to provide editing commands for correcting, refactoring or beautifying your code. These commands may affect more than one visited file belonging to the project.

The command eglot-code-actions asks the server if there any code actions for any point in the buffer or contained in the active region. If there are, you have the choice to execute one of them via the minibuffer.

A common use of code actions is fixing the Flymake error diagnostics issued by Eglot (see GNU Flymake manual). Clicking on a diagnostic with mouse-2 invokes eglot-code-actions-at-mouse which pops up a menu of available code actions. The variable eglot-diagnostics-map can be used to control the mouse binding.

Other commands execute a specific code action. For example, eglot-code-action-organize-imports rearranges the program’s imports—declarations of modules whose capabilities the program uses.

M-x eglot-inlay-hints-mode

This command toggles LSP inlay hints on and off for the current buffer. Inlay hints are small text annotations to specific parts of the whole buffer, not unlike diagnostics, but designed to help readability instead of indicating problems. For example, a C++ language server can serve hints about positional parameter names in function calls and a variable’s automatically deduced type. Inlay hints help the user not have to remember these things by heart.

The following Eglot commands are used less commonly, mostly for diagnostic and troubleshooting purposes:

M-x eglot-events-buffer

This command pops up the events buffer used for communication with the language server of the current buffer.

M-x eglot-stderr-buffer

This command pops up the buffer with the debug info printed by the language server to its standard error stream.

M-x eglot-forget-pending-continuations

Forget pending requests for the server of the current buffer.

M-x eglot-signal-didChangeConfiguration

This command updates the language server configuration according to the current value of the variable eglot-workspace-configuration (see Customizing Eglot).

M-x eglot-clear-status

Clear the last JSONRPC error for the server of the current buffer. Eglot keeps track of erroneous situations encountered by the server in its mode-line indication so that the user may inspect the communication leading up to it (see Troubleshooting Eglot). If the situation is deemed uninteresting or temporary, this command can be used to “forget” the error. Note that the command M-x eglot-reconnect can sometimes be used to unclog a temporarily malfunctioning server.

As described in Eglot Features most features associated with Eglot are actually provided by other Emacs packages and features, and Eglot only enhances them by allowing them to use the information coming from the language servers. For completeness, here’s the list of commands of those other packages that are very commonly used in Eglot-managed buffers:

M-x eldoc

Ask the ElDoc system for help at point.

M-x flymake-show-buffer-diagnostics

Ask Flymake system to display diagnostics for the current buffer.

M-x flymake-show-project-diagnostics

Ask Flymake to list diagnostics for all the files in the current project.

M-x xref-find-definitions

Ask Xref to go the definition of the identifier at point.

M-x imenu

Let the user navigate the program source code using buffer index, categorizing program elements by syntactic class (class, method, variable, etc.) and offering completion.

M-x completion-at-point

Request completion of the symbol at point.