使用 Vim 编辑 python 时出错
当我在 Vim 中编辑 python 文件(使用 MacVim)并按 o
插入新行时,Vim 会抛出以下错误:
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E121: Undefined variable: dummy
Press ENTER or type command to continue
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E15: Invalid expression: line('.') < 7 ? dummy : synIDattr(synID(line('.'), col('.')
, 1), 'name') =~ '\(Comment\|String\)$'
如何解决此问题?
When I edit a python file in Vim (using MacVim), and I press o
to insert a new line, Vim throws the following errors:
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E121: Undefined variable: dummy
Press ENTER or type command to continue
Error detected while processing function <SNR>20_CheckAlign..GetPythonIndent:
line 30:
E15: Invalid expression: line('.') < 7 ? dummy : synIDattr(synID(line('.'), col('.')
, 1), 'name') =~ '\(Comment\|String\)
How do I fix this?
How do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我发现了问题所在。每当文件的选项卡设置与编辑器的选项卡设置不同时,它就会引发错误。例如,我的 test.py 文件设置为每个选项卡 2 个空格,选项卡扩展为空格,而我的编辑器设置为每个选项卡 4 个空格,不扩展。
因此,
solution解决方法是将 Vim 的选项卡设置设置为正在编辑的 python 文件的设置。I figured out the problem. It was throwing an error whenever the file's tab settings were different from the editor's tab settings. For example, my test.py file was set to 2 spaces per tab, with tabs expanded into spaces, whereas my editor was set to 4 spaces per tab, no expand.
So the
solutionworkaround was to set Vim's tab settings to the settings of the python file being edited.在 python 文件中使用以下模型行,其选项卡设置保持一致。
或者,您也可以在
.vimrc
文件中设置它们。这些是在使用 python 文件时确保一致性的最小集合。
有一些很棒的 vimrc 示例可供您使用。
Use the following modeline in your python files, that its tab settings are consistent.
Alternately, you have them set in your
.vimrc
file too.These are minimal set of things which you ensure consistency when working with python file.
There are some great vimrc examples available which you can use as well.
更改缩进设置对我来说不起作用,因此我通过修改 python 缩进文件(/path/to/vim/indent/python.vim)来解决这个问题。
在
GetPythonIndent
函数中,我只是用0
替换了dummy
的所有实例。这解决了我的问题。或者,您可以将 s:maxoff 设置为高得离谱,但这有点不太优雅。
Changing the indentation settings did not work for me so I worked around this by modifying the python indentation file (/path/to/vim/indent/python.vim).
In the
GetPythonIndent
function I simply replaced all instances ofdummy
with0
. This fixed the problem for me.Alternatively you could just set
s:maxoff
to something ridiculously high but this is somewhat less elegant.要解决这个问题,似乎必须编辑
/path/to/vim/indent/python.vim
中的函数GetPythonIndent()
。为此,必须阅读帮助文档、
user_30
、indent
、indentexpr
和indent-expression
(此外,C-indenting
包含有助于解释正在发生的情况的一般信息)。因此,最简单的解决方案是在编辑 python 文件时关闭缩进,直到您有时间处理这个恼人的错误:
您将在
/path/to/vim/indent/python.vim 中看到
该表达式被设置为运行有缺陷的函数GetPythonIndent
:将其设置为
""
以避免每次打开 python 文件时取消设置的不便。丹尼上面的答案看起来可能是更好的解决方案,因为它会保留某种形式的自动缩进。但这会让你立刻松一口气,省去阅读和理解 buggy 函数的麻烦。
To resolve the problem it seems one must edit the function
GetPythonIndent()
in/path/to/vim/indent/python.vim
.To do this, one must read in the help docs,
user_30
,indent
,indentexpr
, andindent-expression
(also,C-indenting
contains general info that will help explain what's going on).The simplest solution therefore is to switch indenting off when you are editing a python file, until such time as you have time to deal with this irritating bug:
You will see in
/path/to/vim/indent/python.vim
that expression is set to run the buggy functionGetPythonIndent
:Set it to
""
there to save the inconvenience of unsetting it every time you open a python file.Dani's answer above looks like it might be the better solution, since it would preserve some form of auto-indentation. But this will give you instant relief and save having to read and understand the buggy func.