[Tipz] Vim - using folds effectively

Ian Langworth bass at ccs.neu.edu
Wed Jan 29 09:35:17 EST 2003


If you're not familiar with them, "folds" allow you to condense multiple
lines into a single line in the editor. For example, if you were editing
a 5,000-line source file and had each subroutine folded, you might be
looking at only 20 lines plus only the subroutine you wanted to edit.
For each fold you only see the first line of text from within the fold.

By default, folds aren't stored in the file you're editing -- you must
save a "session." If I'm editing a file with folds, I don't want to
bother storing and restoring a session. The solution is to set the
`foldmethod' option:

    set foldmethod=marker

There are a few other foldmethods for folding automatically based on
indent or syntax (see ":help foldmethod"), but I prefer to do them
manually.

The next step is to make your fold markers compatible with whatever
language you're coding in. The fold markers use Vim's `commentstring'
option which defaults to /*%s*/, where %s is either "{{{" or "}}}"
depending on where the fold should start or end. I prefer a more
Perl-compatible commentstring:

    set commentstring=\ \ #%s

With these options set, upon opening a document with folded text, you
might see something like:

    +--  3 lines: print "look, mom!\n";  -----------------------------

While all your non-folding friends would open the file and see:

    print "look, mom!\n";  #{{{
    print "folded text!\n";
    print "wizzow!\n";  #}}}

As for keystrokes, they may all be found by using ":help zf". There are
only four that I really use: `zf', `zo', `zc' and `zd' to create, close,
open and remove folds, respectively. (The latter three are used while
the cursor is in/over a fold and `zf' uses motion, so you might first
select a few lines with `V' and then hit `zf'.)

More information can be found by using ":help fold.txt".

-- 
Ian Langworth
CCS Project Guerilla



More information about the Tipz mailing list