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).

About ALE

From its 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

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 if you set the ale_fix_on_save option to 0 (off).

Troubleshooting

Identifying installed/enable/active linters

To see the current config when editing a file activated for ALE (i.e. in a language you installed it for), run :ALEInfo. For me this gives:

 Current Filetype: python
Available Linters: ['bandit', 'cspell', 'flake8', 'flakehell', 'jedils', 'mypy', 'prospector',
'pycodestyle', 'pydocstyle', 'pyflakes', 'pylama', 'pylint', 'pylsp', 'pyre', 'pyright', 'unimport',
'vulture']
  Enabled Linters: ['flake8', 'mypy', 'pylint', 'pyright']
  Ignored Linters: []
 Suggested Fixers:
  'add_blank_lines_for_python_control_statements' - Add blank lines before control statements.
  'autoflake' - Fix flake issues with autoflake.
  'autoimport' - Fix import issues with autoimport.
  'autopep8' - Fix PEP8 issues with autopep8.
  'black' - Fix PEP8 issues with black.
  'isort' - Sort Python imports with isort.
  'pyflyby' - Tidy Python imports with pyflyby.
  'remove_trailing_lines' - Remove all blank lines at the end of a file.
  'reorder-python-imports' - Sort Python imports with reorder-python-imports.
  'trim_whitespace' - Remove all trailing whitespace characters at the end of every line.
  'yapf' - Fix Python files with yapf.

Quietening ALE

Freezing the sign gutter

The default behaviour for ALE is to introduce a "sign gutter" along the left hand side of your code, that shifts the entire code file along -- quite visually disruptive.

The relevant part of the docs here show these are the 'sign gutter' containing ale_sign_error (default: '>>') and ale_sign_warning (default: '--'). These cannot be set to the empty string.

You can either keep the gutter open at all times with

let g:ale_sign_column_always = 1

Hiding the gutter

Another option would be to hide the sign gutter entirely

  • TODO: how to turn off the sign gutter?
Clone this wiki locally