As a result of this answer here, I'm trying to write a foldexpr for vim which is equivalent to foldmethod=indent (as a starting point to then enhancing it to recognise my custom markers, see the question for more details).

I've tried this:

set foldmethod=foldexpr
set foldexpr=indent(v:lnum)

However, this fails on this simple HTML example:

<div id="a">
    <div id="b">
        <div id="c">
            <div id="d">

        <div id="e">

If I place my key on the <div id="c"> line, and hit zc, the fold works as expected and folds only that div. If I re-open, then place my key on the <div id="b"> line, and hit zc, the fold fails as <div id="e"> is not folded.

If I switch to foldmethod=indent, this doesn't happen; the second fold is performed correctly. How can I fix my foldexpr to make it behave like foldmethod=indent?

  • Have you managed to get multiple foldmethods (marker and indent) working together using this approach? – blueyed Dec 17 '13 at 19:06
  • @blueyed, I'm afraid not. I put this project on hold for the time being. Would be interested to hear if you have any success. – Andrew Ferrier Dec 18 '13 at 9:34

Empty lines should keep the fold level of the previous line. Try this:

:set foldmethod=expr
:set foldexpr=empty(getline(v:lnum))?'=':indent(v:lnum)/4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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