For some Markdown documents, I like them to open with some sections initially folded (I use the plugin vim-markdown-folding). I've tried setting a modeline that looks like this:

<!-- vi: set foldmethod=indent foldlevel=1: -->

This works, but unfortunately, every time I save the buffer, even with it still open, the folding is reset back to foldlevel 1. I'd really like it if the modeline only took effect when the file is first opened.

Is this possible?

  • 1
    does this happen with vim --clean? I wonder if there is some autocommand or something triggering this; nothing in the modeline help indicates this is default behavior
    – D. Ben Knoble
    Commented May 21, 2020 at 21:35
  • 2
    Does it prevent Vim from re-reading the modeline if you use :noautocmd w to save?
    – filbranden
    Commented May 22, 2020 at 1:55
  • 1
    Yes, @D.BenKnoble is right. It shouldn't be so. Try :au BufWrite to see if it's set up by some plugin or such.
    – Matt
    Commented May 22, 2020 at 8:44

1 Answer 1


Thanks everyone for the helpful comments. I didn't realise that this was not default behaviour. But with some binary searching in disabling plugins, I was able to find out that this was due to my use of the vim-markdown-folding plugin (which in retrospect seems obvious, sorry). Its foldexpr overrides the one I was using and causes this issue. I may discontinue my use of the plugin or see if there is a way to use it which can be disabled for specific files.

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.