Skip to main content

Automatically fix ESLint errors on save in Vim

  • setting up eslint lsp with autofixing

took me awhile!

This was the missing lspconfig setting for finding my eslintrc file when it wasnโ€™t in the project root:

servers = {
  eslint = {
    settings = {
      -- helpful when eslintrc is in a subfolder
      workingDirectory = { mode = โ€˜autoโ€™ },
    },
  },
}

And hereโ€™s the autocommand to enable auto-fix on save:

setup = {
  eslint = function()
    vim.cmd([[
      autocmd BufWritePre *.tsx,*.ts,*.jsx,*.js EslintFixAll
    ]])
  end,
},
  • See improvement to autocmd in my dotfiles, which I got from LazyVim ESLint โ€œextraโ€ (after I suggested a similar idea in a LazyVim Discussion)

Inbox