# Simplifying Vim folding

**URL:** <https://www.ruby-forum.com/t/simplifying-vim-folding/60113>\
**Category:** Ruby\
**Created:** [May 10, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113 "2006-05-10T13:08:12Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/1 "2006-05-10T13:08:12Z")

</div>

G’day folks,

I was planning on simplifying the default folding provided when  
foldmethod=syntax.

Currently pretty much everything is folded but I was planning on  
reducing this to just module/class/method definitions, multi-line  
comments and **END** data chunks, with an option to disable comment  
folding.

We could, of course, make this ‘infinitely’ configurable but I see  
little point. Is anyone particularly enamoured with having their  
multi-line regexes folded? 😉

Any thoughts appreciated…

Thanks,  
Doug

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 1:30pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/2 "2006-05-10T13:30:02Z")

</div>

On 5/10/06, Doug K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> We could, of course, make this ‘infinitely’ configurable but I see  
> little point. Is anyone particularly enamoured with having their  
> multi-line regexes folded? 😉

Enamoured? No. But there are things that I really like being folded –  
multi-line hash definitions (I wish it extended to multiline array  
definitions).

All in all, I’m mostly happy with the state of folding in the folding.

This probably makes me unusual, but I’d much rather keep my cases and  
if statements foldable. If it’s changed in the “official”  
distribution, I will need to pretty much stop upgrading because method  
folding is too coarse for me.

-austin

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/3 "2006-05-10T14:00:25Z")

</div>

> > folding.
> 
> This probably makes me unusual, but I’d much rather keep my cases and  
> if statements foldable. If it’s changed in the “official”  
> distribution, I will need to pretty much stop upgrading because method  
> folding is too coarse for me.  
> Me too, I like the way folding works right now, folding pretty much  
> everything. That way I only unfold the part of code that I’m currently  
> interested in. Though sometimes I do make use of zR to get the bigger  
> picture… 🙂

I guess, if you make it configurable, it would be ok, so that we can  
switch ON the ‘full folding’ mode if we wanted to…

All the best,  
Alex

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 3:51pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/4 "2006-05-10T15:51:14Z")

</div>

I agree with you Doug, folding module, class & method make it easy to  
see the structure of your code still. Anyway, it would be nice if there  
could just be a switch so everyone can have their flavor. In the  
meantime, check out Mauricio F. blog. He’s got a couple really  
helpful bits for vim & ruby. This is the folding you want:

[http://eigenclass.org/hiki.rb?Usable+Ruby+folding+for+Vim](http://eigenclass.org/hiki.rb?Usable+Ruby+folding+for+Vim)

-Jeff

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/5 "2006-05-10T15:11:08Z")

</div>

On Wed, 10 May 2006 20:07:38 +0900, you wrote:

> Currently pretty much everything is folded but I was planning on  
> reducing this to just module/class/method definitions, multi-line

> Any thoughts appreciated…

occasionaly i get frustrated with folding by syntax that most of the  
ide’s do, so i use cream for vim which defaults to manual folding.  
with easy access keys you can create folds or whatever lines you want.

in my regular vim installation i have folding defaulted to marker but  
i find that tends to clutter up otherwise cluttered code 🙂

[http://home.cogeco.ca/~tsummerfelt1](http://home.cogeco.ca/~tsummerfelt1)  
telnet://ventedspleen.dyndns.org

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/6 "2006-05-10T17:21:34Z")

</div>

On 5/10/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i simply bit the bullet and always use marker - it’s very convenient if one  
> works with many many files since the folds endure when you close the file.  
> i’d also add folding of here-docs since, by definition, those are huge  
> multi-line blocks of string content that should nearly always be folded.

The standard folding for Ruby works with here-docs.

-austin

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/7 "2006-05-10T16:06:42Z")

</div>

On Wed, 10 May 2006, tony summerfelt wrote:

> with easy access keys you can create folds or whatever lines you want.
> 
> in my regular vim installation i have folding defaulted to marker but  
> i find that tends to clutter up otherwise cluttered code 🙂

i simply bit the bullet and always use marker - it’s very convenient if  
one  
works with many many files since the folds endure when you close the  
file.  
i’d also add folding of here-docs since, by definition, those are huge  
multi-line blocks of string content that should nearly always be folded.

s =  
#–{{{  
\<\<-s  
this  
should  
be  
folded  
s  
#–}}}

regards.

-a

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 11, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/8 "2006-05-11T18:36:07Z")

</div>

On Wed, May 10, 2006 at 08:07:38PM +0900, Doug K. wrote:

> We could, of course, make this ‘infinitely’ configurable but I see  
> little point. Is anyone particularly enamoured with having their  
> multi-line regexes folded? 😉

I’ve written a plugin that achieves something similar; it’s a much  
improved  
version of the vimscript I’d had in my .vimrc for a long time.

It is available at [eigenclass.org](http://eigenclass.org/hiki.rb?simplefold+plugin+0.4.0)  
There are also a few animations comparing it to fdm=syntax and  
fdm=marker.

Some features:

- folds on module/class/method/{constant,attribute} definitions  
(“top-level folds”)  
These folds are _not_ nested, so methods inside a class are always  
visible, even when all folds are closed. This way zM gets you a  
high-level  
view of all of the file without having to open some module/class  
folds.
- I’ve added folding on if/while/until/blocks/etc (“nestable folds”)  
(can be  
turned of, but it’s not a problem since we have zO)
- top-level folds are enlarged to include preceding comments (good for  
RDoc’ed code)
- the use of a specialized foldtext achieves over 60% savings in  
vertical  
space over fdm=marker at the class level.
- it is possible to specify additional “top-level” folds with #{{{ /  
#}}} and  
additional “nestable” folds with {{{{ / }}}}.

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 11, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/9 "2006-05-11T19:09:54Z")

