Neovim LSP Format Client Selection Across Multiple Attached Servers
0 reputation · 15 Nov 2026, 14:15 UTC
0 reputation · 15 Nov 2026, 14:15 UTC
Goal: ensure vim.lsp.buf.format consistently targets a single language server when two or more servers attach to the same buffer and both advertise documentFormattingProvider. The configuration must not depend on server registration order or implicit client iteration.
Constraint: Neovim has no built-in priority among eligible formatting clients, so an unfiltered call can produce different results across Neovim versions and setups. The filter function passed to vim.lsp.buf.format is the documented selection mechanism, but the exact client set and capability flags must be confirmed for each filetype.
Assumption: Neovim 0.5 through 0.11. The selected client’s stability after LSP restart or buffer reattach is unspecified.
documentFormattingProvider?37275 reputation · 15 Nov 2026, 15:23 UTC
Pass a filter to vim.lsp.buf.format that selects by client.name and re-checks documentFormattingProvider at call time. Do not use client.id or rely on the order Neovim returns attached clients.
Neovim gathers every attached client that advertises documentFormattingProvider and hands that list to your filter. Without a filter, the chosen client depends on internal iteration order, which can change across Neovim versions, server versions, and attach sequence.
nil.client.name stays the same after an LSP restart or buffer reattach; client.id does not.vim.lsp.buf.format is a silent no-op.documentRangeFormattingProvider, a separate flag.async = true was added later but does not change filter semantics.:LspInfo. Note the exact client name of the server you want to format.vim.lsp.buf.format directly:local function format_with_preferred(preferred_name)
vim.lsp.buf.format({
filter = function(client)
return client.name == preferred_name
and client.server_capabilities.documentFormattingProvider == true
end,
})
end
vim.keymap.set("n", "<leader>f", function()
format_with_preferred("eslint")
end, { buffer = true })
conform.nvim, formatter.nvim, or formatprg:local function format_or_fallback(preferred_name)
local clients = vim.lsp.get_active_clients({ bufnr = 0 })
local eligible = vim.tbl_filter(function(client)
return client.name == preferred_name
and client.server_capabilities.documentFormattingProvider == true
end, clients)
if #eligible > 0 then
vim.lsp.buf.format({
filter = function(client)
return client.name == preferred_name
end,
})
else
vim.cmd("silent! execute 'FormatWrite'")
end
end
A name-based filter remains stable after :LspRestart or buffer reattach because the client object is recreated but client.name is preserved. A filter built on client.id breaks after restart.
:echo v:version — confirm Neovim 0.5–0.11.:LspInfo — confirm the intended server appears and lists documentFormattingProvider.:lua print(vim.inspect(vim.lsp.get_active_clients({ bufnr = 0 }))) — inspect the exact client set and capability flags.:LspRestart, then run it again — the same client.name should be selected.Run :LspInfo in the affected buffer and share the exact client names. Name matching is case-sensitive and exact, so the filter string depends on the reported names.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.