当我编辑 Python 文件时,例如:
def my_func():
print('Something')
<-- CURSOR IS HERE
Run Code Online (Sandbox Code Playgroud)
我想通过输入 a 添加注释#
,该行会自动重新缩进到该行的开头:
def my_func():
print('Something')
#<-- CURSOR IS HERE
Run Code Online (Sandbox Code Playgroud)
我发现这是该smartindent
选项的影响,因此要修复它,我只需运行:set nosi
(或在我的 .vimrc 中禁用它)。
但在 Vim 的帮助中,:h 'smartindent'
您可以阅读以下内容:
当 'cindent' 打开或设置 'indentexpr' 时,设置 'si' 无效。
但我的indentexpr
选项设置如下:
:set indentexpr?
indentexpr=GetPythonIndent(v:lnum)
Run Code Online (Sandbox Code Playgroud)
我当然应该完全避免使用smartindent
选项,因为它看起来是一个旧功能,并且仅设计用于 C 风格语言;
但我想知道,smartindent
考虑到帮助中写的内容,为什么在编辑 python 文件时会产生一些影响?
是的,它的行为确实与您解释的方式相同。
就我而言:echo GetPythonIndent(v:lnum)
返回-1
:h indentexpr
有以下文字解释该行为。
The expression must return the number of spaces worth of indent. It
can return "-1" to keep the current indent (this means 'autoindent' is
used for the indent).
Run Code Online (Sandbox Code Playgroud)
当我们设定时si
,它就会接管ai
。
现在:h si
建议一个解决方法:
:inoremap # X^H#
Run Code Online (Sandbox Code Playgroud)
其中 ^H 输入为Ctrl+ V Ctrl+H
我相信您会得到比所提供的解决方案更好的解决方案