usr_28.txt 16 KB

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