25

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 
Community
  • 1
  • 1
guilin 桂林
  • 17,050
  • 29
  • 92
  • 146

7 Answers7

30

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
jceb
  • 434
  • 3
  • 5
  • 1
    thanks. Just what I want. but, what's the mean of %:p. and the errorformat is a vim variable, how could it affect pylint? – guilin 桂林 Oct 02 '10 at 03:08
  • 1
    `%:p` means to use the full path of the file – idbrii Feb 23 '11 at 20:19
  • 6
    Great answer. But --output-format is now a deprecated option in pylint 1.0.0. Alternative is to use `set makeprg=pylint\ --reports=n\ --msg-template=\"{path}:{line}:\ {msg_id}\ {symbol},\ {obj}\ {msg}\"\ %:p` – John Slade Feb 21 '14 at 17:17
  • Works like a charm! `$ pylint --version No config file found, using default configuration pylint 1.4.4, astroid 1.3.8, common 1.1.0 Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4]` – ATOzTOA Nov 17 '15 at 23:20
17

pylint.vim is old, use syntastic instead:

https://github.com/scrooloose/syntastic

Reactormonk
  • 21,472
  • 14
  • 74
  • 123
  • 8
    Something a little more substantial than just a bare link would get an upvote. – Daenyth Oct 01 '10 at 13:59
  • 14
    It protects against link rot. – Dennis Williamson Oct 01 '10 at 14:34
  • 1
    The above Vim plugin is not maintained. This fork aims to keep up to date with pylint. https://github.com/orenhe/pylint.vim – Viktiglemma Jun 15 '12 at 09:22
  • Thanks, [syntastic has many checkers](https://github.com/vim-syntastic/syntastic/blob/master/doc/syntastic-checkers.txt): `Bandit`, `flake8`, `Frosted`, `mypy`, `Prospector`, `py3kwarn`, `pycodestyle`, `pydocstyle`, `Pyflakes`, `Pylama`, `Pylint`, `python`, which one is the default? – Paul Rougieux May 10 '20 at 13:05
4

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 
guilin 桂林
  • 17,050
  • 29
  • 92
  • 146
3

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.

Dmitry Shvetsov
  • 651
  • 10
  • 19
3

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.

liberforce
  • 11,189
  • 37
  • 48
0

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

khushbu
  • 158
  • 11
-1

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.

gurney alex
  • 13,247
  • 4
  • 43
  • 57