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