1

If I am in an open fold containing closed folds, how do I recursively open all those closed folds?

I kind of thought this would be the use-case for zO, but it seems zO only works when the fold is already closed. Is a better way to accomplish this than closing and re-opening the current fold recursively (za zA)?

4

I don't think there's anything shorter than zczA. That's pretty easy and probably what I'd do. I guess you could shorten it a bit with a mapping ;)

:nnoremap <leader>O zczA

But let's say you absolutely can't or don't want to close the already open fold. This is what I came up with...


It's not a Normal mode command but from the command line foldopen might do what you want...

:{range}foldo[pen][!]

Let's say you have folds like this...

10: F1<
+1: ...
+2: F1.1>
??: ...
??: F1.1.1>
??: ...
+3: ...
+4: F1.2>

Here < indicates an open fold, > closed. F1 is an open fold at line 10. F1.1 is a closed fold nested inside F1 that starts +2 lines down. ?? indicates that we can't currently see that line.

If you're on line 10 then this will open F1.1:

:+2foldo

Add a bang, :+2foldo! and it will operate recursively so F1.1.1 will open as well. With range +4 you'll also open F1.2 as well as any folds nested inside it (if you are using !).

Hope that makes sense. Play around with it. Note the close equivalent; it is the same format with name foldc[lose].

If you're using the Marker type of folds (surrounded with {{{ and }}}) then an alternative is to go the end of the first line of the open fold of interest, put the cursor on one of the three {, hit V% which will select everything inside the fold and then hit zO for recursive open.

None of this is as nice as a simple, short Normal mode key sequence...perhaps someone knows folds better than me and has a better idea...but you could use a mapping to keep it in Normal mode. I haven't tested this yet but I'm thinking along the lines of:

:nnoremap <expr> <leader>zO ':+' . v:count1 . 'foldo!<CR>'

So Normal mode sequence 2\zO should do the same as :+2foldo!.

Your Answer

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

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