1*fold.txt* For Vim version 7.3. Last change: 2010 May 13 2 3 4 VIM REFERENCE MANUAL by Bram Moolenaar 5 6 7Folding *Folding* *folding* *folds* 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. 112You can change and restore them if you are careful. 113 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 130It may happened that folds are not updated properly. You can use |zx| or |zX| 131to force updating folds. 132 133 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 197The 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: > 202 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* 280zF Create a fold for [count] lines. Works like "zf". 281 282:{range}fo[ld] *:fold* *:fo* 283 Create a fold for the lines in {range}. Works like "zf". 284 285 *zd* *E351* 286zd Delete one fold at the cursor. When the cursor is on a folded 287 line, that fold is deleted. Nested folds are moved one level 288 up. In Visual mode all folds (partially) in the selected area 289 are deleted. Careful: This easily deletes more folds than you 290 expect and there is no undo. 291 This only works when 'foldmethod' is "manual" or "marker". 292 Also see |fold-delete-marker|. 293 294 *zD* 295zD Delete folds recursively at the cursor. In Visual mode all 296 folds (partially) in the selected area and all nested folds in 297 them are deleted. 298 This only works when 'foldmethod' is "manual" or "marker". 299 Also see |fold-delete-marker|. 300 301 *zE* *E352* 302zE Eliminate all folds in the window. 303 This only works when 'foldmethod' is "manual" or "marker". 304 Also see |fold-delete-marker|. 305 306 307OPENING AND CLOSING FOLDS ~ 308 309A fold smaller than 'foldminlines' will always be displayed like it was open. 310Therefore the commands below may work differently on small folds. 311 312 *zo* 313zo Open one fold under the cursor. When a count is given, that 314 many folds deep will be opened. In Visual mode one level of 315 folds is opened for all lines in the selected area. 316 317 *zO* 318zO Open all folds under the cursor recursively. Folds that don't 319 contain the cursor line are unchanged. 320 In Visual mode it opens all folds that are in the selected 321 area, also those that are only partly selected. 322 323 *zc* 324zc Close one fold under the cursor. When a count is given, that 325 many folds deep are closed. In Visual mode one level of folds 326 is closed for all lines in the selected area. 327 'foldenable' will be set. 328 329 *zC* 330zC Close all folds under the cursor recursively. Folds that 331 don't contain the cursor line are unchanged. 332 In Visual mode it closes all folds that are in the selected 333 area, also those that are only partly selected. 334 'foldenable' will be set. 335 336 *za* 337za When on a closed fold: open it. When folds are nested, you 338 may have to use "za" several times. When a count is given, 339 that many closed folds are opened. 340 When on an open fold: close it and set 'foldenable'. This 341 will only close one level, since using "za" again will open 342 the fold. When a count is given that many folds will be 343 closed (that's not the same as repeating "za" that many 344 times). 345 346 *zA* 347zA When on a closed fold: open it recursively. 348 When on an open fold: close it recursively and set 349 'foldenable'. 350 351 *zv* 352zv View cursor line: Open just enough folds to make the line in 353 which the cursor is located not folded. 354 355 *zx* 356zx Update folds: Undo manually opened and closed folds: re-apply 357 'foldlevel', then do "zv": View cursor line. 358 Also forces recomputing folds. This is useful when using 359 'foldexpr' and the buffer is changed in a way that results in 360 folds not to be updated properly. 361 362 *zX* 363zX Undo manually opened and closed folds: re-apply 'foldlevel'. 364 Also forces recomputing folds, like |zx|. 365 366 *zm* 367zm Fold more: Subtract one from 'foldlevel'. If 'foldlevel' was 368 already zero nothing happens. 369 'foldenable' will be set. 370 371 *zM* 372zM Close all folds: set 'foldlevel' to 0. 373 'foldenable' will be set. 374 375 *zr* 376zr Reduce folding: Add one to 'foldlevel'. 377 378 *zR* 379zR Open all folds. This sets 'foldlevel' to highest fold level. 380 381 *:foldo* *:foldopen* 382:{range}foldo[pen][!] 383 Open folds in {range}. When [!] is added all folds are 384 opened. Useful to see all the text in {range}. Without [!] 385 one level of folds is opened. 386 387 *:foldc* *:foldclose* 388:{range}foldc[lose][!] 389 Close folds in {range}. When [!] is added all folds are 390 closed. Useful to hide all the text in {range}. Without [!] 391 one level of folds is closed. 392 393 *zn* 394zn Fold none: reset 'foldenable'. All folds will be open. 395 396 *zN* 397zN Fold normal: set 'foldenable'. All folds will be as they 398 were before. 399 400 *zi* 401zi Invert 'foldenable'. 402 403 404MOVING OVER FOLDS ~ 405 *[z* 406[z Move to the start of the current open fold. If already at the 407 start, move to the start of the fold that contains it. If 408 there is no containing fold, the command fails. 409 When a count is used, repeats the command [count] times. 410 411 *]z* 412]z Move to the end of the current open fold. If already at the 413 end, move to the end of the fold that contains it. If there 414 is no containing fold, the command fails. 415 When a count is used, repeats the command [count] times. 416 417 *zj* 418zj Move downwards to the start of the next fold. A closed fold 419 is counted as one fold. 420 When a count is used, repeats the command [count] times. 421 This command can be used after an |operator|. 422 423 *zk* 424zk Move upwards to the end of the previous fold. A closed fold 425 is counted as one fold. 426 When a count is used, repeats the command [count] times. 427 This command can be used after an |operator|. 428 429 430EXECUTING COMMANDS ON FOLDS ~ 431 432:[range]foldd[oopen] {cmd} *:foldd* *:folddoopen* 433 Execute {cmd} on all lines that are not in a closed fold. 434 When [range] is given, only these lines are used. 435 Each time {cmd} is executed the cursor is positioned on the 436 line it is executed for. 437 This works like the ":global" command: First all lines that 438 are not in a closed fold are marked. Then the {cmd} is 439 executed for all marked lines. Thus when {cmd} changes the 440 folds, this has no influence on where it is executed (except 441 when lines are deleted, of course). 442 Example: > 443 :folddoopen s/end/loop_end/ge 444< Note the use of the "e" flag to avoid getting an error message 445 where "end" doesn't match. 446 447:[range]folddoc[losed] {cmd} *:folddoc* *:folddoclosed* 448 Execute {cmd} on all lines that are in a closed fold. 449 Otherwise like ":folddoopen". 450 451============================================================================== 4523. Fold options *fold-options* 453 454COLORS *fold-colors* 455 456The colors of a closed fold are set with the Folded group |hl-Folded|. The 457colors of the fold column are set with the FoldColumn group |hl-FoldColumn|. 458Example to set the colors: > 459 460 :highlight Folded guibg=grey guifg=blue 461 :highlight FoldColumn guibg=darkgrey guifg=white 462 463 464FOLDLEVEL *fold-foldlevel* 465 466'foldlevel' is a number option: The higher the more folded regions are open. 467When 'foldlevel' is 0, all folds are closed. 468When 'foldlevel' is positive, some folds are closed. 469When 'foldlevel' is very high, all folds are open. 470'foldlevel' is applied when it is changed. After that manually folds can be 471opened and closed. 472When increased, folds above the new level are opened. No manually opened 473folds will be closed. 474When decreased, folds above the new level are closed. No manually closed 475folds will be opened. 476 477 478FOLDTEXT *fold-foldtext* 479 480'foldtext' is a string option that specifies an expression. This expression 481is evaluated to obtain the text displayed for a closed fold. Example: > 482 483 :set foldtext=v:folddashes.substitute(getline(v:foldstart),'/\\*\\\|\\*/\\\|{{{\\d\\=','','g') 484 485This shows the first line of the fold, with "/*", "*/" and "{{{" removed. 486Note the use of backslashes to avoid some characters to be interpreted by the 487":set" command. It's simpler to define a function and call that: > 488 489 :set foldtext=MyFoldText() 490 :function MyFoldText() 491 : let line = getline(v:foldstart) 492 : let sub = substitute(line, '/\*\|\*/\|{{{\d\=', '', 'g') 493 : return v:folddashes . sub 494 :endfunction 495 496Evaluating 'foldtext' is done in the |sandbox|. The current window is set to 497the window that displays the line. Errors are ignored. 498 499The default value is |foldtext()|. This returns a reasonable text for most 500types of folding. If you don't like it, you can specify your own 'foldtext' 501expression. It can use these special Vim variables: 502 v:foldstart line number of first line in the fold 503 v:foldend line number of last line in the fold 504 v:folddashes a string that contains dashes to represent the 505 foldlevel. 506 v:foldlevel the foldlevel of the fold 507 508In the result a TAB is replaced with a space and unprintable characters are 509made into printable characters. 510 511The resulting line is truncated to fit in the window, it never wraps. 512When there is room after the text, it is filled with the character specified 513by 'fillchars'. 514 515Note that backslashes need to be used for characters that the ":set" command 516handles differently: Space, backslash and double-quote. |option-backslash| 517 518 519FOLDCOLUMN *fold-foldcolumn* 520 521'foldcolumn' is a number, which sets the width for a column on the side of the 522window to indicate folds. When it is zero, there is no foldcolumn. A normal 523value is 4 or 5. The minimal useful value is 2, although 1 still provides 524some information. The maximum is 12. 525 526An open fold is indicated with a column that has a '-' at the top and '|' 527characters below it. This column stops where the open fold stops. When folds 528nest, the nested fold is one character right of the fold it's contained in. 529 530A closed fold is indicated with a '+'. 531 532Where the fold column is too narrow to display all nested folds, digits are 533shown to indicate the nesting level. 534 535The mouse can also be used to open and close folds by clicking in the 536fold column: 537- Click on a '+' to open the closed fold at this row. 538- Click on any other non-blank character to close the open fold at this row. 539 540 541OTHER OPTIONS 542 543'foldenable' 'fen': Open all folds while not set. 544'foldexpr' 'fde': Expression used for "expr" folding. 545'foldignore' 'fdi': Characters used for "indent" folding. 546'foldmarker' 'fmr': Defined markers used for "marker" folding. 547'foldmethod' 'fdm': Name of the current folding method. 548'foldminlines' 'fml': Minimum number of screen lines for a fold to be 549 displayed closed. 550'foldnestmax' 'fdn': Maximum nesting for "indent" and "syntax" folding. 551'foldopen' 'fdo': Which kinds of commands open closed folds. 552'foldclose' 'fcl': When the folds not under the cursor are closed. 553 554============================================================================== 5554. Behavior of folds *fold-behavior* 556 557When moving the cursor upwards or downwards and when scrolling, the cursor 558will move to the first line of a sequence of folded lines. When the cursor is 559already on a folded line, it moves to the next unfolded line or the next 560closed fold. 561 562While the cursor is on folded lines, the cursor is always displayed in the 563first column. The ruler does show the actual cursor position, but since the 564line is folded, it cannot be displayed there. 565 566Many movement commands handle a sequence of folded lines like an empty line. 567For example, the "w" command stops once in the first column. 568 569When in Insert mode, the cursor line is never folded. That allows you to see 570what you type! 571 572When using an operator, a closed fold is included as a whole. Thus "dl" 573deletes the whole closed fold under the cursor. 574 575For Ex commands the range is adjusted to always start at the first line of a 576closed fold and end at the last line of a closed fold. Thus this command: > 577 :s/foo/bar/g 578when used with the cursor on a closed fold, will replace "foo" with "bar" in 579all lines of the fold. 580This does not happen for |:folddoopen| and |:folddoclosed|. 581 582When editing a buffer that has been edited before, the last used folding 583settings are used again. For manual folding the defined folds are restored. 584For all folding methods the manually opened and closed folds are restored. 585If this buffer has been edited in this window, the values from back then are 586used. Otherwise the values from the window where the buffer was edited last 587are used. 588 589============================================================================== 590 vim:tw=78:ts=8:ft=help:norl: 591