-
Notifications
You must be signed in to change notification settings - Fork 7
Install linters for Python code in vim with ALE
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).
- ALE requires vim 8+ (mine is 8.1 at time of writing, in 2022)
To install plugins for Vim I use Pathogen.
To install ALE with Pathogen run the following code
(and "ensure you have the line execute pathogen#infect() in your ~/.vimrc file"):
cd ~/.vim/bundle
git clone https://github.com/dense-analysis/ale.gitTo 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-bugbearandpandas-vet. - My standard set of linters that themselves modify code is:
isort(reorder imports),black(enforce standard code style), andautoflake8(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-optionsfor global options -
:help ale-integration-optionsfor specific linters
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
- 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- 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'You 'fix' files with the linters installed in this way by calling ALEFix