blob: cf9208936f8e461948b8c7f5b110df4886590d3d [file] [log] [blame]
Konfekt3920bb42024-12-16 21:10:45 +01001*fold.txt* For Vim version 9.1. Last change: 2024 Dec 16
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
Bram Moolenaardb84e452010-08-15 13:50:43 +020017{not available when compiled without the |+folding| feature}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018
19==============================================================================
201. Fold methods *fold-methods*
21
22The folding method can be set with the 'foldmethod' option.
23
24When setting 'foldmethod' to a value other than "manual", all folds are
25deleted and new ones created. Switching to the "manual" method doesn't remove
26the existing folds. This can be used to first define the folds automatically
27and then change them manually.
28
29There are six methods to select folds:
30 manual manually define folds
31 indent more indent means a higher fold level
32 expr specify an expression to define folds
33 syntax folds defined by syntax highlighting
34 diff folds for unchanged text
35 marker folds defined by markers in the text
36
37
38MANUAL *fold-manual*
39
40Use commands to manually define the fold regions. This can also be used by a
41script that parses text to find folds.
42
43The level of a fold is only defined by its nesting. To increase the fold
44level of a fold for a range of lines, define a fold inside it that has the
45same lines.
46
47The manual folds are lost when you abandon the file. To save the folds use
48the |:mkview| command. The view can be restored later with |:loadview|.
49
50
51INDENT *fold-indent*
52
53The folds are automatically defined by the indent of the lines.
54
55The foldlevel is computed from the indent of the line, divided by the
56'shiftwidth' (rounded down). A sequence of lines with the same or higher fold
57level form a fold, with the lines with a higher level forming a nested fold.
58
59The nesting of folds is limited with 'foldnestmax'.
60
61Some lines are ignored and get the fold level of the line above or below it,
Bram Moolenaar446beb42011-05-10 17:18:44 +020062whichever is lower. These are empty or white lines and lines starting
Bram Moolenaar071d4272004-06-13 20:20:40 +000063with a character in 'foldignore'. White space is skipped before checking for
64characters in 'foldignore'. For C use "#" to ignore preprocessor lines.
65
Bram Moolenaar214641f2017-03-05 17:04:09 +010066When you want to ignore lines in another way, use the "expr" method. The
Bram Moolenaar071d4272004-06-13 20:20:40 +000067|indent()| function can be used in 'foldexpr' to get the indent of a line.
68
69
70EXPR *fold-expr*
71
72The folds are automatically defined by their foldlevel, like with the "indent"
73method. The value of the 'foldexpr' option is evaluated to get the foldlevel
74of a line. Examples:
Bram Moolenaar666771a2007-05-12 14:03:30 +000075This will create a fold for all consecutive lines that start with a tab: >
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 :set foldexpr=getline(v:lnum)[0]==\"\\t\"
Bram Moolenaar071d4272004-06-13 20:20:40 +000077This will make a fold out of paragraphs separated by blank lines: >
78 :set foldexpr=getline(v:lnum)=~'^\\s*$'&&getline(v:lnum+1)=~'\\S'?'<1':1
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010079This does the same: >
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 :set foldexpr=getline(v:lnum-1)=~'^\\s*$'&&getline(v:lnum)=~'\\S'?'>1':1
81
82Note that backslashes must be used to escape characters that ":set" handles
83differently (space, backslash, double quote, etc., see |option-backslash|).
84
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +010085The most efficient is to call a compiled function without arguments: >
86 :set foldexpr=MyFoldLevel()
87The function must use v:lnum. See |expr-option-function|.
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089These are the conditions with which the expression is evaluated:
Konfekt3920bb42024-12-16 21:10:45 +010090
Bram Moolenaar071d4272004-06-13 20:20:40 +000091- The current buffer and window are set for the line.
92- The variable "v:lnum" is set to the line number.
Konfekt3920bb42024-12-16 21:10:45 +010093
94The result of foldexpr then determines the fold level as follows:
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 value meaning ~
96 0 the line is not in a fold
97 1, 2, .. the line is in a fold with this level
98 -1 the fold level is undefined, use the fold level of a
99 line before or after this line, whichever is the
100 lowest.
101 "=" use fold level from the previous line
102 "a1", "a2", .. add one, two, .. to the fold level of the previous
Bram Moolenaard042dc82015-11-24 19:18:36 +0100103 line, use the result for the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 "s1", "s2", .. subtract one, two, .. from the fold level of the
Bram Moolenaard042dc82015-11-24 19:18:36 +0100105 previous line, use the result for the next line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 "<1", "<2", .. a fold with this level ends at this line
107 ">1", ">2", .. a fold with this level starts at this line
108
Konfekt3920bb42024-12-16 21:10:45 +0100109The result values "=", "s" and "a" are more expensive, please see |fold-expr-slow|.
110
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111It is not required to mark the start (end) of a fold with ">1" ("<1"), a fold
112will also start (end) when the fold level is higher (lower) than the fold
113level of the previous line.
114
115There must be no side effects from the expression. The text in the buffer,
116cursor position, the search patterns, options etc. must not be changed.
Bram Moolenaare2f98b92006-03-29 21:18:24 +0000117You can change and restore them if you are careful.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119If there is some error in the expression, or the resulting value isn't
120recognized, there is no error message and the fold level will be zero.
121For debugging the 'debug' option can be set to "msg", the error messages will
122be visible then.
123
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000124If the 'foldexpr' expression starts with s: or |<SID>|, then it is replaced
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +0000125with the script ID (|local-function|). Examples: >
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000126 set foldexpr=s:MyFoldExpr()
127 set foldexpr=<SID>SomeFoldExpr()
128<
Bram Moolenaard042dc82015-11-24 19:18:36 +0100129An example of using "a1" and "s1": For a multi-line C comment, a line
130containing "/*" would return "a1" to start a fold, and a line containing "*/"
131would return "s1" to end the fold after that line: >
132 if match(thisline, '/\*') >= 0
133 return 'a1'
134 elseif match(thisline, '\*/') >= 0
135 return 's1'
136 else
137 return '='
138 endif
139However, this won't work for single line comments, strings, etc.
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141|foldlevel()| can be useful to compute a fold level relative to a previous
142fold level. But note that foldlevel() may return -1 if the level is not known
143yet. And it returns the level at the start of the line, while a fold might
144end in that line.
145
Bram Moolenaar214641f2017-03-05 17:04:09 +0100146It may happen that folds are not updated properly. You can use |zx| or |zX|
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200147to force updating folds.
148
Konfekt3920bb42024-12-16 21:10:45 +0100149Minimizing Computational Cost *fold-expr-slow*
150
151Due to its computational cost, this fold method can make Vim unresponsive,
152especially when the fold level of all lines have to be initially computed.
153Afterwards, after each change, Vim restricts the computation of foldlevels
154to those lines whose fold level was affected by it (and reuses the known
155foldlevels of all the others).
156
157The fold expression should therefore strive to minimize the number of dependent
158lines needed for the computation of a given line: For example, try to avoid the
159"=", "a" and "s" return values, because these will require the evaluation of the
160fold levels on previous lines until an independent fold level is found.
161
162If this proves difficult, the next best thing could be to cache all fold levels
glepnir6a38aff2024-12-16 21:56:16 +0100163in a buffer-local variable (b:foldlevels) that is only updated on |b:changedtick|:
Konfekt3920bb42024-12-16 21:10:45 +0100164>vim
165 vim9script
166 def MyFoldFunc(): number
167 if b:lasttick == b:changedtick
168 return b:foldlevels[v:lnum - 1]
169 endif
170 b:lasttick = b:changedtick
171 b:foldlevels = []
172 # compute foldlevels ...
173 return b:foldlevels[v:lnum - 1]
174 enddef
175 set foldexpr=s:MyFoldFunc()
176<
177In above example further speedup was gained by using a precompiled Vim9script
178function without arguments (that must still use v:lnum). See |expr-option-function|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
180SYNTAX *fold-syntax*
181
182A fold is defined by syntax items that have the "fold" argument. |:syn-fold|
183
184The fold level is defined by nesting folds. The nesting of folds is limited
185with 'foldnestmax'.
186
187Be careful to specify proper syntax syncing. If this is not done right, folds
188may differ from the displayed highlighting. This is especially relevant when
189using patterns that match more than one line. In case of doubt, try using
190brute-force syncing: >
191 :syn sync fromstart
192
193
194DIFF *fold-diff*
195
196The folds are automatically defined for text that is not part of a change or
197close to a change.
198
199This method only works properly when the 'diff' option is set for the current
200window and changes are being displayed. Otherwise the whole buffer will be
201one big fold.
202
203The 'diffopt' option can be used to specify the context. That is, the number
204of lines between the fold and a change that are not included in the fold. For
205example, to use a context of 8 lines: >
206 :set diffopt=filler,context:8
207The default context is six lines.
208
209When 'scrollbind' is also set, Vim will attempt to keep the same folds open in
210other diff windows, so that the same text is visible.
211
212
213MARKER *fold-marker*
214
215Markers in the text tell where folds start and end. This allows you to
216precisely specify the folds. This will allow deleting and putting a fold,
217without the risk of including the wrong lines. The 'foldtext' option is
218normally set such that the text before the marker shows up in the folded line.
219This makes it possible to give a name to the fold.
220
221Markers can have a level included, or can use matching pairs. Including a
222level is easier, you don't have to add end markers and avoid problems with
223non-matching marker pairs. Example: >
224 /* global variables {{{1 */
225 int varA, varB;
226
227 /* functions {{{1 */
228 /* funcA() {{{2 */
229 void funcA() {}
230
231 /* funcB() {{{2 */
232 void funcB() {}
Bram Moolenaarbe4e0162023-02-02 13:59:48 +0000233< *{{{* *}}}*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234A fold starts at a "{{{" marker. The following number specifies the fold
235level. What happens depends on the difference between the current fold level
236and the level given by the marker:
2371. If a marker with the same fold level is encountered, the previous fold
238 ends and another fold with the same level starts.
2392. If a marker with a higher fold level is found, a nested fold is started.
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01002403. If a marker with a lower fold level is found, all folds up to and including
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 this level end and a fold with the specified level starts.
242
Bram Moolenaar009b2592004-10-24 19:18:58 +0000243The number indicates the fold level. A zero cannot be used (a marker with
244level zero is ignored). You can use "}}}" with a digit to indicate the level
245of the fold that ends. The fold level of the following line will be one less
246than the indicated level. Note that Vim doesn't look back to the level of the
247matching marker (that would take too much time). Example: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
249 {{{1
250 fold level here is 1
251 {{{3
252 fold level here is 3
253 }}}3
254 fold level here is 2
255
256You can also use matching pairs of "{{{" and "}}}" markers to define folds.
257Each "{{{" increases the fold level by one, each "}}}" decreases the fold
258level by one. Be careful to keep the markers matching! Example: >
259
260 {{{
261 fold level here is 1
262 {{{
263 fold level here is 2
264 }}}
265 fold level here is 1
266
267You can mix using markers with a number and without a number. A useful way of
268doing this is to use numbered markers for large folds, and unnumbered markers
269locally in a function. For example use level one folds for the sections of
270your file like "structure definitions", "local variables" and "functions".
271Use level 2 markers for each definition and function, Use unnumbered markers
272inside functions. When you make changes in a function to split up folds, you
273don't have to renumber the markers.
274
275The markers can be set with the 'foldmarker' option. It is recommended to
276keep this at the default value of "{{{,}}}", so that files can be exchanged
277between Vim users. Only change it when it is required for the file (e.g., it
278contains markers from another folding editor, or the default markers cause
279trouble for the language of the file).
280
281 *fold-create-marker*
282"zf" can be used to create a fold defined by markers. Vim will insert the
283markers for you. Vim will append the start and end marker, as specified with
284'foldmarker'. The markers are appended to the end of the line.
285'commentstring' is used if it isn't empty.
286This does not work properly when:
287- The line already contains a marker with a level number. Vim then doesn't
288 know what to do.
289- Folds nearby use a level number in their marker which gets in the way.
290- The line is inside a comment, 'commentstring' isn't empty and nested
291 comments don't work. For example with C: adding /* {{{ */ inside a comment
292 will truncate the existing comment. Either put the marker before or after
293 the comment, or add the marker manually.
294Generally it's not a good idea to let Vim create markers when you already have
295markers with a level number.
296
297 *fold-delete-marker*
298"zd" can be used to delete a fold defined by markers. Vim will delete the
299markers for you. Vim will search for the start and end markers, as specified
300with 'foldmarker', at the start and end of the fold. When the text around the
301marker matches with 'commentstring', that text is deleted as well.
302This does not work properly when:
303- A line contains more than one marker and one of them specifies a level.
304 Only the first one is removed, without checking if this will have the
305 desired effect of deleting the fold.
306- The marker contains a level number and is used to start or end several folds
307 at the same time.
308
309==============================================================================
3102. Fold commands *fold-commands* *E490*
311
312All folding commands start with "z". Hint: the "z" looks like a folded piece
313of paper, if you look at it from the side.
314
315
316CREATING AND DELETING FOLDS ~
317 *zf* *E350*
318zf{motion} or
319{Visual}zf Operator to create a fold.
320 This only works when 'foldmethod' is "manual" or "marker".
321 The new fold will be closed for the "manual" method.
322 'foldenable' will be set.
323 Also see |fold-create-marker|.
324
325 *zF*
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100326zF Create a fold for [count] lines. Works like "zf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
328:{range}fo[ld] *:fold* *:fo*
329 Create a fold for the lines in {range}. Works like "zf".
330
331 *zd* *E351*
Bram Moolenaar81695252004-12-29 20:58:21 +0000332zd Delete one fold at the cursor. When the cursor is on a folded
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 line, that fold is deleted. Nested folds are moved one level
Bram Moolenaardfb18412013-12-11 18:53:29 +0100334 up. In Visual mode one level of all folds (partially) in the
335 selected area are deleted.
336 Careful: This easily deletes more folds than you expect and
337 there is no undo for manual folding.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 This only works when 'foldmethod' is "manual" or "marker".
339 Also see |fold-delete-marker|.
340
341 *zD*
342zD Delete folds recursively at the cursor. In Visual mode all
343 folds (partially) in the selected area and all nested folds in
344 them are deleted.
345 This only works when 'foldmethod' is "manual" or "marker".
346 Also see |fold-delete-marker|.
347
348 *zE* *E352*
349zE Eliminate all folds in the window.
350 This only works when 'foldmethod' is "manual" or "marker".
351 Also see |fold-delete-marker|.
352
353
354OPENING AND CLOSING FOLDS ~
355
356A fold smaller than 'foldminlines' will always be displayed like it was open.
357Therefore the commands below may work differently on small folds.
358
359 *zo*
360zo Open one fold under the cursor. When a count is given, that
361 many folds deep will be opened. In Visual mode one level of
362 folds is opened for all lines in the selected area.
363
364 *zO*
365zO Open all folds under the cursor recursively. Folds that don't
366 contain the cursor line are unchanged.
367 In Visual mode it opens all folds that are in the selected
368 area, also those that are only partly selected.
369
370 *zc*
371zc Close one fold under the cursor. When a count is given, that
372 many folds deep are closed. In Visual mode one level of folds
373 is closed for all lines in the selected area.
374 'foldenable' will be set.
375
376 *zC*
377zC Close all folds under the cursor recursively. Folds that
378 don't contain the cursor line are unchanged.
379 In Visual mode it closes all folds that are in the selected
380 area, also those that are only partly selected.
381 'foldenable' will be set.
382
383 *za*
Bram Moolenaar71badf92023-04-22 22:40:14 +0100384za Summary: Toggle the fold under the cursor.
385 When on a closed fold: open it. When folds are nested, you
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 may have to use "za" several times. When a count is given,
387 that many closed folds are opened.
388 When on an open fold: close it and set 'foldenable'. This
389 will only close one level, since using "za" again will open
390 the fold. When a count is given that many folds will be
391 closed (that's not the same as repeating "za" that many
392 times).
393
394 *zA*
395zA When on a closed fold: open it recursively.
396 When on an open fold: close it recursively and set
397 'foldenable'.
398
399 *zv*
400zv View cursor line: Open just enough folds to make the line in
401 which the cursor is located not folded.
402
403 *zx*
404zx Update folds: Undo manually opened and closed folds: re-apply
405 'foldlevel', then do "zv": View cursor line.
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200406 Also forces recomputing folds. This is useful when using
407 'foldexpr' and the buffer is changed in a way that results in
408 folds not to be updated properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
410 *zX*
411zX Undo manually opened and closed folds: re-apply 'foldlevel'.
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200412 Also forces recomputing folds, like |zx|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
414 *zm*
Bram Moolenaar7d2757a2015-03-31 17:46:22 +0200415zm Fold more: Subtract |v:count1| from 'foldlevel'. If 'foldlevel' was
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 already zero nothing happens.
417 'foldenable' will be set.
418
419 *zM*
420zM Close all folds: set 'foldlevel' to 0.
421 'foldenable' will be set.
422
423 *zr*
Bram Moolenaar7d2757a2015-03-31 17:46:22 +0200424zr Reduce folding: Add |v:count1| to 'foldlevel'.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425
426 *zR*
427zR Open all folds. This sets 'foldlevel' to highest fold level.
428
429 *:foldo* *:foldopen*
430:{range}foldo[pen][!]
431 Open folds in {range}. When [!] is added all folds are
432 opened. Useful to see all the text in {range}. Without [!]
433 one level of folds is opened.
434
435 *:foldc* *:foldclose*
436:{range}foldc[lose][!]
437 Close folds in {range}. When [!] is added all folds are
438 closed. Useful to hide all the text in {range}. Without [!]
439 one level of folds is closed.
440
441 *zn*
442zn Fold none: reset 'foldenable'. All folds will be open.
443
444 *zN*
445zN Fold normal: set 'foldenable'. All folds will be as they
446 were before.
447
448 *zi*
449zi Invert 'foldenable'.
450
451
452MOVING OVER FOLDS ~
453 *[z*
454[z Move to the start of the current open fold. If already at the
455 start, move to the start of the fold that contains it. If
456 there is no containing fold, the command fails.
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100457 When a count is used, repeats the command [count] times.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458
459 *]z*
460]z Move to the end of the current open fold. If already at the
461 end, move to the end of the fold that contains it. If there
462 is no containing fold, the command fails.
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100463 When a count is used, repeats the command [count] times.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
465 *zj*
466zj Move downwards to the start of the next fold. A closed fold
467 is counted as one fold.
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100468 When a count is used, repeats the command [count] times.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 This command can be used after an |operator|.
470
471 *zk*
472zk Move upwards to the end of the previous fold. A closed fold
473 is counted as one fold.
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100474 When a count is used, repeats the command [count] times.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 This command can be used after an |operator|.
476
477
478EXECUTING COMMANDS ON FOLDS ~
479
Bram Moolenaar61da1bf2019-06-06 12:14:49 +0200480:[range]foldd[oopen] {cmd} *:foldd* *:folddo* *:folddoopen*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 Execute {cmd} on all lines that are not in a closed fold.
482 When [range] is given, only these lines are used.
483 Each time {cmd} is executed the cursor is positioned on the
484 line it is executed for.
485 This works like the ":global" command: First all lines that
486 are not in a closed fold are marked. Then the {cmd} is
487 executed for all marked lines. Thus when {cmd} changes the
488 folds, this has no influence on where it is executed (except
489 when lines are deleted, of course).
490 Example: >
491 :folddoopen s/end/loop_end/ge
492< Note the use of the "e" flag to avoid getting an error message
493 where "end" doesn't match.
494
495:[range]folddoc[losed] {cmd} *:folddoc* *:folddoclosed*
496 Execute {cmd} on all lines that are in a closed fold.
497 Otherwise like ":folddoopen".
498
499==============================================================================
5003. Fold options *fold-options*
501
502COLORS *fold-colors*
503
504The colors of a closed fold are set with the Folded group |hl-Folded|. The
505colors of the fold column are set with the FoldColumn group |hl-FoldColumn|.
506Example to set the colors: >
507
508 :highlight Folded guibg=grey guifg=blue
509 :highlight FoldColumn guibg=darkgrey guifg=white
510
511
512FOLDLEVEL *fold-foldlevel*
513
514'foldlevel' is a number option: The higher the more folded regions are open.
515When 'foldlevel' is 0, all folds are closed.
Bram Moolenaar81695252004-12-29 20:58:21 +0000516When 'foldlevel' is positive, some folds are closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517When 'foldlevel' is very high, all folds are open.
518'foldlevel' is applied when it is changed. After that manually folds can be
519opened and closed.
520When increased, folds above the new level are opened. No manually opened
521folds will be closed.
522When decreased, folds above the new level are closed. No manually closed
523folds will be opened.
524
525
526FOLDTEXT *fold-foldtext*
527
528'foldtext' is a string option that specifies an expression. This expression
529is evaluated to obtain the text displayed for a closed fold. Example: >
530
531 :set foldtext=v:folddashes.substitute(getline(v:foldstart),'/\\*\\\|\\*/\\\|{{{\\d\\=','','g')
532
533This shows the first line of the fold, with "/*", "*/" and "{{{" removed.
534Note the use of backslashes to avoid some characters to be interpreted by the
Bram Moolenaarf269eab2022-10-03 18:04:35 +0100535":set" command. It is much simpler to define a function and call it: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537 :set foldtext=MyFoldText()
538 :function MyFoldText()
539 : let line = getline(v:foldstart)
540 : let sub = substitute(line, '/\*\|\*/\|{{{\d\=', '', 'g')
Bram Moolenaarc51cf032022-02-26 12:25:45 +0000541 : return v:folddashes .. sub
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 :endfunction
543
Bram Moolenaarf269eab2022-10-03 18:04:35 +0100544The advantage of using a function call without arguments is that it is faster,
545see |expr-option-function|.
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547Evaluating 'foldtext' is done in the |sandbox|. The current window is set to
Bram Moolenaar6f4754b2022-01-23 12:07:04 +0000548the window that displays the line. The context is set to the script where the
549option was last set.
550
551Errors are ignored. For debugging set the 'debug' option to "throw".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552
553The default value is |foldtext()|. This returns a reasonable text for most
554types of folding. If you don't like it, you can specify your own 'foldtext'
555expression. It can use these special Vim variables:
556 v:foldstart line number of first line in the fold
557 v:foldend line number of last line in the fold
558 v:folddashes a string that contains dashes to represent the
559 foldlevel.
560 v:foldlevel the foldlevel of the fold
561
562In the result a TAB is replaced with a space and unprintable characters are
563made into printable characters.
564
565The resulting line is truncated to fit in the window, it never wraps.
566When there is room after the text, it is filled with the character specified
567by 'fillchars'.
568
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +0000569If the 'foldtext' expression starts with s: or |<SID>|, then it is replaced
570with the script ID (|local-function|). Examples: >
571 set foldtext=s:MyFoldText()
572 set foldtext=<SID>SomeFoldText()
573<
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574Note that backslashes need to be used for characters that the ":set" command
575handles differently: Space, backslash and double-quote. |option-backslash|
576
577
578FOLDCOLUMN *fold-foldcolumn*
579
580'foldcolumn' is a number, which sets the width for a column on the side of the
581window to indicate folds. When it is zero, there is no foldcolumn. A normal
Bram Moolenaar578b49e2005-09-10 19:22:57 +0000582value is 4 or 5. The minimal useful value is 2, although 1 still provides
583some information. The maximum is 12.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585An open fold is indicated with a column that has a '-' at the top and '|'
586characters below it. This column stops where the open fold stops. When folds
587nest, the nested fold is one character right of the fold it's contained in.
588
589A closed fold is indicated with a '+'.
590
Bram Moolenaar53f7fcc2021-07-28 20:10:16 +0200591These characters can be changed with the 'fillchars' option.
592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593Where the fold column is too narrow to display all nested folds, digits are
594shown to indicate the nesting level.
595
596The mouse can also be used to open and close folds by clicking in the
597fold column:
598- Click on a '+' to open the closed fold at this row.
599- Click on any other non-blank character to close the open fold at this row.
600
601
602OTHER OPTIONS
603
604'foldenable' 'fen': Open all folds while not set.
605'foldexpr' 'fde': Expression used for "expr" folding.
606'foldignore' 'fdi': Characters used for "indent" folding.
607'foldmarker' 'fmr': Defined markers used for "marker" folding.
608'foldmethod' 'fdm': Name of the current folding method.
609'foldminlines' 'fml': Minimum number of screen lines for a fold to be
610 displayed closed.
611'foldnestmax' 'fdn': Maximum nesting for "indent" and "syntax" folding.
612'foldopen' 'fdo': Which kinds of commands open closed folds.
613'foldclose' 'fcl': When the folds not under the cursor are closed.
614
615==============================================================================
6164. Behavior of folds *fold-behavior*
617
618When moving the cursor upwards or downwards and when scrolling, the cursor
619will move to the first line of a sequence of folded lines. When the cursor is
620already on a folded line, it moves to the next unfolded line or the next
621closed fold.
622
623While the cursor is on folded lines, the cursor is always displayed in the
624first column. The ruler does show the actual cursor position, but since the
625line is folded, it cannot be displayed there.
626
627Many movement commands handle a sequence of folded lines like an empty line.
628For example, the "w" command stops once in the first column.
629
Bram Moolenaar86b48162022-12-06 18:20:10 +0000630When starting a search in a closed fold it will not find a match in the
631current fold. It's like a forward search always starts from the end of the
632closed fold, while a backwards search starts from the start of the closed
633fold.
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635When in Insert mode, the cursor line is never folded. That allows you to see
636what you type!
637
638When using an operator, a closed fold is included as a whole. Thus "dl"
639deletes the whole closed fold under the cursor.
640
Bram Moolenaara3306952016-01-02 21:41:06 +0100641For Ex commands that work on buffer lines the range is adjusted to always
642start at the first line of a closed fold and end at the last line of a closed
643fold. Thus this command: >
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 :s/foo/bar/g
645when used with the cursor on a closed fold, will replace "foo" with "bar" in
646all lines of the fold.
647This does not happen for |:folddoopen| and |:folddoclosed|.
648
649When editing a buffer that has been edited before, the last used folding
650settings are used again. For manual folding the defined folds are restored.
651For all folding methods the manually opened and closed folds are restored.
652If this buffer has been edited in this window, the values from back then are
653used. Otherwise the values from the window where the buffer was edited last
654are used.
655
656==============================================================================
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200657 vim:tw=78:ts=8:noet:ft=help:norl: