how to use pylint in vim

Viewed 34604

I do not want to install another plugin, like pylint.vim,

And today, I decide to use vim edit python instead of pydev which is a eclipse plugin. But I got issues.

I have add this in my vimrc

autocmd BufWritePost *.py !pylint <afile>

but pylint does not contains filename in output

************* Module mymodule
E: 22: invalid syntax

shell return 2

so it can not jump to the line 22 , so I use sed change the output

autocmd BufWritePost *.py !pylint <afile> | sed 's/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2: \1: /g'

it returns:

mymodule.py:22: E: : invalid syntax

but without shell return 2 by vim. so it still can't jump to that line. vim consider it as compile successfully

========================= new comment =========== Call a function in Vim’s `autocmd` command

I think maybe I should use make command and set makeprg, so I use below config

autocmd FileType python let &makeprg='pylint <afile> | sed s/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2: \1: /g'
autocmd BufWritePost *.py make

when I save, vim returns:

************* Module count
E:  3: invalid syntax
(1 of 2): ************* Module count
Error detected while processing BufWritePost Auto commands for "*.py":
E492: Not an editor command:  sed s/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2: 
\1: /g 
7 Answers

I would recommend to use A.L.E (Asynchronous Lint Engine) https://github.com/w0rp/ale

It supports a range of python linters and formatters including pylint. Great thing about A.L.E that it supports many other languages.

Nowadays vim ships a compiler file for pylint. This means that if you have enabled filetype detection (filetype plugin indent on), this is already available without external plugins.

:set makeprg? should show you that pylint is what will be called when issuing :make. If it doesn't, you need to set it as the current compiler with :compiler! pylint.

Now, to have this working, you need to pass some arguments so pylint knows what to lint, most notably what you want to lint, i.e. the filename or directory to link. So to lint the current buffer, run :make %. To lint the current directory, run :make .

That same mecanism could be extended to use flake8 for example, or any linter for any kind of file. See :h :compiler.

Related