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?

up vote 0 down vote accepted

Try this:

zM
: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. – Cutú Chiqueño May 23 '17 at 8:42
  • ok, I found out that I need to substitute | with <bar> in a vimrc file. Now it works – Cutú Chiqueño May 23 '17 at 9:04
  • You can escape it too: \|. – Antony May 23 '17 at 9:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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