I have configured lsp-mode as follows:

(use-package lsp-mode
  :straight t
  :init (setq lsp-keymap-prefix "C-c l")
  :hook ((python-mode . lsp))
  :commands lsp)

I am also using desktop-save-mode to restore my editing state upon restart. When I have several projects open and quit Emacs, the restart takes a long time as it starts each lsp server. Is there a way to start each server only when I run an lsp-related command in the buffer?

2 Answers 2


How about:

(use-package lsp-mode
  :straight t
  :init (setq lsp-keymap-prefix "C-c l")
  :hook (python-mode . lsp-deferred))

The doc for lsp-deferred:


Entry point that defers server startup until buffer is visible.

lsp-deferred will wait until the buffer is visible before invoking lsp.
This avoids overloading the server with many files when starting Emacs.

Edit: I've encountered some issues with this as described by scry, if the above doesn't work, try his workaround (I've opened a PR here: https://github.com/emacs-lsp/lsp-mode/pull/3975).


The accepted answer didn't work for me, since when lsp-mode is already listed as a minor mode in the saved buffers, desktop-read will activate it before lsp-deferred runs.

Instead, I'm using this:

(defun my-lsp-deferred-on (_)
(setq desktop-minor-mode-table
   '((lsp-mode my-lsp-deferred) 

desktop-minor-mode-table tells Desktop how to restore the minor mode:

Table mapping minor mode variables to minor mode functions.
Each entry has the form (NAME RESTORE-FUNCTION).
NAME is the name of the buffer-local variable indicating that the minor
mode is active.  RESTORE-FUNCTION is the function to activate the minor mode.

Note that the function is called with one argument like a minor-mode function, but lsp-deferred takes none, hence the placeholder; also, it has to be mapped by its name, so we can't just use a lambda.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.