Skip to content

Toggle LSP for current buffer

adrian5 edited this page Sep 21, 2023 · 7 revisions

To start an LSP client for the current buffer — or stop an active one — you can use a function like the following:

local toggle_lsp_client = function()
  local buf = vim.api.nvim_get_current_buf()
  local clients = vim.lsp.get_active_clients({ bufnr = buf })
  if not vim.tbl_isempty(clients) then
    vim.cmd("LspStop")
  else
    vim.cmd("LspStart")
  end
end

This could be mapped to a keybinding for convenience.

Clone this wiki locally