</div>

On Fri, 12 May 2006, Mauricio F. wrote:

> - it is possible to specify additional “top-level” folds with #{{{ / #}}} and  
> additional “nestable” folds with {{{{ / }}}}.

you need to use

#–{{{ / #–}}}

#–{{{{ / #–}}}}

or else the #{{{ totally horks rdoc. fyi.

-a

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 11, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/10 "2006-05-11T19:28:35Z")

</div>

On Fri, May 12, 2006 at 02:08:09AM +0900, removed\_email\_address@domain.invalid wrote:

> or else the #{{{ totally horks rdoc. fyi.

I’m matching against /{{{{/ and /^\s\*[#%"0-9]{0,4}\s\*{{{[^{]/, so  
#–{{{{ would work fine. === ===  
^  
don’t remember why

Would

# {{{

(with extra spaces) also break rdoc? And ##{{{? While we’re at it, #%{{{  
should be safe.

#–{{{ is out because… well, I didn’t want it to conflict with your  
markers 🙂

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 11, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/11 "2006-05-11T19:41:00Z")

</div>

On Fri, 12 May 2006, Mauricio F. wrote:

> > #–{{{{ / #–}}}}
> 
> # {{{
> 
> (with extra spaces) also break rdoc? And ##{{{? While we’re at it, #%{{{  
> should be safe.
> 
> ## #–{{{ is out because… well, I didn’t want it to conflict with your markers 🙂
> 
> Mauricio F. - [http://eigenclass.org](http://eigenclass.org) - singular Ruby

boy. reading this post in vim is tough! there are a lot of folds! 😉

dunno about the spaces - i think i arrived at #-- because that’s some  
sort of  
flag for rdoc to ignore comments, so any such char should work.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 10, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/12 "2006-05-10T18:02:08Z")

</div>

On Thu, 11 May 2006, Austin Z. wrote:

> On 5/10/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > i simply bit the bullet and always use marker - it’s very convenient if one  
> > works with many many files since the folds endure when you close the file.  
> > i’d also add folding of here-docs since, by definition, those are huge  
> > multi-line blocks of string content that should nearly always be folded.
> 
> The standard folding for Ruby works with here-docs.

oh i know - just saying that here-docs should be included in a ‘minimal’  
folding config too imho.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 12, 2006, 3:11am UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/13 "2006-05-12T03:11:18Z")

</div>

On Fri, May 12, 2006 at 02:37:31AM +0900, removed\_email\_address@domain.invalid wrote:

> of flag for rdoc to ignore comments, so any such char should work.  
> Alright, I thought you meant that {{{ drove rdoc mad, when it’s just  
> that  
> they look bad in the docs 🙂 (at least that’s what it looks like after a  
> quick test).

rdoc/parsers/parse\_rb.rb:  
…  
def remove\_private\_comments(comment)  
comment.gsub!(/^#–._?^#++/m, ‘’)  
comment.sub!(/^#–._/m, ‘’)  
end  
…

So it seems things aren’t that bad:

- nestable folds will do fine, since we can use e.g. #-- {{{{, also  
avoiding  
conflicts with your markers
- no problem either with “top-level” folds: anyway, there’s no point in  
adding  
one just before a rdoc’able element (class, method…), since a fold  
would  
be created there automatically anyway.

Thanks for pointing this out, I’ll add a few words about rdoc in the  
docs.

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 13, 2006, 10:50am UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/14 "2006-05-13T10:50:27Z")

</div>

On Fri, May 12, 2006 at 01:35:03AM +0900, Mauricio F. wrote:

> I’ve written a plugin that achieves something similar; it’s a much improved  
> version of the vimscript I’d had in my .vimrc for a long time.
> 
> It is available at [eigenclass.org](http://eigenclass.org/hiki.rb?simplefold+plugin+0.4.0)  
> There are also a few animations comparing it to fdm=syntax and fdm=marker.

This looks interesting; I’ll add a note to the vim-ruby docs.

Regards,  
Doug

---

<div class="post-metadata">

**Author:** ![Doug\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@Doug\_K](https://www.ruby-forum.com/u/Doug_K)\
**Post date:** [May 13, 2006, 10:47am UTC](https://www.ruby-forum.com/t/simplifying-vim-folding/60113/15 "2006-05-13T10:47:23Z")

</div>

On Wed, May 10, 2006 at 08:27:32PM +0900, Austin Z. wrote:

> Enamoured? No. But there are things that I really like being folded –  
> multi-line hash definitions (I wish it extended to multiline array  
> definitions).

I’ll include something in the next release but in the meantime you can  
just add

syn region rubyArrayLiteral start="%(^|\s)[" end="]"  
contains=ALLBUT,@rubyExtendedStringSpecial,rubyTodo fold

to ~/.vim/after/syntax/ruby.vim. It should work for the common cases…

> All in all, I’m mostly happy with the state of folding in the folding.

Good. I was under the impression that almost everyone found the folding  
excessive.

> This probably makes me unusual, but I’d much rather keep my cases and  
> if statements foldable. If it’s changed in the “official”  
> distribution, I will need to pretty much stop upgrading because method  
> folding is too coarse for me.

I’ll make any changes configurable.

Thanks,  
Doug
