How do I fix vim to properly indent folds containing Python comment lines?

一曲冷凌霜 提交于 2019-12-22 03:25:29

问题


I set vim's foldmethod to indent which works very well when writing Python except when I have a comment line. For example, if I have this bit of code:

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

If I have my cursor on the comment line and type "za" I get get an error saying "E490: No fold found." If I have the cursor on the line beginning with "myString = " I will geta fold like this:

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

In both cases I would like to get this fold:

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

Basically the comment line should be treated like anything else. I haven't come up with an answer from searching the web. Any ideas? Thanks!


回答1:


You have to set foldignore to nothing.

:set foldignore=

From :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|.



回答2:


From :help fold-indent:

Some lines are ignored and get the fold level of the line above or below it, whatever is the lowest. These are empty or white lines and lines starting with a character in 'foldignore'. White space is skipped before checking for characters in 'foldignore'. For C use "#" to ignore preprocessor lines.

At least in my vim, foldignore is set to the character '#'. Set it to empty, with a command like

:set foldignore=

to have those lines included in the fold.



来源:https://stackoverflow.com/questions/8993455/how-do-i-fix-vim-to-properly-indent-folds-containing-python-comment-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!