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