简体   繁体   中英

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:

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.

So the solution workaround was to set Vim's tab settings to the settings of the python file being edited.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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