Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

vim

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!

like image 823
Alex Avatar asked Jan 24 '12 20:01

Alex


2 Answers

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|.
like image 92
Susam Pal Avatar answered Oct 11 '22 19:10

Susam Pal


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.

like image 25
Ian Clelland Avatar answered Oct 11 '22 17:10

Ian Clelland