gpt4 book ai didi

python - Vim 自动删除 Python 注释的缩进

转载 作者:IT老高 更新时间:2023-10-28 21:33:15 25 4
gpt4 key购买 nike

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

Autoindent 通常工作得很好,但是当我开始一个新行并输入 '#' 来输入注释时,Vim 会为我取消缩进该行。

例如,如果有

def foo():

然后按回车,Vim 会正确缩进

def foo():
pass

但是,如果我不输入 pass,而是输入 #,它会自动取消缩进

def foo():
# comment

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

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

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

更新:来自 smartindent 上的文档:

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.

好像是这样。


更新:可能无需费心以下...我将把它留在这里以增加信息值(value)。 ;-)

如果设置 nosmartindent 没有帮助,也许你可以使用 :set 命令 -- 不带参数 -- 来获取所有有效设置的列表您的 Vim session ,然后将其粘贴到某处(可能在 Pastie 上)。据我记得,还有一些其他选项会影响自动缩进。

关于python - Vim 自动删除 Python 注释的缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2360249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com