2

Say I have a file:

// vim: syntax=rust fdm=syntax
{
  blah blah
}

Let's say that lines 2 through 4 are folded:

// vim: syntax=rust fdm=syntax
+ { ... }

Now I am in insert mode. I insert a newline on line 2, and begin typing {, then }.

I type {, and the folded block updates to being a deeper nesting:

// vim: syntax=rust fdm=syntax
{
+- { ... }

Then I type }, and the previously folded block recognizes it's gone up a nesting level, and opens:

// vim: syntax=rust fdm=syntax
{}
{
  blah
}

The behavior is also inconsistent with blocks that follow later in the file, wherein typing { opens them.

This occurs whether I'm using fdm=syntax or fdm=marker fmr={,}

How do I stop previously unfolded/folded blocks from closing/opening every time I write fold-markers earlier in the file?

I've been using the setlocal trick in my .vimrc:

autocmd InsertEnter * if !exists('w:last_fdm') | let w:last_fdm=&foldmethod | setlocal foldmethod=manual | endif
autocmd InsertLeave,WinLeave * if exists('w:last_fdm') | let &l:foldmethod=w:last_fdm | unlet w:last_fdm | endif

And it works great for files that don't have modelines setting the syntax and the fold method. For some strange reason it doesn't work when I have the modeline as described above.

  • I frequently use folds for navigating, but disable them when editing, as an example of an alternate workflow. One could set up InsertEnter/Leave autocmds to trigger e.g. zi and the like. (I too find this behavior annoying, and avoid it altogether—hoping for a solution.) – D. Ben Knoble Nov 21 '18 at 3:46

Your Answer

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

Browse other questions tagged or ask your own question.