Format Files on Save in Neovim Using the Built-in LSP Client
Configure Neovim's built-in LSP client to format buffers automatically before saving, using vim.lsp.buf.format inside a buffer-local BufWritePre autocommand.
19 Nov 2026, 00:23 UTC

Problem: Manual Formatting Breaks Flow and Consistency
Developers often run a formatter by hand before committing, or rely on editor plugins that format on every keystroke. Both approaches are error-prone: a forgotten save leaves inconsistent style in the diff, and aggressive formatting can jump the cursor while you are still typing. Neovim includes a built-in LSP client, so you can trigger formatting through the language server itself at a predictable moment: just before the buffer is written to disk.
Takeaway: Run vim.lsp.buf.format on BufWritePre
The useful takeaway is to attach a buffer-local autocommand to LspAttach. When a language server attaches and advertises formatting support, register a BufWritePre callback that calls vim.lsp.buf.format synchronously. This keeps formatting scoped to buffers with an active LSP client and avoids loading a separate formatter plugin.
Prerequisites
- Neovim 0.10 or newer. The snippet uses
client.supports_methodandvim.lsp.get_client_by_id, which are stable in this release. Verify withnvim --versionin a terminal. - A language server installed for the target language, such as
lua-language-server,pyright, orclangd, available on yourPATH. - Write access to your user configuration directory, typically
~/.config/nvim/init.lua. No root or sudo permissions are required.
Procedure: Add a Format-on-Save Autocommand
- Open
~/.config/nvim/init.luawith your normal user permissions. - Create an autocommand group so the configuration can be reloaded without duplicating handlers.
- Register an
LspAttachcallback. Inside it, confirm the client supportstextDocument/formatting. - Create a buffer-local
BufWritePreautocommand that callsvim.lsp.buf.formatwithasync = false, so formatting completes before the file is written. - Save
init.luaand restart Neovim, or source the file with:luafile %.
local group = vim.api.nvim_create_augroup("LspFormatOnSave", { clear = true })
vim.api.nvim_create_autocmd("LspAttach", {
group = group,
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if not client then
return
end
if not client.supports_method("textDocument/formatting", { bufnr = args.buf }) then
return
end
vim.api.nvim_create_autocmd("BufWritePre", {
group = group,
buffer = args.buf,
callback = function()
vim.lsp.buf.format({
bufnr = args.buf,
async = false,
})
end,
})
end,
})
LspAttach is Neovim's built-in event that fires when a language server client connects to a buffer. bufnr is the buffer number. Setting async = false blocks the UI briefly while the server formats, which is intentional for a pre-save hook because the write should not proceed over unformatted text.
Verification and Diagnostic Checks
- Start Neovim as your normal user:
nvim example.lua. - Confirm the language server attached by running
:lua print(vim.inspect(vim.lsp.get_clients()))in Normal mode. The output should list a client for the file's language. - Change indentation or spacing in the buffer, then run
:w. The buffer should be reformatted before the file is written to disk. - Run
:checkhealthand review the LSP section for client or log errors.
Limitations
- This only works for languages with an LSP server that advertises
textDocument/formatting. Buffers without an attached client are not formatted. - Synchronous formatting can delay saving large files. If the delay is noticeable, switch
async = falsetoasync = trueand accept that formatting may complete after the write. - If multiple attached servers support formatting for the same filetype, Neovim picks one automatically. Competing formatters can produce conflicting results. Use the
filteroption invim.lsp.buf.formatto select a specific client by name or ID. - This does not replace project-wide formatters such as Prettier unless your LSP server wraps them.
Recovery and Rollback
Because this change writes to init.lua, roll back by deleting or commenting out the LspFormatOnSave block, saving the file, and restarting Neovim. To test without restarting, clear the autocommand group, reload your configuration, then manually run :lua vim.lsp.buf.format() on demand.