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
Create an autocmd
Run code when matching files are written.
OpenIn sheetluasame section
Create an augroup
Group related autocmds together.
OpenIn sheetluasame section
Enable line numbers
Turn on absolute and relative line numbers.
OpenIn sheetlua2 tag match
Map a save command
Bind a normal-mode key to save the file.
OpenIn sheetlua2 tag match
Configure tabs and indentation
Set width and spacing rules.
OpenIn sheetlua2 tag match
Map Telescope file search
Bind a key to file-finding with a Lua callback.
OpenIn sheetlua2 tag match