如何在 vim 中使用 pylint
我不想安装另一个插件,比如 pylint.vim,
今天,我决定使用 vim 编辑 python,而不是 pydev,这是一个 eclipse 插件。但我遇到了问题。
我已将其添加到我的 vimrc 中,
autocmd BufWritePost *.py !pylint <afile>
但 pylint 在输出中不包含文件名
************* Module mymodule
E: 22: invalid syntax
shell return 2
,因此它无法跳转到第 22 行,因此我使用 sed 更改
autocmd BufWritePost *.py !pylint <afile> | sed 's/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2: \1: /g'
它返回的输出:
mymodule.py:22: E: : invalid syntax
但没有 shell return 2 by vim。所以它仍然无法跳到那条线。 vim 认为编译成功
========================= 新注释 =========== 在 Vim 的 `autocmd` 命令中调用函数
我想也许我应该 使用以下配置,vim 返回:
autocmd FileType python let &makeprg='pylint <afile> | sed s/^\(\w*\):\s*\([0-9]\+\)/<afile>:\2: \1: /g'
autocmd BufWritePost *.py make
使用 make 命令并设置 makeprg,所以我在保存时
************* 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为什么在 Linux 上可以正常工作的 sed 如此复杂?请尝试以下操作:
why so complicated with sed which just works properly on Linux? Try the following:
pylint.vim
已旧,请使用 syntastic 代替:https://github.com/scrooloose/综合
pylint.vim
is old, use syntastic instead:https://github.com/scrooloose/syntastic
最后我自己解决了。我想和大家分享一下。 vimrc 中的 2 行。
at last I resolve it myself. I'd like share with you guys. 2 lines in vimrc.
我建议使用 ALE(异步 Lint 引擎)https://github.com/w0rp/ale
它支持一系列 python linter 和格式化程序,包括 pylint。 ALE 的一大优点是它支持许多其他语言。
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.
现在,vim 提供了 pylint 的编译器文件。
这意味着,如果您启用了文件类型检测(
文件类型插件缩进
),则无需外部插件即可使用此功能。:set makeprg?
应该显示 pylint 是发出:make
时将被调用的内容。如果没有,您需要使用:compiler 将其设置为当前编译器! pylint
。现在,要使其正常工作,您需要传递一些参数,以便 pylint 知道要检查什么,最值得注意的是您想要检查什么,即要链接的文件名或目录。因此,要检查当前缓冲区,请运行
:make %
。要检查当前目录,请运行:make 。
同样的机制可以扩展到使用 flake8,或者任何类型文件的任何 linter。请参阅
:h:编译器
。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
.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
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
您可能想尝试运行 epylint 而不仅仅是 pylint。
epylint(随 pylint 一起提供)是在 emacs(与 Flymake 一起)中使用的。它有一些更改,特别是在路径处理方面,请参阅 pylint/epylint.py 开头的文档字符串以获取更多信息。它也可能对你在 vim 中有所帮助。
旁注:我不是自己编程的 vim 用户,但 pylint.vim 似乎仍然是一个不错的选择。但我不会质疑你的先决条件。
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.