close all folds but current?

344 views
Skip to first unread message

steen

unread,
Jun 4, 2013, 9:41:26 PM6/4/13
to vim...@googlegroups.com
Hey folks,

I have a problem that I suspect there is an invocation or possibly a plugin for, but I have been unable to find or figure it out. I work a lot with outline-style documents, where sections are defined by either indentation or occasionally markdown headers (mostly using Vimwiki or TVO), so I use folding extensively. What I would like to do is to be able to close all folds in a document except the one I am currently working in. 

I've had a few coder friends also tell me they would find this useful in that context as well, and I'm surprised to not find an easy way to do this already. 

Any suggestions?

-- Steen

Tim Chase

unread,
Jun 4, 2013, 10:18:01 PM6/4/13
to vim...@googlegroups.com, media...@gmail.com
On 2013-06-04 18:41, steen wrote:
> What I would like to do is to be able to close all folds in a
> document except the one I am currently working in.

It sounds like

zMzO

would do what you want: "zM" closes all folds, then "zO" (that's
a capital oh-for-open, not a zero) opens all folds under the cursor.
Alternatively, you might be looking for

zMzv

Either can be pretty easily mapped. If neither does what you're
looking for, perhaps you could expand on how you'd want it behave
differently from either of those.

To read up more:

:help zM
:help zO
:help zv

-tim


Erik Christiansen

unread,
Jun 5, 2013, 3:05:53 AM6/5/13
to vim...@googlegroups.com
On 04.06.13 21:18, Tim Chase wrote:
> On 2013-06-04 18:41, steen wrote:
> > What I would like to do is to be able to close all folds in a
> > document except the one I am currently working in.
>
> It sounds like
>
> zMzO
>
> would do what you want: "zM" closes all folds, then "zO" (that's
> a capital oh-for-open, not a zero) opens all folds under the cursor.
> Alternatively, you might be looking for
>
> zMzv
>

There's a minor variant which also works. I've adopted the practice of:

zM<right-arrow> # Or zMl, if you're a hjkl-er.

to close all folds, then open the current one. Since <right-arrow> is
what I use for opening a fold in the first place, it's used often enough
that I don't sit there wondering which z-whatsit I'm trying to remember
for the less common use-case.

Erik

--
You will stop at nothing to reach your objective, but only because your
brakes are defective.

Christian Brabandt

unread,
Jun 5, 2013, 3:15:26 AM6/5/13
to vim...@googlegroups.com
On Wed, June 5, 2013 09:05, Erik Christiansen wrote:
> On 04.06.13 21:18, Tim Chase wrote:
>> On 2013-06-04 18:41, steen wrote:
>> > What I would like to do is to be able to close all folds in a
>> > document except the one I am currently working in.
>>
>> It sounds like
>>
>> zMzO
>>
>> would do what you want: "zM" closes all folds, then "zO" (that's
>> a capital oh-for-open, not a zero) opens all folds under the cursor.
>> Alternatively, you might be looking for
>>
>> zMzv
>>
>
> There's a minor variant which also works. I've adopted the practice of:
>
> zM<right-arrow> # Or zMl, if you're a hjkl-er.
>
> to close all folds, then open the current one. Since <right-arrow> is
> what I use for opening a fold in the first place, it's used often enough
> that I don't sit there wondering which z-whatsit I'm trying to remember
> for the less common use-case.

