file
and adding these items in it. That's not so easy to do. But once it's done,
all folding happens automatically.
Here we'll assume you are using an existing syntax file. Then there is
nothing more to explain. You can open and close folds as explained above.
The folds will be created and deleted automatically when you edit the file.
More about folding by syntax in the reference manual: |fold-syntax|
==============================================================================
*28.8* Folding by expression
This is similar to folding by indent, but instead of using the indent of a
line a user function is called to compute the fold level of a line. You can
use this for text where something in the text indicates which lines belong
together. An example is an e-mail message where the quoted text is indicated
by a ">" before the line. To fold these quotes use this: >
:set foldmethod=expr
:set foldexpr=strlen(substitute(substitute(getline(v:lnum),'\\s','',\"g\"),'[^>].*','',''))
You can try it out on this text:
> quoted text he wrote
> quoted text he wrote
> > double quoted text I wrote
> > double quoted text I wrote
Explanation for the 'foldexpr' used in the example (inside out):
getline(v:lnum) gets the current line
substitute(...,'\\s','','g') removes all white space from the line
substitute(...,'[^>].*','','') removes everything after leading '>'s
strlen(...) counts the length of the string, which
is the number of '>'s found
Note that a backslash must be inserted before every space, double quote and
backslash for the ":set" command. If this confuses you, do >
:set foldexpr
to check the actual resulting value. To correct a complicated expression, use
the command-line completion: >
:set foldexpr=<Tab>
Where <Tab> is a real Tab. Vim will fill in the previous value, which you can
then edit.
When the expression gets more complicated you should put it in a function and
set 'foldexpr' to call that function.
More about folding by expression in the reference manual: |fold-expr|
==============================================================================
*28.9* Folding unchanged lines
This is useful when you set the 'diff' option in the same window. The