简体   繁体   English

使用 Vim 编辑 python 时出错

[英]Errors editing python with Vim

When I edit a python file in Vim (using MacVim), and I press o to insert a new line, Vim throws the following errors:当我在 Vim(使用 MacVim)中编辑一个 python 文件时,我按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\)$'

How do I fix this?我该如何解决?

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.例如,我的 test.py 文件设置为每个选项卡 2 个空格,选项卡扩展为空格,而我的编辑器设置为每个选项卡 4 个空格,不扩展。

So the solution workaround was to set Vim's tab settings to the settings of the python file being edited.所以 解决 方案是将 Vim 的选项卡设置设置为正在编辑的 python 文件的设置。

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).更改缩进设置对我不起作用,因此我通过修改 python 缩进文件 (/path/to/vim/indent/python.vim) 解决了这个问题。

In the GetPythonIndent function I simply replaced all instances of dummy with 0 .GetPythonIndent function 中,我只是用0替换了dummy的所有实例。 This fixed the problem for me.这为我解决了问题。

Alternatively you could just set s:maxoff to something ridiculously high but this is somewhat less elegant.或者,您可以将s:maxoff设置为高得离谱的值,但这有点不那么优雅。

Use the following modeline in your python files, that its tab settings are consistent.在您的 python 文件中使用以下模式行,其选项卡设置是一致的。

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

Alternately, you have them set in your .vimrc file too.或者,您也可以在.vimrc文件中设置它们。

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.这些是您在使用 python 文件时确保一致性的最小集合。 There are some great vimrc examples available which you can use as well.您也可以使用一些很棒的 vimrc 示例。

To resolve the problem it seems one must edit the function GetPythonIndent() in /path/to/vim/indent/python.vim .要解决这个问题,似乎必须编辑/path/to/vim/indent/python.vim中的 function GetPythonIndent()

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).为此,必须阅读帮助文档user_30indentindentexprindent-expression (另外, C-indenting包含有助于解释正在发生的事情的一般信息)。

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

:set indentexpr=""

You will see in /path/to/vim/indent/python.vim that expression is set to run the buggy function GetPythonIndent :您将在/path/to/vim/indent/python.vim中看到该表达式设置为运行错误 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.将它设置为""以节省每次打开 python 文件时取消设置的不便。

Dani's answer above looks like it might be the better solution, since it would preserve some form of auto-indentation. Dani 上面的回答看起来可能是更好的解决方案,因为它会保留某种形式的自动缩进。 But this will give you instant relief and save having to read and understand the buggy func.但这将使您立即松一口气,而无需阅读和理解错误的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM