I would like to open all folds in a buffer recursively apart from folds which contain just text and no further folds.

I know that zO opens all folds in a buffer but as explained this is different from what I would like to achieve. Furthermore it will also not help to set a specific fold level because the depth in the fold hierarchy is very different.

Is there a way to achieve what I want?

1 Answer 1


Try this:

:folddoclosed if foldlevel('.') < foldlevel(line('.')+1) | exe 'norm! zo' | endif

Close all folds. Then, iterating over all lines in closed folds, check if the subsequent line has a greater foldlevel. If so, unfold the current line.

  • I tried to keybind the solution like this: nnoremap ,z :%foldc!<CR>:folddoclosed if foldlevel('.') < foldlevel(line('.')+1) | exe 'norm! zo' | endif. Unfortunately it does not work and I get some error messages E490 that there are no folds. May 23, 2017 at 8:42
  • ok, I found out that I need to substitute | with <bar> in a vimrc file. Now it works May 23, 2017 at 9:04
  • You can escape it too: \|.
    – Antony
    May 23, 2017 at 9:36

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.