3

I am new to vim.

I have a HTML code with lots of <li></li> with its content that could span multiply lines like below:

<ol>
    <li>
        text
        text
    </li>
    <li>
        text
        text
    </li>
</ol>

When I apply :set foldmethod=syntax, vim does nothing on folding that tags. When I apply :set foldmethod=indent, vim apply code folding like this:

<ol>
    <li>
+---- 2 lines: text------
    </li>
    <li>
+---- 2 lines: text------
    </li>
</ol>

However, I intend to fold the code like when I use manual folding zfat:

<ol>
+---- 4 lines: <li>------
+---- 4 lines: <li>------
</ol>

So that it is more condensed and, at the same time, I know it is a listed items.

The problem is: how do I achieve this with auto folding?

1 Answer 1

2

You can achieve this using foldexpression. The best would be to create a function, that analyzes each line and returns an actual foldlevel. Here is a short example, that should get you started:

set fen fdm=expr fde=getline(v:lnum)=~#'<[^>]*>'?'a1':getline(v:lnum)=~#'</[^>]*>'?'s1':'='

Read the details at :h fold-expr (especially the special meaning of the return values for the evaluation of the expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.