3

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)?

1 Answer 1

8

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!.

Update: @ropeladder has a Normal/Visual mode command that doesn't require closing the outer fold first:

  • ]z : jump to end of current open fold
  • <s-v> : (Shift-V) start linewise visual mode
  • [z : jump to beginning of current open fold
  • zO : recursively open all folds that are visually selected

Of course, it'd be much easier to close the outer fold first and use zczA but since this doesn't close first and isn't on the command line I figured I'd note it. (And bonus points for ]z/[z.)

2
  • How about ]z<shift-v>[zzO? That should automatically select the entire contents of the current partially-open fold, and then open them recursively.
    – ropeladder
    Commented Apr 18, 2020 at 16:12
  • @ropeladder A little long but clever enough to merit a mention in my answer. :)
    – B Layer
    Commented Apr 19, 2020 at 12:44

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.