Vim自动删除Python注释上的缩进

Vim自动删除Python注释上的缩进

本文介绍了Vim自动删除Python注释上的缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Vim并编辑Python脚本.

I'm using Vim and editing Python scripts.

一般来说,自动缩进效果很好,但是当我开始换行并输入'#'来输入注释时,Vim会为我取消缩进.

Autoindent works pretty well in general, but when I start a new line and type '#' to type a comment, Vim unindents that line for me.

例如,如果有

def foo():

然后按Enter,Vim会正确缩进

and I press enter, Vim will indent properly

def foo():
    pass

但是,如果我没有键入pass,而是键入#,它将自动缩进

but, if instead of typing pass, I type #, it unindents automatically

def foo():
# comment

class Thing():
    def __init__(self):
         pass
# comment line gets unindented all the way

我的.vimrc文件如下.有人知道为什么会这样吗?

my .vimrc file follows. anyone know why this is happening?

set tabstop=4
set smartindent
set shiftwidth=4
set expandtab
set backspace=indent,eol,start
set scrolloff=3
set statusline=%f%m%r%h%w\ [%Y\ %{&ff}]\ [%l/%L\ (%p%%)]
set laststatus=2

推荐答案

设置smartindent会使Vim像您为我描述的那样运行,而对于nosmartindent(这是我倾向于使用的),Vim的行为类似于您' d喜欢它.

Setting smartindent on makes Vim behave like you describe for me, whereas with nosmartindent (which is what I tend to use) it behaves like you'd prefer it to.

更新:来自smartindent上的文档:

就是这样.

更新:可能无需理会以下内容...我将其保留在此处以获取补充的信息价值. ;-)

Update: Probably no need to bother with the following... I'll leave it here for the added informational value. ;-)

如果设置nosmartindent没有帮助,也许您可​​以使用:set命令(不带参数)获取Vim会话中有效的所有设置的列表,然后将其粘贴到某个位置(在 Pastie ).据我所知,还有其他一些选项会影响自动缩进.

If setting nosmartindent doesn't help, perhaps you could use the :set command -- with no parameters -- to obtain the list of all settings in effect in your Vim session, then paste it somewhere (on Pastie perhaps). There's a few other options which affect automatic indentation, as far as I remember.

这篇关于Vim自动删除Python注释上的缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:28