how to use pylint in vim
Question
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
Solution
why so complicated with sed which just works properly on Linux? Try the following:
set makeprg=pylint\ --reports=n\ --output-format=parseable\ %:p
set errorformat=%f:%l:\ %m
OTHER TIPS
pylint.vim
is old, use syntastic instead:
at last I resolve it myself. I'd like share with you guys. 2 lines in vimrc.
autocmd FileType python let &makeprg='pylint %\|sed "s/^\(\w*\):\s*\([0-9]\+\)/%:\2:\ \1:\ /g"'
autocmd BufWritePost *.py make
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.
you may want to try running epylint instead of just pylint.
epylint (shipped with pylint) is the one that is used within emacs (with flymake). It has a few changes, especially regarding path handling, see the docstring at the start of pylint/epylint.py for more information. It may help you in vim too.
Sidenote: I'm not a vim user for programming myself, but pylint.vim still seems to be a decent option. But I won't question your prerequisites.
autocmd FileType python let &makeprg=‘/usr/local/bin/pylint %’
autocmd BufWritePost *.py make
autocmd FileType python let &makeprg=‘/usr/local/bin/pyflakes %’
autocmd BufWritePost *.py make