0

I have a file with foldmethod=indent. I have the folds automatically open when I enter them, and close when I leave them. When I sit in Normal mode on the bottom line of a fold and press o, the fold closes and I can't see what is in it.

I want Vim to understand that when I'm in a fold and type a command that leaves the fold but enters Insert mode, I want that fold to stay open.

2
  • I assume you're using :set foldclose=all, correct? I don't think there's a setting that will prevent the fold from closing while you append a new line with o at the end of a fold when that setting is enabled though...
    – filbranden
    Nov 3, 2020 at 17:24
  • I am indeed using :set foldclose=all. How about using autocmd InsertEnter ...? That's what I tried before, although I am not good enough at Vimscript to make it work.
    – Not me
    Nov 5, 2020 at 0:34

1 Answer 1

1

It seems like the indent fold method has some flaws, and this is one of them. My solution might not be the best, but here it is:

I created my own fold implementation in my vimrc:

function! FoldLevel(lnum)
    return ( max([
        \     indent(prevnonblank(a:lnum)),
        \     indent(nextnonblank(a:lnum))
        \ ]) / getbufvar('.', '&tabstop', 1) )
endfunction


set foldmethod=expr
set foldexpr=FoldLevel(v:lnum)

You just need to put that in your .vimrc, and you should be good to go!

Your Answer

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

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