损坏的 VIM 缩进


有时,

我的python脚本中的标识突然变得,我想你可以说是损坏了。标识会突然改变,使我的程序失败。

如果我使用 cat 查看文件,我可以看到识别是错误的。但在 VIM 中,它显示为正常。这是输出和设置,

任何想法???

通过"猫-e"

              validate_hostname = RegexValidator(regex=r'[a-zA-Z0-9-_]*.[a-zA-Z]{2,6}',message="Enter a valid hostname.")$
           validate_hostname(host_input)$
   except ValidationError, e:$
               print type(e)$
       print str(e[0])$
       error = str(e)$
else:$
       error = "Please complete all fields."       $
   $
   print error$
   return [error,host_input,record_input]$

在 VIM 中

                   validate_hostname = RegexValidator(regex=r'[a-zA-Z0-9-_]*.[a-zA-Z]{2,6}',message="Enter a valid hostname.")
                   validate_hostname(host_input)
           except ValidationError, e:
               print type(e)
               print str(e[0])
               error = str(e)
       else:
           error = "Please complete all fields."
       print error
       return [error,host_input,record_input]

我的 .vimrc 看起来像,

syntax on
se bg=dark
set tabstop=4      " insert 4 spaces when a tab is pressed
set shiftwidth=4   " change the number of space characters inserted for indentation
set expandtab      " insert spaces whenver a tab key is pressed

看起来你有混合的空格和制表符。代码在vimcat -e(或简称less)中看起来不同,因为它们对选项卡使用不同的宽度,由于您的set tabstop=4

如果在vim它看起来不错,那么:retab应该可以修复它:它将用您看到的空格量替换制表符。结果看起来相同,但所有制表符都将消失。

执行retab之前,拥有正确的tabstop值很重要。例如,如果你有相反的问题 - 代码在less中看起来正确,但在vim中被破坏,并且你在这种状态下:retab,这将破坏Python脚本。

看看这篇关于 vim 中选项卡的精彩文章:

http://vimcasts.org/episodes/tabs-and-spaces/

特别是,我认为您应该将这些设置添加到您的.vimrc

set softtabstop=4
set smarttab

特别是在 Python 中,空格很重要,你不应该混合制表符和空格。即使您在 Vim 中仔细设置了缩进设置(甚至可能在每个文件中包含模式行来设置缩进),编辑该文件的其他用户也可能不太小心。

因此,我编写了IndentConsistencyCop插件,该插件验证缩进,并在缩进不一致时抱怨。插件页面包含指向替代插件的链接。

最新更新