neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

usr_28.txt (15976B)


      1 *usr_28.txt*	Nvim
      2 
      3 
      4 	     VIM USER MANUAL	by Bram Moolenaar
      5 
      6 
      7 			   Folding
      8 
      9 
     10 Structured text can be separated in sections.  And sections in sub-sections.
     11 Folding allows you to display a section as one line, providing an overview.
     12 This chapter explains the different ways this can be done.
     13 
     14 |28.1|	What is folding?
     15 |28.2|	Manual folding
     16 |28.3|	Working with folds
     17 |28.4|	Saving and restoring folds
     18 |28.5|	Folding by indent
     19 |28.6|	Folding with markers
     20 |28.7|	Folding by syntax
     21 |28.8|	Folding by expression
     22 |28.9|	Folding unchanged lines
     23 |28.10|	Which fold method to use?
     24 
     25     Next chapter: |usr_29.txt|  Moving through programs
     26 Previous chapter: |usr_27.txt|  Search commands and patterns
     27 Table of contents: |usr_toc.txt|
     28 
     29 ==============================================================================
     30 *28.1*	What is folding?
     31 
     32 Folding is used to show a range of lines in the buffer as a single line on the
     33 screen.  Like a piece of paper which is folded to make it shorter:
     34 
     35 +------------------------+
     36 | line 1		 |
     37 | line 2		 |
     38 | line 3		 |
     39 |_______________________ |
     40 \			 \
     41  \________________________\
     42  / folded lines		  /
     43 /________________________/
     44 | line 12		 |
     45 | line 13		 |
     46 | line 14		 |
     47 +------------------------+
     48 
     49 The text is still in the buffer, unchanged.  Only the way lines are displayed
     50 is affected by folding.
     51 
     52 The advantage of folding is that you can get a better overview of the
     53 structure of text, by folding lines of a section and replacing it with a line
     54 that indicates that there is a section.
     55 
     56 ==============================================================================
     57 *28.2*	Manual folding
     58 
     59 Try it out: Position the cursor in a paragraph and type: >
     60 
     61 zfap
     62 
     63 You will see that the paragraph is replaced by a highlighted line.  You have
     64 created a fold.  |zf| is an operator and |ap| a text object selection.  You
     65 can use the |zf| operator with any movement command to create a fold for the
     66 text that it moved over.  |zf| also works in Visual mode.
     67 
     68 To view the text again, open the fold by typing: >
     69 
     70 zo
     71 
     72 And you can close the fold again with: >
     73 
     74 zc
     75 
     76 All the folding commands start with "z".  With some fantasy, this looks like a
     77 folded piece of paper, seen from the side.  The letter after the "z" has a
     78 mnemonic meaning to make it easier to remember the commands:
     79 
     80 zf	F-old creation
     81 zo	O-pen a fold
     82 zc	C-lose a fold
     83 
     84 Folds can be nested: A region of text that contains folds can be folded
     85 again.  For example, you can fold each paragraph in this section, and then
     86 fold all the sections in this chapter.  Try it out.  You will notice that
     87 opening the fold for the whole chapter will restore the nested folds as they
     88 were, some may be open and some may be closed.
     89 
     90 Suppose you have created several folds, and now want to view all the text.
     91 You could go to each fold and type "zo".  To do this faster, use this command: >
     92 
     93 zr
     94 
     95 This will R-educe the folding.  The opposite is: >
     96 
     97 zm
     98 
     99 This folds M-ore.  You can repeat "zr" and "zm" to open and close nested folds
    100 of several levels.
    101 
    102 If you have nested several levels deep, you can open all of them with: >
    103 
    104 zR
    105 
    106 This R-educes folds until there are none left.  And you can close all folds
    107 with: >
    108 
    109 zM
    110 
    111 This folds M-ore and M-ore.
    112 
    113 You can quickly disable the folding with the |zn| command.  Then |zN| brings
    114 back the folding as it was.  |zi| toggles between the two.  This is a useful
    115 way of working:
    116 - create folds to get overview on your file
    117 - move around to where you want to do your work
    118 - do |zi| to look at the text and edit it
    119 - do |zi| again to go back to moving around
    120 
    121 More about manual folding in the reference manual: |fold-manual|
    122 
    123 ==============================================================================
    124 *28.3*	Working with folds
    125 
    126 When some folds are closed, movement commands like "j" and "k" move over a
    127 fold like it was a single, empty line.  This allows you to quickly move around
    128 over folded text.
    129 
    130 You can yank, delete and put folds as if it was a single line.  This is very
    131 useful if you want to reorder functions in a program.  First make sure that
    132 each fold contains a whole function (or a bit less) by selecting the right
    133 'foldmethod'.  Then delete the function with "dd", move the cursor and put it
    134 with "p".  If some lines of the function are above or below the fold, you can
    135 use Visual selection:
    136 - put the cursor on the first line to be moved
    137 - hit "V" to start Visual mode
    138 - put the cursor on the last line to be moved
    139 - hit "d" to delete the selected lines.
    140 - move the cursor to the new position and "p"ut the lines there.
    141 
    142 It is sometimes difficult to see or remember where a fold is located, thus
    143 where a |zo| command would actually work.  To see the defined folds: >
    144 
    145 :set foldcolumn=4
    146 
    147 This will show a small column on the left of the window to indicate folds.
    148 A "+" is shown for a closed fold.  A "-" is shown at the start of each open
    149 fold and "|" at following lines of the fold.
    150 
    151 You can use the mouse to open a fold by clicking on the "+" in the foldcolumn.
    152 Clicking on the "-" or a "|" below it will close an open fold.
    153 
    154 To open all folds at the cursor line use |zO|.
    155 To close all folds at the cursor line use |zC|.
    156 To delete a fold at the cursor line use |zd|.
    157 To delete all folds at the cursor line use |zD|.
    158 
    159 When in Insert mode, the fold at the cursor line is never closed.  That allows
    160 you to see what you type!
    161 
    162 Folds are opened automatically when jumping around or moving the cursor left
    163 or right.  For example, the "0" command opens the fold under the cursor
    164 (if 'foldopen' contains "hor", which is the default).  The 'foldopen' option
    165 can be changed to open folds for specific commands.  If you want the line
    166 under the cursor always to be open, do this: >
    167 
    168 :set foldopen=all
    169 
    170 Warning: You won't be able to move onto a closed fold then.  You might want to
    171 use this only temporarily and then set it back to the default: >
    172 
    173 :set foldopen&
    174 
    175 You can make folds close automatically when you move out of it: >
    176 
    177 :set foldclose=all
    178 
    179 This will re-apply 'foldlevel' to all folds that don't contain the cursor.
    180 You have to try it out if you like how this feels.  Use |zm| to fold more and
    181 |zr| to fold less (reduce folds).
    182 
    183 The folding is local to the window.  This allows you to open two windows on
    184 the same buffer, one with folds and one without folds.  Or one with all folds
    185 closed and one with all folds open.
    186 
    187 ==============================================================================
    188 *28.4*	Saving and restoring folds
    189 
    190 When you abandon a file (starting to edit another one), the state of the folds
    191 is lost.  If you come back to the same file later, all manually opened and
    192 closed folds are back to their default.  When folds have been created
    193 manually, all folds are gone!  To save the folds use the |:mkview| command: >
    194 
    195 :mkview
    196 
    197 This will store the settings and other things that influence the view on the
    198 file.  You can change what is stored with the 'viewoptions' option.
    199 When you come back to the same file later, you can load the view again: >
    200 
    201 :loadview
    202 
    203 You can store up to ten views on one file.  For example, to save the current
    204 setup as the third view and load the second view: >
    205 
    206 :mkview 3
    207 :loadview 2
    208 
    209 Note that when you insert or delete lines the views might become invalid.
    210 Also check out the 'viewdir' option, which specifies where the views are
    211 stored.  You might want to delete old views now and then.
    212 
    213 ==============================================================================
    214 *28.5*	Folding by indent
    215 
    216 Defining folds with |zf| is a lot of work.  If your text is structured by
    217 giving lower level items a larger indent, you can use the indent folding
    218 method.  This will create folds for every sequence of lines with the same
    219 indent.  Lines with a larger indent will become nested folds.  This works well
    220 with many programming languages.
    221 
    222 Try this by setting the 'foldmethod' option: >
    223 
    224 :set foldmethod=indent
    225 
    226 Then you can use the |zm| and |zr| commands to fold more and reduce folding.
    227 It's easy to see on this example text:
    228 
    229 This line is not indented
    230 This line is indented once
    231 	This line is indented twice
    232 	This line is indented twice
    233 This line is indented once
    234 This line is not indented
    235 This line is indented once
    236 This line is indented once
    237 
    238 Note that the relation between the amount of indent and the fold depth depends
    239 on the 'shiftwidth' option.  Each 'shiftwidth' worth of indent adds one to the
    240 depth of the fold.  This is called a fold level.
    241 
    242 When you use the |zr| and |zm| commands you actually increase or decrease the
    243 'foldlevel' option.  You could also set it directly: >
    244 
    245 :set foldlevel=3
    246 
    247 This means that all folds with three times a 'shiftwidth' indent or more will
    248 be closed.  The lower the foldlevel, the more folds will be closed.  When
    249 'foldlevel' is zero, all folds are closed.  |zM| does set 'foldlevel' to zero.
    250 The opposite command |zR| sets 'foldlevel' to the deepest fold level that is
    251 present in the file.
    252 
    253 Thus there are two ways to open and close the folds:
    254 (A) By setting the fold level.
    255    This gives a very quick way of "zooming out" to view the structure of the
    256    text, move the cursor, and "zoom in" on the text again.
    257 
    258 (B) By using |zo| and |zc| commands to open or close specific folds.
    259    This allows opening only those folds that you want to be open, while other
    260    folds remain closed.
    261 
    262 This can be combined: You can first close most folds by using |zm| a few times
    263 and then open a specific fold with |zo|.  Or open all folds with |zR| and
    264 then close specific folds with |zc|.
    265 
    266 But you cannot manually define folds when 'foldmethod' is "indent", as that
    267 would conflict with the relation between the indent and the fold level.
    268 
    269 More about folding by indent in the reference manual: |fold-indent|
    270 
    271 ==============================================================================
    272 *28.6*	Folding with markers
    273 
    274 Markers in the text are used to specify the start and end of a fold region.
    275 This gives precise control over which lines are included in a fold.  The
    276 disadvantage is that the text needs to be modified.
    277 
    278 Try it: >
    279 
    280 :set foldmethod=marker
    281 
    282 Example text, as it could appear in a C program: >
    283 
    284 /* foobar () {{{ */
    285 int foobar()
    286 {
    287 	/* return a value {{{ */
    288 	return 42;
    289 	/* }}} */
    290 }
    291 /* }}} */
    292 
    293 Notice that the folded line will display the text before the marker.  This is
    294 very useful to tell what the fold contains.
    295 
    296 It's quite annoying when the markers don't pair up correctly after moving some
    297 lines around.  This can be avoided by using numbered markers.  Example: >
    298 
    299 /* global variables {{{1 */
    300 int varA, varB;
    301 
    302 /* functions {{{1 */
    303 /* funcA() {{{2 */
    304 void funcA() {}
    305 
    306 /* funcB() {{{2 */
    307 void funcB() {}
    308 /* }}}1 */
    309 
    310 At every numbered marker a fold at the specified level begins.  This will make
    311 any fold at a higher level stop here.  You can just use numbered start markers
    312 to define all folds.  Only when you want to explicitly stop a fold before
    313 another starts you need to add an end marker.
    314 
    315 More about folding with markers in the reference manual: |fold-marker|
    316 
    317 ==============================================================================
    318 *28.7*	Folding by syntax
    319 
    320 For each language Vim uses a different syntax file.  This defines the colors
    321 for various items in the file.  If you are reading this in Vim, in a terminal
    322 that supports colors, the colors you see are made with the "help" syntax file.
    323   In the syntax files it is possible to add syntax items that have the "fold"
    324 argument.  These define a fold region.  This requires writing a syntax file
    325 and adding these items in it.  That's not so easy to do.  But once it's done,
    326 all folding happens automatically.
    327   Here we'll assume you are using an existing syntax file.  Then there is
    328 nothing more to explain.  You can open and close folds as explained above.
    329 The folds will be created and deleted automatically when you edit the file.
    330 
    331 More about folding by syntax in the reference manual: |fold-syntax|
    332 
    333 ==============================================================================
    334 *28.8*	Folding by expression
    335 
    336 This is similar to folding by indent, but instead of using the indent of a
    337 line a user function is called to compute the fold level of a line.  You can
    338 use this for text where something in the text indicates which lines belong
    339 together.  An example is an e-mail message where the quoted text is indicated
    340 by a ">" before the line.  To fold these quotes use this: >
    341 
    342 :set foldmethod=expr
    343 :set foldexpr=strlen(substitute(substitute(getline(v:lnum),'\\s','',\"g\"),'[^>].*','',''))
    344 
    345 You can try it out on this text:
    346 
    347 > quoted text he wrote
    348 > quoted text he wrote
    349 > > double quoted text I wrote
    350 > > double quoted text I wrote
    351 
    352 Explanation for the 'foldexpr' used in the example (inside out):
    353   getline(v:lnum)			gets the current line
    354   substitute(...,'\\s','','g')		removes all white space from the line
    355   substitute(...,'[^>].*','','')	removes everything after leading '>'s
    356   strlen(...)				counts the length of the string, which
    357 				is the number of '>'s found
    358 
    359 Note that a backslash must be inserted before every space, double quote and
    360 backslash for the ":set" command.  If this confuses you, do >
    361 
    362 :set foldexpr
    363 
    364 to check the actual resulting value.  To correct a complicated expression, use
    365 the command-line completion: >
    366 
    367 :set foldexpr=<Tab>
    368 
    369 Where <Tab> is a real Tab.  Vim will fill in the previous value, which you can
    370 then edit.
    371 
    372 When the expression gets more complicated you should put it in a function and
    373 set 'foldexpr' to call that function.
    374 
    375 More about folding by expression in the reference manual: |fold-expr|
    376 
    377 ==============================================================================
    378 *28.9*	Folding unchanged lines
    379 
    380 This is useful when you set the 'diff' option in the same window.  The
    381 |-d| option does this for you.  Example: >
    382 
    383 :setlocal diff foldmethod=diff scrollbind nowrap foldlevel=1
    384 
    385 Do this in every window that shows a different version of the same file.  You
    386 will clearly see the differences between the files, while the text that didn't
    387 change is folded.
    388 
    389 For more details see |fold-diff|.
    390 
    391 ==============================================================================
    392 *28.10* Which fold method to use?
    393 
    394 All these possibilities make you wonder which method you should choose.
    395 Unfortunately, there is no golden rule.  Here are some hints.
    396 
    397 If there is a syntax file with folding for the language you are editing, that
    398 is probably the best choice.  If there isn't one, you might try to write it.
    399 This requires a good knowledge of search patterns.  It's not easy, but when
    400 it's working you will not have to define folds manually.
    401 
    402 Typing commands to manually fold regions can be used for unstructured text.
    403 Then use the |:mkview| command to save and restore your folds.
    404 
    405 The marker method requires you to change the file.  If you are sharing the
    406 files with other people or you have to meet company standards, you might not
    407 be allowed to add them.
    408   The main advantage of markers is that you can put them exactly where you
    409 want them.  That avoids that a few lines are missed when you cut and paste
    410 folds.  And you can add a comment about what is contained in the fold.
    411 
    412 Folding by indent is something that works in many files, but not always very
    413 well.  Use it when you can't use one of the other methods.  However, it is
    414 very useful for outlining.  Then you specifically use one 'shiftwidth' for
    415 each nesting level.
    416 
    417 Folding with expressions can make folds in almost any structured text.  It is
    418 quite simple to specify, especially if the start and end of a fold can easily
    419 be recognized.
    420   If you use the "expr" method to define folds, but they are not exactly how
    421 you want them, you could switch to the "manual" method.  This will not remove
    422 the defined folds.  Then you can delete or add folds manually.
    423 
    424 ==============================================================================
    425 
    426 Next chapter: |usr_29.txt|  Moving through programs
    427 
    428 Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl: