如何修复vim以正确缩进包含Python注释行的折叠?
我把vim的折叠方式设置成了根据缩进来折叠,这在写Python代码的时候效果很好,但在有注释行的时候就不太行了。比如,我有这么一段代码:
def myFunction():
# here is my comment
myString = "hello"
myInt = 2
如果我把光标放在注释行上,然后输入“za”,就会出现一个错误,提示“E490: 找不到折叠。”但是如果我把光标放在以“myString = ”开头的那一行上,就能得到这样的折叠:
def myFunction():
# here is my comment
+--- 2 lines: myString = "hello" -------------------------
在这两种情况下,我都希望能得到这样的折叠:
def myFunction():
+--- 3 lines: # here is my comment -------------------------
基本上,注释行应该和其他行一样被处理。我在网上搜索了一下,但还没有找到解决办法。有没有什么想法?谢谢!
2 个回答
3
来自 :help fold-indent
的内容:
有些行会被忽略,它们会得到上面或下面那一行的折叠级别,取决于哪个级别更低。这些行包括空行、空白行,以及以 'foldignore' 中的字符开头的行。在检查 'foldignore' 中的字符之前,会跳过空白部分。对于 C 语言,可以用 "#" 来忽略预处理器的行。
至少在我的 vim 中,foldignore
被设置为字符 '#'。如果你想让这些行也包含在折叠中,可以用类似下面的命令将其设置为空:
:set foldignore=
这样就可以把这些行也算进折叠里了。
21
你需要把foldignore设置为空。
:set foldignore=
来自 :help foldignore
:
'foldignore' 'fdi' string (default: "#")
Used only when 'foldmethod' is "indent". Lines starting with
characters in 'foldignore' will get their fold level from surrounding
lines. White space is skipped before checking for this character.
The default "#" works well for C programs. See |fold-indent|.