使用 Vim 编辑 python 时出错

发布于 2024-10-15 05:36:51 字数 508 浏览 3 评论 0原文

当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滥情哥ㄟ 2024-10-22 05:36:51

我发现了问题所在。每当文件的选项卡设置与编辑器的选项卡设置不同时,它就会引发错误。例如,我的 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 solution workaround was to set Vim's tab settings to the settings of the python file being edited.

伴梦长久 2024-10-22 05:36:51

在 python 文件中使用以下模型行,其选项卡设置保持一致。

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

或者,您也可以在 .vimrc 文件中设置它们。

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

这些是在使用 python 文件时确保一致性的最小集合。
有一些很棒的 vimrc 示例可供您使用。

Use the following modeline in your python files, that its tab settings are consistent.

# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

Alternately, you have them set in your .vimrc file too.

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

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.

将军与妓 2024-10-22 05:36:51

更改缩进设置对我来说不起作用,因此我通过修改 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 of dummy with 0. This fixed the problem for me.

Alternatively you could just set s:maxoff to something ridiculously high but this is somewhat less elegant.

简单爱 2024-10-22 05:36:51

要解决这个问题,似乎必须编辑 /path/to/vim/indent/python.vim 中的函数 GetPythonIndent()

为此,必须阅读帮助文档、user_30indentindentexprindent-expression (此外,C-indenting 包含有助于解释正在发生的情况的一般信息)。

因此,最简单的解决方案是在编辑 python 文件时关闭缩进,直到您有时间处理这个恼人的错误:

:set indentexpr=""

您将在 /path/to/vim/indent/python.vim 中看到 该表达式被设置为运行有缺陷的函数 GetPythonIndent

setlocal indentexpr=GetPythonIndent(v:lnum)

将其设置为 "" 以避免每次打开 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, and indent-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:

:set indentexpr=""

You will see in /path/to/vim/indent/python.vim that expression is set to run the buggy function GetPythonIndent:

setlocal indentexpr=GetPythonIndent(v:lnum)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文