如何修复vim以正确缩进包含Python注释行的折叠?

15

我将vim的foldmethod设为了indent,在编写Python代码时非常有效,但是当我有一行注释时就出现了问题。比如,如果我的代码是这样的:

def myFunction():
    # here is my comment
    myString = "hello"
    myInt = 2

如果我的光标在注释行上,然后我键入"za",就会收到错误消息"E490: No fold found." 如果我的光标在以 "myString =" 开头的行上,就会出现像这样的折叠:

def myFunction():
    # here is my comment
+--- 2 lines: myString = "hello" -------------------------

在这两种情况下,我都希望能够得到这个折叠:

def myFunction():
+--- 3 lines: # here is my comment -------------------------

基本上,注释行应该像其他任何东西一样处理。我在网上搜索中没有找到答案。有什么想法吗?谢谢!

2个回答

21
您需要将foldignore设置为nothing。
: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|.

3

来自:help fold-indent

一些行会被忽略,它们的折叠层级与其上面或下面的行相同,取决于两者中较小的那个。这些行包括空行、以及以'foldignore'中的字符开头的行。在检查'foldignore'中的字符之前先跳过空格。对于C语言,请使用"#"来忽略预处理器行。

至少在我的vim中,foldignore设置为字符"#", 可以使用命令将其设置为空:

:set foldignore=

要将这些行包含在折叠中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接