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

  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. Then define a List in an ftplugin file at ~/.vim/ftplugin/python.vim, (after mkdir -p ~/.vim/ftplugin in my case), similar to the suggestion here [but with the standard line length]:
" Fix files with isort, and then black.
let b:ale_fixers = {'python': ['isort', 'black']}
let g:ale_python_isort_options = '--profile black -l 88'

💡 Note: typically you'll find the defaults to use here in pyproject.toml (e.g.) or .pre-commit-config.yaml (e.g.)

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