Vim 自动删除 Python 注释上的缩进 [英] Vim automatically removes indentation on Python comments

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

问题描述

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

I'm using Vim and editing Python scripts.

Autoindent 通常工作得很好,但是当我开始一个新行并输入 '#' 来输入注释时,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():

然后按回车,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 (这就是我倾向于使用)它的行为就像你喜欢的那样.

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: From the docs on smartindent:

当输入'#'作为新行的第一个字符时,缩进该行被删除,'#' 放在第一列.缩进为下一行恢复.如果你不想要这个,使用这个映射: ":inoremap # X^H#",其中 ^H 是用 CTRL-V CTRL-H 输入的.使用>>"命令时,以#"开头的行不会移位没错.

When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H. When using the ">>" command, lines starting with '#' are not shifted right.

好像是这样.

更新:可能不需要理会以下内容...我将把它留在这里以增加信息价值.;-)

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 注释上的缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