Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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
up vote 3 down vote accepted

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
share|improve this answer
Yep, that works, thanks! – Andrew Ferrier Mar 4 '13 at 22:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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