Recently I added this autocmd to my Neovim configuration, to format Go language code when the buffer was saved.
acmd({ "BufWritePre" },
{ pattern = "*.go",
callback = function()
vim.lsp.buf.format()
end,
group = _go})
The vim.lsp.buf.format()
call executes the LSP provided formatter for buffer being written. By
having it tied to Go language (.go
) files I was missing out on formatting of other languages.
Today I moved that autocmd to the general section of my autocmd.lua
file, and set the pattern to
be *
, so that it would work for all languages.
acmd({ "BufWritePre" },
{ pattern = "*",
callback = function()
vim.lsp.buf.format()
end,
group = _general })
Now when I save a buffer, whether it is Go program, or Rust, or even the Lua code in my Neovim configuration, it gets formatted.