That only works, if the cursor is not on the last char of the current line
(in which case right-arrow is a noop and therefore doesn't open the fold).
It might work with :set ve=onemore however.

I have reported this a bug to Bram (and even provided a fix), but Bram
didn't want to change it.

regards,
Christian

Erik Christiansen

unread,
Jun 6, 2013, 2:22:10 AM6/6/13
to vim...@googlegroups.com
On 05.06.13 09:15, Christian Brabandt wrote:
> On Wed, June 5, 2013 09:05, Erik Christiansen wrote:
> > There's a minor variant which also works. I've adopted the practice of:
> >
> > zM<right-arrow> # Or zMl, if you're a hjkl-er.
> >
> > to close all folds, then open the current one. Since <right-arrow> is
> > what I use for opening a fold in the first place, it's used often enough
> > that I don't sit there wondering which z-whatsit I'm trying to remember
> > for the less common use-case.
>
> That only works, if the cursor is not on the last char of the current line
> (in which case right-arrow is a noop and therefore doesn't open the fold).
> It might work with :set ve=onemore however.

That's surely an alternative, but it already works for me, because I
have: whichwrap=<,>,[,]

(It'll be '>' which is facilitating it, IIUC.)

> I have reported this a bug to Bram (and even provided a fix), but Bram
> didn't want to change it.

That seems fine. If there is anyone who cannot tolerate cursor wrapping,
but absolutely needs the shorter and (IME) more easily remembered
zM<right-arrow> form, then your option is the way forward. ISTM that
Vim is admirably (and _almost_ perfectly) configurable.

<Apropos_Perfection>
Now, if the new regex engine for Vim 7.4 supports Posix EREs, without
the need for leading "\v", e.g. with a config selection instead, then
Vim is Purrrrfect! :-)
</Apropos_Perfection>

Erik
(Who still can't get over how niftily Vim's folding works.)

--
Very few things happen at the right time, and the rest do not happen at all.
The conscientious historian will correct these defects.
- Mark Twain, in "A Horse's Tale"

steen

unread,
Jun 7, 2013, 4:44:01 AM6/7/13
to vim...@googlegroups.com
Thanks for all the replies you guys. 

Turns out zMzv is what I was looking for. Parsing error on my part of the helpfile, mostly. I am kind of still surprised that vim doesn't natively have such a "focus" behavior as a z command, to mitigate the fact that you are essentially blind for the first half of that. But, I can see the argument either way.

Now the only issue I am having is with what I perceive to be inconsistent behavior in foldmethod=indent, where it looks sometimes the same indentations of line get included in a single fold. I suspect this is something about me not understanding whitespace, so I should do further testing before I ask any real questions about it.

Thanks for the assist. This list is really fantastic and you guys are awesome.

Cheers,

-- Steen



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Erik Christiansen

unread,
Jun 7, 2013, 5:21:25 AM6/7/13
to vim...@googlegroups.com
On 07.06.13 01:44, steen wrote:
> Now the only issue I am having is with what I perceive to be inconsistent
> behavior in foldmethod=indent, where it looks sometimes the same
> indentations of line get included in a single fold. I suspect this is
> something about me not understanding whitespace, so I should do further
> testing before I ask any real questions about it.

One test I'd do in that case is:

:set expandtab
:retab

to convert all mixes of spaces and tabs to just spaces.
Then see if fold behaviour improves, given the more consistent whitespace.

It may change nothing, but you'd then know one thing which is not the
cause.

Erik

--
I can only reply that I arrived at it by experimenting until I found
something that works, and then stopping. - Rick Moen, on luv-main

Charles Campbell

unread,
Jun 7, 2013, 11:34:59 AM6/7/13
to vim...@googlegroups.com
Erik Christiansen wrote:
> On 07.06.13 01:44, steen wrote:
>> Now the only issue I am having is with what I perceive to be inconsistent
>> behavior in foldmethod=indent, where it looks sometimes the same
>> indentations of line get included in a single fold. I suspect this is
>> something about me not understanding whitespace, so I should do further
>> testing before I ask any real questions about it.
> One test I'd do in that case is:
>
> :set expandtab
> :retab
>
> to convert all mixes of spaces and tabs to just spaces.
> Then see if fold behaviour improves, given the more consistent whitespace.
>
> It may change nothing, but you'd then know one thing which is not the
> cause.
>
> Erik

I use the following map:invoke it by hitting the space bar...
nno <silent> <space> :exe 'sil! norm! '.((foldclosed('.')>0)?
"zMzxzt" : 'zc')<cr>

In order to get just the current fold the only open one, I usually just
slap the space bar twice.

Regards,
C Campbell


steen

unread,
Jun 10, 2013, 8:13:49 PM6/10/13
to vim...@googlegroups.com
That is very useful to know. I have been converting my most used files to markdown headers and using vimwiki's markdown fold method, but the next time I make a sprawling outline for a novel I'll try this and see if it helps. Thanks!

-- Steen


Reply all
Reply to author
Forward
0 new messages