vimtricks.wiki Concise Vim tricks, one at a time.

How do I use ALE to automatically lint and fix code in Vim?

Answer

:ALEFix

Explanation

ALE (Asynchronous Lint Engine) is a popular Vim plugin that runs linters and formatters asynchronously in the background, showing errors and warnings in the gutter and status line without blocking your editing. The :ALEFix command applies configured fixers (like prettier, black, eslint --fix) to the current file.

How it works

  • ALE automatically runs linters as you type or save
  • :ALEFix runs configured fixers to auto-format/fix the current buffer
  • Configure linters and fixers in your ~/.vimrc
let g:ale_linters = {
\   'python': ['flake8', 'mypy'],
\   'javascript': ['eslint'],
\}

let g:ale_fixers = {
\   'python': ['black', 'isort'],
\   'javascript': ['prettier'],
\   '*': ['remove_trailing_lines', 'trim_whitespace'],
\}

" Fix on save
let g:ale_fix_on_save = 1

Example

With black configured as a Python fixer, opening a messy Python file and running:

:ALEFix

Reformats the entire file according to black's style — removing extra spaces, fixing indentation, normalizing quotes.

Tips

  • :ALEInfo shows which linters and fixers are active for the current file
  • :ALEToggle disables/enables ALE for the current session
  • Navigate between errors: :ALENextWrap and :ALEPreviousWrap
  • Map :ALEFix for convenience:
    nnoremap <leader>f :ALEFix<CR>
    
  • ALE works alongside LSP (Language Server Protocol) setups — they complement each other

Next

How do I visually select a double-quoted string including the quotes themselves?