eigenclass logo
MAIN  Index  Search  Changes  PageRank  Login

simplefold: better vim folding (Ruby, Objective Caml, Perl, PHP, Java)

simplefold is a small vim script that improves (for some definition of "improve", see the description + screenshots below) on other folding methods (syntax, marker, indent, expr). It was originally written for Ruby, but now also supports Objective Caml, PHP, Perl and Java. It features:

  • optimized vertical space usage
  • sensible foldtext
  • top-level folds: one per interesting definition. No need to open a class fold to see which methods it contains. Get a quick overview of the classes/methods/functions (whatever applies in the current filetype) with zM.
  • optional nested folds for if/while and so on
  • easy to adapt to other filetypes; just by setting 2-3 regexps

simplefold 0.5.0 introduces Objective Caml, Perl and PHP support, a few bugfixes and a new way to define fold boundaries.

Getting it

The last tarball is simplefold-0.5.0.tar.gz.

You can also access the darcs repository (you can explore it from your browser too) at http://eigenclass.org/repos/simplefold .

simplefold vs. fold_syntax

Both images correspond to the same files; I've timed them carefully to show equivalent views (top-level, class-level, method-level) at a time (you might have to reload the page to make sure they play synchronously). The first one is fdm=syntax, the second is simplefold's syntax.gif simplefold1.gif

  • syntax doesn't fold comments, so the window covers but a small part of the buffer even when all folds are closed
  • syntax uses nested folds for nested classes/modules/methods; simplefold makes them all top-level so there's no need to open class/module folds to see their methods (fdm=syntax with foldnestmax is not enough)
  • syntax folds on lots of things, which is good or bad depending on your preferences

simplefold vs. fold_marker

marker.gif simplefold2.gif

  • markers are robust and portable, but need some work and pollute the code
  • simplefold is almost a superset of fdm=marker because you can specify nestable folds with
# open a fold { { { { (four braces w/o spaces, hiki doesn't let me write that) 
.... stuff
# and now close it }}}} (the markers can be anywhere in the line)

(yes, FOUR braces, so that legacy markers are ignored) and top-level ones with

 #{ { {   # this line must match ^\s*\{\{\{
 #{ { { this opens a new fold 
  • simplefold optimizes vertical space usage: no markers (by default) cluttering the code. This, and the "sticky comments" (which get folded along with the next method/class/etc) plus the use of foldtext help achieve over 66% vertical space savings in the "class-level" view.


Just place the .vim file in the plugin directory (e.g. ~/.vim/plugin) and use <Leader>f (by default \f) to fold.

You can also use 

to create folds showing the lines matching the given regexp, so that everything else is hidden; e.g.

 :Fold \vfoo(bar|baz)

will create folds starting at each line matching foo(bar|baz), so that these are the only lines you see in your buffer (it's effectively a sort of internal grep).

(Comments will eventually be moved to a version-specific node)

Nice! - Aredridel (2007-04-18 (Wed) 14:55:31)

First folding I've found worth the bother.

No Title - Manpreet (2007-04-18 (Wed) 18:21:28)

Will you be posting it to vim.org?

mfp 2007-04-19 (Thr) 10:24:43

yes it's become useful enough to warrant it, I'm posting it

Wonderful - Mike (2007-04-20 (Fri) 05:52:49)

Awesome script. I love it already.

I'm trying to construct a java_simplefold_prefix which will capture javadoc (c-style) comments. Can you easily construct one?

mfp 2007-04-20 (Fri) 15:29:57

I believe this cannot really be done at the moment because the xxx_simplefold_prefix regexp must match a single line. If you're leaving at least an empty line between each method/var, and the javadoc is immediately above the definition it applies to, "\v\S" would do, though.

I'll try to add a simple way to recognize such comments using searchpair.

mfp 2007-04-20 (Fri) 15:56:21

On second thought, \v\S won't work either, it'd break the foldtext.

If the c-style comments include a leading *, as in

 * adasd asd
 * asasdasdas
 * adasdas */
 private int duration;

this will work: '\v(\s*\*|\s*/\*|\*/)'

This? Amazing. - Clarke Brunsdon (2007-05-04 (Fri) 23:41:49)

This is exactly what I was looking for in a folding plugin, thanks a lot for sharing man.

Last modified:2007/04/17 12:03:39
Keyword(s):[blog] [ruby] [frontpage] [vim] [folding] [simplefold]