Skip to content

Install linters for Python code in vim with ALE

Louis Maddox edited this page Jul 30, 2022 · 29 revisions

Motivation

I had previously tried to use linters in vim without success, before finding "ALE" a tool with a "fixer" feature which lints on save, much like Go does (for imports, similar to Python's isort).

Requirements

ALE requires vim 8+ (mine is 8.1 at time of writing, in 2022)

To install plugins for Vim I use Pathogen (see the README for other plugin managers). The requirement is one line in .vimrc, which should already be there if you've used it before.

"ensure you have the line execute pathogen#infect() in your ~/.vimrc file"

Installing ALE with Pathogen

cd ~/.vim/bundle
git clone https://github.com/dense-analysis/ale.git

Installing linters with ALE

The recommended way to configure fixers is to define a List in an ftplugin file.

" In ~/.vim/ftplugin/javascript.vim, or somewhere similar.

" Fix files with prettier, and then ESLint.
let b:ale_fixers = ['prettier', 'eslint']
" Equivalent to the above.
let b:ale_fixers = {'javascript': ['prettier', 'eslint']}
  • grep.app has some examples for other languages
  1. Put this in .vimrc (to automatically fix files when you save them):
" Set this variable to 1 to fix files when you save them.
let g:ale_fix_on_save = 1
  1. Put this in ~/.vim/ftplugin/python.vim (as recommended on StackOverflow):
" Fix files with isort, and then black.
let b:ale_fixers = {'python': ['isort', 'black']}
let g:ale_python_isort_options = '--profile black -l 100'

Using linters with ALE

You 'fix' files with the linters installed in this way by calling ALEFix

About ALE

To quote from the README:

ALE offers support for fixing code with command line tools in a non-blocking manner with the :ALEFix feature, supporting tools in many languages, like prettier, eslint, autopep8, and more.

  • There are a ton of these: recently I've used flake8-bugbear and pandas-vet.
  • My standard set of linters that themselves modify code is: isort (reorder imports), black (enforce standard code style), and autoflake8 (remove unused imports)

It also claims to be efficient

One of ALE's general missions is that you won't pay for the features that you don't use.

The work is done in the background upon opening new buffers or editing files.

Options are documented in the vim help file

  • :help ale-options for global options
  • :help ale-integration-options for specific linters
Clone this wiki locally