Trigger LSP formatting before write.
Section: Lua autocommands
Format on save with LSP
lua
lua
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.ts",
callback = function()
vim.lsp.buf.format()
end,
})Explanation
A common modern workflow for language-aware formatting.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Lua autocommands