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