blob: d80a1022f58c6aee1ec7d3d7bdc8e739e1525eae [file] [log] [blame]
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001*fold.txt* For Vim version 7.0aa. Last change: 2005 Mar 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Folding *Folding* *folding*
8
9You can find an introduction on folding in chapter 28 of the user manual.
10|usr_28.txt|
11
121. Fold methods |fold-methods|
132. Fold commands |fold-commands|
143. Fold options |fold-options|
154. Behavior of folds |fold-behavior|
16
17{Vi has no Folding}
18{not available when compiled without the +folding feature}
19
20==============================================================================
211. Fold methods *fold-methods*
22
23The folding method can be set with the 'foldmethod' option.
24
25When setting 'foldmethod' to a value other than "manual", all folds are
26deleted and new ones created. Switching to the "manual" method doesn't remove
27the existing folds. This can be used to first define the folds automatically
28and then change them manually.
29
30There are six methods to select folds:
31 manual manually define folds
32 indent more indent means a higher fold level
33 expr specify an expression to define folds
34 syntax folds defined by syntax highlighting
35 diff folds for unchanged text
36 marker folds defined by markers in the text
37
38
39MANUAL *fold-manual*
40
41Use commands to manually define the fold regions. This can also be used by a
42script that parses text to find folds.
43
44The level of a fold is only defined by its nesting. To increase the fold
45level of a fold for a range of lines, define a fold inside it that has the
46same lines.
47
48The manual folds are lost when you abandon the file. To save the folds use
49the |:mkview| command. The view can be restored later with |:loadview|.
50
51
52INDENT *fold-indent*
53
54The folds are automatically defined by the indent of the lines.
55
56The foldlevel is computed from the indent of the line, divided by the
57'shiftwidth' (rounded down). A sequence of lines with the same or higher fold
58level form a fold, with the lines with a higher level forming a nested fold.
59
60The nesting of folds is limited with 'foldnestmax'.
61
62Some lines are ignored and get the fold level of the line above or below it,
63whatever is the lowest. These are empty or white lines and lines starting
64with a character in 'foldignore'. White space is skipped before checking for
65characters in 'foldignore'. For C use "#" to ignore preprocessor lines.
66
67When you want to ignore lines in another way, use the 'expr' method. The
68|indent()| function can be used in 'foldexpr' to get the indent of a line.
69
70
71EXPR *fold-expr*
72
73The folds are automatically defined by their foldlevel, like with the "indent"
74method. The value of the 'foldexpr' option is evaluated to get the foldlevel
75of a line. Examples:
76This will create a fold for all consecutive lines that start with a Tab: >
77 :set foldexpr=getline(v:lnum)[0]==\"\\t\"
78This will call a function to compute the fold level: >
79 :set foldexpr=MyFoldLevel(v:lnum)
80This will make a fold out of paragraphs separated by blank lines: >
81 :set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
82this does the same: >
83 :set foldexpr=getline(v:lnum-1)=~'^\\s*$'&&getline(v:lnum)=~'\\S'?'>1':1
84
85Note that backslashes must be used to escape characters that ":set" handles
86differently (space, backslash, double quote, etc., see |option-backslash|).
87
88These are the conditions with which the expression is evaluated:
89- The current buffer and window are set for the line.
90- The variable "v:lnum" is set to the line number.
91- The result is used for the fold level in this way:
92 value meaning ~
93 0 the line is not in a fold
94 1, 2, .. the line is in a fold with this level
95 -1 the fold level is undefined, use the fold level of a
96 line before or after this line, whichever is the
97 lowest.
98 "=" use fold level from the previous line
99 "a1", "a2", .. add one, two, .. to the fold level of the previous
100 line
101 "s1", "s2", .. subtract one, two, .. from the fold level of the
102 previous line
103 "<1", "<2", .. a fold with this level ends at this line
104 ">1", ">2", .. a fold with this level starts at this line
105
106It is not required to mark the start (end) of a fold with ">1" ("<1"), a fold
107will also start (end) when the fold level is higher (lower) than the fold
108level of the previous line.
109
110There must be no side effects from the expression. The text in the buffer,
111cursor position, the search patterns, options etc. must not be changed.
112
113If there is some error in the expression, or the resulting value isn't
114recognized, there is no error message and the fold level will be zero.
115For debugging the 'debug' option can be set to "msg", the error messages will
116be visible then.
117
118Note: Since the expression has to be evaluated for every line, this fold
119method can be very slow!
120
121Try to avoid the "=", "a" and "s" return values, since Vim often has to search
122backwards for a line for which the fold level is defined. This can be slow.
123
124|foldlevel()| can be useful to compute a fold level relative to a previous
125fold level. But note that foldlevel() may return -1 if the level is not known
126yet. And it returns the level at the start of the line, while a fold might
127end in that line.
128
129
130SYNTAX *fold-syntax*
131
132A fold is defined by syntax items that have the "fold" argument. |:syn-fold|
133
134The fold level is defined by nesting folds. The nesting of folds is limited
135with 'foldnestmax'.
136
137Be careful to specify proper syntax syncing. If this is not done right, folds
138may differ from the displayed highlighting. This is especially relevant when
139using patterns that match more than one line. In case of doubt, try using
140brute-force syncing: >
141 :syn sync fromstart
142
143
144DIFF *fold-diff*
145
146The folds are automatically defined for text that is not part of a change or
147close to a change.
148
149This method only works properly when the 'diff' option is set for the current
150window and changes are being displayed. Otherwise the whole buffer will be
151one big fold.
152
153The 'diffopt' option can be used to specify the context. That is, the number
154of lines between the fold and a change that are not included in the fold. For
155example, to use a context of 8 lines: >
156 :set diffopt=filler,context:8
157The default context is six lines.
158
159When 'scrollbind' is also set, Vim will attempt to keep the same folds open in
160other diff windows, so that the same text is visible.
161
162
163MARKER *fold-marker*
164
165Markers in the text tell where folds start and end. This allows you to
166precisely specify the folds. This will allow deleting and putting a fold,
167without the risk of including the wrong lines. The 'foldtext' option is
168normally set such that the text before the marker shows up in the folded line.
169This makes it possible to give a name to the fold.
170
171Markers can have a level included, or can use matching pairs. Including a
172level is easier, you don't have to add end markers and avoid problems with
173non-matching marker pairs. Example: >
174 /* global variables {{{1 */
175 int varA, varB;
176
177 /* functions {{{1 */
178 /* funcA() {{{2 */
179 void funcA() {}
180
181 /* funcB() {{{2 */
182 void funcB() {}
183
184A fold starts at a "{{{" marker. The following number specifies the fold
185level. What happens depends on the difference between the current fold level
186and the level given by the marker:
1871. If a marker with the same fold level is encountered, the previous fold
188 ends and another fold with the same level starts.
1892. If a marker with a higher fold level is found, a nested fold is started.
1903. if a marker with a lower fold level is found, all folds up to and including
191 this level end and a fold with the specified level starts.
192
Bram Moolenaar009b2592004-10-24 19:18:58 +0000193The number indicates the fold level. A zero cannot be used (a marker with
194level zero is ignored). You can use "}}}" with a digit to indicate the level
195of the fold that ends. The fold level of the following line will be one less
196than the indicated level. Note that Vim doesn't look back to the level of the
197matching marker (that would take too much time). Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
199 {{{1
200 fold level here is 1
201 {{{3
202 fold level here is 3
203 }}}3
204 fold level here is 2
205
206You can also use matching pairs of "{{{" and "}}}" markers to define folds.
207Each "{{{" increases the fold level by one, each "}}}" decreases the fold
208level by one. Be careful to keep the markers matching! Example: >
209
210 {{{
211 fold level here is 1
212 {{{
213 fold level here is 2
214 }}}
215 fold level here is 1
216
217You can mix using markers with a number and without a number. A useful way of
218doing this is to use numbered markers for large folds, and unnumbered markers
219locally in a function. For example use level one folds for the sections of
220your file like "structure definitions", "local variables" and "functions".
221Use level 2 markers for each definition and function, Use unnumbered markers
222inside functions. When you make changes in a function to split up folds, you
223don't have to renumber the markers.
224
225The markers can be set with the 'foldmarker' option. It is recommended to
226keep this at the default value of "{{{,}}}", so that files can be exchanged
227between Vim users. Only change it when it is required for the file (e.g., it
228contains markers from another folding editor, or the default markers cause
229trouble for the language of the file).
230
231 *fold-create-marker*
232"zf" can be used to create a fold defined by markers. Vim will insert the
233markers for you. Vim will append the start and end marker, as specified with
234'foldmarker'. The markers are appended to the end of the line.
235'commentstring' is used if it isn't empty.
236This does not work properly when:
237- The line already contains a marker with a level number. Vim then doesn't
238 know what to do.
239- Folds nearby use a level number in their marker which gets in the way.
240- The line is inside a comment, 'commentstring' isn't empty and nested
241 comments don't work. For example with C: adding /* {{{ */ inside a comment
242 will truncate the existing comment. Either put the marker before or after
243 the comment, or add the marker manually.
244Generally it's not a good idea to let Vim create markers when you already have
245markers with a level number.
246
247 *fold-delete-marker*
248"zd" can be used to delete a fold defined by markers. Vim will delete the
249markers for you. Vim will search for the start and end markers, as specified
250with 'foldmarker', at the start and end of the fold. When the text around the
251marker matches with 'commentstring', that text is deleted as well.
252This does not work properly when:
253- A line contains more than one marker and one of them specifies a level.
254 Only the first one is removed, without checking if this will have the
255 desired effect of deleting the fold.
256- The marker contains a level number and is used to start or end several folds
257 at the same time.
258
259==============================================================================
2602. Fold commands *fold-commands* *E490*
261
262All folding commands start with "z". Hint: the "z" looks like a folded piece
263of paper, if you look at it from the side.
264
265
266CREATING AND DELETING FOLDS ~
267 *zf* *E350*
268zf{motion} or
269{Visual}zf Operator to create a fold.
270 This only works when 'foldmethod' is "manual" or "marker".
271 The new fold will be closed for the "manual" method.
272 'foldenable' will be set.
273 Also see |fold-create-marker|.
274
275 *zF*
276zF Create a fold for N lines. Works like "zf".
277
278:{range}fo[ld] *:fold* *:fo*
279 Create a fold for the lines in {range}. Works like "zf".
280
281 *zd* *E351*
Bram Moolenaar81695252004-12-29 20:58:21 +0000282zd Delete one fold at the cursor. When the cursor is on a folded
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 line, that fold is deleted. Nested folds are moved one level
284 up. In Visual mode all folds (partially) in the selected area
285 are deleted. Careful: This easily deletes more folds than you
286 expect and there is no undo.
287 This only works when 'foldmethod' is "manual" or "marker".
288 Also see |fold-delete-marker|.
289
290 *zD*
291zD Delete folds recursively at the cursor. In Visual mode all
292 folds (partially) in the selected area and all nested folds in
293 them are deleted.
294 This only works when 'foldmethod' is "manual" or "marker".
295 Also see |fold-delete-marker|.
296
297 *zE* *E352*
298zE Eliminate all folds in the window.
299 This only works when 'foldmethod' is "manual" or "marker".
300 Also see |fold-delete-marker|.
301
302
303OPENING AND CLOSING FOLDS ~
304
305A fold smaller than 'foldminlines' will always be displayed like it was open.
306Therefore the commands below may work differently on small folds.
307
308 *zo*
309zo Open one fold under the cursor. When a count is given, that
310 many folds deep will be opened. In Visual mode one level of
311 folds is opened for all lines in the selected area.
312
313 *zO*
314zO Open all folds under the cursor recursively. Folds that don't
315 contain the cursor line are unchanged.
316 In Visual mode it opens all folds that are in the selected
317 area, also those that are only partly selected.
318
319 *zc*
320zc Close one fold under the cursor. When a count is given, that
321 many folds deep are closed. In Visual mode one level of folds
322 is closed for all lines in the selected area.
323 'foldenable' will be set.
324
325 *zC*
326zC Close all folds under the cursor recursively. Folds that
327 don't contain the cursor line are unchanged.
328 In Visual mode it closes all folds that are in the selected
329 area, also those that are only partly selected.
330 'foldenable' will be set.
331
332 *za*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000333za When on a closed fold: open it. When folds are nested, you
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 may have to use "za" several times. When a count is given,
335 that many closed folds are opened.
336 When on an open fold: close it and set 'foldenable'. This
337 will only close one level, since using "za" again will open
338 the fold. When a count is given that many folds will be
339 closed (that's not the same as repeating "za" that many
340 times).
341
342 *zA*
343zA When on a closed fold: open it recursively.
344 When on an open fold: close it recursively and set
345 'foldenable'.
346
347 *zv*
348zv View cursor line: Open just enough folds to make the line in
349 which the cursor is located not folded.
350
351 *zx*
352zx Update folds: Undo manually opened and closed folds: re-apply
353 'foldlevel', then do "zv": View cursor line.
354
355 *zX*
356zX Undo manually opened and closed folds: re-apply 'foldlevel'.
357
358 *zm*
359zm Fold more: Subtract one from 'foldlevel'. If 'foldlevel' was
360 already zero nothing happens.
361 'foldenable' will be set.
362
363 *zM*
364zM Close all folds: set 'foldlevel' to 0.
365 'foldenable' will be set.
366
367 *zr*
368zr Reduce folding: Add one to 'foldlevel'.
369
370 *zR*
371zR Open all folds. This sets 'foldlevel' to highest fold level.
372
373 *:foldo* *:foldopen*
374:{range}foldo[pen][!]
375 Open folds in {range}. When [!] is added all folds are
376 opened. Useful to see all the text in {range}. Without [!]
377 one level of folds is opened.
378
379 *:foldc* *:foldclose*
380:{range}foldc[lose][!]
381 Close folds in {range}. When [!] is added all folds are
382 closed. Useful to hide all the text in {range}. Without [!]
383 one level of folds is closed.
384
385 *zn*
386zn Fold none: reset 'foldenable'. All folds will be open.
387
388 *zN*
389zN Fold normal: set 'foldenable'. All folds will be as they
390 were before.
391
392 *zi*
393zi Invert 'foldenable'.
394
395
396MOVING OVER FOLDS ~
397 *[z*
398[z Move to the start of the current open fold. If already at the
399 start, move to the start of the fold that contains it. If
400 there is no containing fold, the command fails.
401 When a count is used, repeats the command N times.
402
403 *]z*
404]z Move to the end of the current open fold. If already at the
405 end, move to the end of the fold that contains it. If there
406 is no containing fold, the command fails.
407 When a count is used, repeats the command N times.
408
409 *zj*
410zj Move downwards to the start of the next fold. A closed fold
411 is counted as one fold.
412 When a count is used, repeats the command N times.
413 This command can be used after an |operator|.
414
415 *zk*
416zk Move upwards to the end of the previous fold. A closed fold
417 is counted as one fold.
418 When a count is used, repeats the command N times.
419 This command can be used after an |operator|.
420
421
422EXECUTING COMMANDS ON FOLDS ~
423
424:[range]foldd[oopen] {cmd} *:foldd* *:folddoopen*
425 Execute {cmd} on all lines that are not in a closed fold.
426 When [range] is given, only these lines are used.
427 Each time {cmd} is executed the cursor is positioned on the
428 line it is executed for.
429 This works like the ":global" command: First all lines that
430 are not in a closed fold are marked. Then the {cmd} is
431 executed for all marked lines. Thus when {cmd} changes the
432 folds, this has no influence on where it is executed (except
433 when lines are deleted, of course).
434 Example: >
435 :folddoopen s/end/loop_end/ge
436< Note the use of the "e" flag to avoid getting an error message
437 where "end" doesn't match.
438
439:[range]folddoc[losed] {cmd} *:folddoc* *:folddoclosed*
440 Execute {cmd} on all lines that are in a closed fold.
441 Otherwise like ":folddoopen".
442
443==============================================================================
4443. Fold options *fold-options*
445
446COLORS *fold-colors*
447
448The colors of a closed fold are set with the Folded group |hl-Folded|. The
449colors of the fold column are set with the FoldColumn group |hl-FoldColumn|.
450Example to set the colors: >
451
452 :highlight Folded guibg=grey guifg=blue
453 :highlight FoldColumn guibg=darkgrey guifg=white
454
455
456FOLDLEVEL *fold-foldlevel*
457
458'foldlevel' is a number option: The higher the more folded regions are open.
459When 'foldlevel' is 0, all folds are closed.
Bram Moolenaar81695252004-12-29 20:58:21 +0000460When 'foldlevel' is positive, some folds are closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461When 'foldlevel' is very high, all folds are open.
462'foldlevel' is applied when it is changed. After that manually folds can be
463opened and closed.
464When increased, folds above the new level are opened. No manually opened
465folds will be closed.
466When decreased, folds above the new level are closed. No manually closed
467folds will be opened.
468
469
470FOLDTEXT *fold-foldtext*
471
472'foldtext' is a string option that specifies an expression. This expression
473is evaluated to obtain the text displayed for a closed fold. Example: >
474
475 :set foldtext=v:folddashes.substitute(getline(v:foldstart),'/\\*\\\|\\*/\\\|{{{\\d\\=','','g')
476
477This shows the first line of the fold, with "/*", "*/" and "{{{" removed.
478Note the use of backslashes to avoid some characters to be interpreted by the
479":set" command. It's simpler to define a function and call that: >
480
481 :set foldtext=MyFoldText()
482 :function MyFoldText()
483 : let line = getline(v:foldstart)
484 : let sub = substitute(line, '/\*\|\*/\|{{{\d\=', '', 'g')
485 : return v:folddashes . sub
486 :endfunction
487
488Evaluating 'foldtext' is done in the |sandbox|. The current window is set to
489the window that displays the line. Errors are ignored.
490
491The default value is |foldtext()|. This returns a reasonable text for most
492types of folding. If you don't like it, you can specify your own 'foldtext'
493expression. It can use these special Vim variables:
494 v:foldstart line number of first line in the fold
495 v:foldend line number of last line in the fold
496 v:folddashes a string that contains dashes to represent the
497 foldlevel.
498 v:foldlevel the foldlevel of the fold
499
500In the result a TAB is replaced with a space and unprintable characters are
501made into printable characters.
502
503The resulting line is truncated to fit in the window, it never wraps.
504When there is room after the text, it is filled with the character specified
505by 'fillchars'.
506
507Note that backslashes need to be used for characters that the ":set" command
508handles differently: Space, backslash and double-quote. |option-backslash|
509
510
511FOLDCOLUMN *fold-foldcolumn*
512
513'foldcolumn' is a number, which sets the width for a column on the side of the
514window to indicate folds. When it is zero, there is no foldcolumn. A normal
515value is 4 or 5. The minimal useful value is 2. The maximum is 12.
516
517An open fold is indicated with a column that has a '-' at the top and '|'
518characters below it. This column stops where the open fold stops. When folds
519nest, the nested fold is one character right of the fold it's contained in.
520
521A closed fold is indicated with a '+'.
522
523Where the fold column is too narrow to display all nested folds, digits are
524shown to indicate the nesting level.
525
526The mouse can also be used to open and close folds by clicking in the
527fold column:
528- Click on a '+' to open the closed fold at this row.
529- Click on any other non-blank character to close the open fold at this row.
530
531
532OTHER OPTIONS
533
534'foldenable' 'fen': Open all folds while not set.
535'foldexpr' 'fde': Expression used for "expr" folding.
536'foldignore' 'fdi': Characters used for "indent" folding.
537'foldmarker' 'fmr': Defined markers used for "marker" folding.
538'foldmethod' 'fdm': Name of the current folding method.
539'foldminlines' 'fml': Minimum number of screen lines for a fold to be
540 displayed closed.
541'foldnestmax' 'fdn': Maximum nesting for "indent" and "syntax" folding.
542'foldopen' 'fdo': Which kinds of commands open closed folds.
543'foldclose' 'fcl': When the folds not under the cursor are closed.
544
545==============================================================================
5464. Behavior of folds *fold-behavior*
547
548When moving the cursor upwards or downwards and when scrolling, the cursor
549will move to the first line of a sequence of folded lines. When the cursor is
550already on a folded line, it moves to the next unfolded line or the next
551closed fold.
552
553While the cursor is on folded lines, the cursor is always displayed in the
554first column. The ruler does show the actual cursor position, but since the
555line is folded, it cannot be displayed there.
556
557Many movement commands handle a sequence of folded lines like an empty line.
558For example, the "w" command stops once in the first column.
559
560When in Insert mode, the cursor line is never folded. That allows you to see
561what you type!
562
563When using an operator, a closed fold is included as a whole. Thus "dl"
564deletes the whole closed fold under the cursor.
565
566For Ex commands the range is adjusted to always start at the first line of a
Bram Moolenaared203462004-06-16 11:19:22 +0000567closed fold and end at the last line of a closed fold. Thus this command: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 :s/foo/bar/g
569when used with the cursor on a closed fold, will replace "foo" with "bar" in
570all lines of the fold.
571This does not happen for |:folddoopen| and |:folddoclosed|.
572
573When editing a buffer that has been edited before, the last used folding
574settings are used again. For manual folding the defined folds are restored.
575For all folding methods the manually opened and closed folds are restored.
576If this buffer has been edited in this window, the values from back then are
577used. Otherwise the values from the window where the buffer was edited last
578are used.
579
580==============================================================================
581 vim:tw=78:ts=8:ft=help:norl: