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