Vim Tips Wiki
Edit Page
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 38: Line 38:
   
 
===Indent folding===
 
===Indent folding===
  +
In the vimrc, set the foldcolumn and foldlevel to the depth of folds you want displayed. A sidebar will appear showing which lines in the file can be folded, or are already folded.
As noted above, with 'foldmethod' set to indent, lines with the same level of indentation will be folded together. Note this means text like this:
 
 
<pre>
 
Line one
 
Line two
 
Line three
 
Line four
 
</pre>
 
 
will be folded as follows, which may not be expected:
 
 
<pre>
 
Line one
 
+ Line two and three
 
Line four
 
</pre>
 
 
Since folding by indent may not be as clear as you like, the foldcolumn is especially useful for this method. Setting foldcolumn to at least the level of folds you want displayed will show a sidebar, in which you can see which lines in the file can be folded, or are already folded, with a '+' (closed) or '-' (open) indicator.
 
   
 
===Syntax folding===
 
===Syntax folding===
Try <code>:setlocal foldmethod=syntax</code>. Many syntax files define folding based on the language syntax, although you may need to enable it by [[VimTip1534|setting syntax file options]]. If a specific syntax file doesn't define folding, you can [[Syntax folding of Vim scripts|define your own]].
+
Try setting <code>foldmethod=syntax</code>. Many syntax files define folding based on the language syntax, although you may need to enable it by [[VimTip1534|setting syntax file options]]. If a specific syntax file doesn't define folding, you can [[Syntax folding of Vim scripts|define your own]].
   
 
Note that this particular fold method especially may define too many folds for your liking. You can change this using the {{help|prefix=no|'foldnestmax'}} option, by setting it to a value low enough for your liking.
 
Note that this particular fold method especially may define too many folds for your liking. You can change this using the {{help|prefix=no|'foldnestmax'}} option, by setting it to a value low enough for your liking.
Line 77: Line 60:
   
 
===Folding with markers===
 
===Folding with markers===
Setting foldmethod to "marker" is similar to manual folding, because you can use zf and zd to add/remove folds, and all folds are defined manually. However, it works by adding special markers to your buffer text so that the same folds can be loaded by anyone opening the file in Vim. The markers are defined by the {{help|prefix=no|'foldmarker'}} option, which defaults to three consecutive open curly braces to start a fold and three consecutive closed curly braces to end a fold. This method is mostly useful when the other options don't give satisfactory results, either because a syntax file does not define folding, you want to break up a file into logical portions instead of syntactical constructs, or you are folding plain text in some way.
+
Setting foldmethod to "marker" is similar to manual folding, because you can use zf and zd to add/remove folds, and all folds are defined manually. However, it works by adding special markers to your buffer text so that the same folds can be loaded by anyone opening the file in Vim. This method is mostly useful when the other options don't give satisfactory results, either because a syntax file does not define folding, you want to break up a file into logical portions instead of syntactical constructs, or you are folding plain text in some way.
 
If you change the default 'foldmarker' setting, be sure to communicate the correct setting to other people in some way. An especially effective method for this would be to use a [[Modeline magic|modeline]], so that most Vim users will get your intended settings by default when they edit the file.
 
   
 
===Diff folding===
 
===Diff folding===
 
This foldmethod should never be set manually without very good reason. It is the default fold method when using Vim in diff mode. Simply running any of the commands to put Vim in diff mode will set this option for you. It works by folding away lines that do not differ between the buffers in the diff, so that you can focus on the lines with differences. Note that you can tweak the number of lines to leave unfolded, using the {{help|prefix=no|'diffopt'}} option.
 
This foldmethod should never be set manually without very good reason. It is the default fold method when using Vim in diff mode. Simply running any of the commands to put Vim in diff mode will set this option for you. It works by folding away lines that do not differ between the buffers in the diff, so that you can focus on the lines with differences. Note that you can tweak the number of lines to leave unfolded, using the {{help|prefix=no|'diffopt'}} option.
 
If you want folding in diff/patch files, see [[Folding for diff files]] instead.
 
   
 
==Opening and closing folds==
 
==Opening and closing folds==
Line 112: Line 91:
 
In the first mapping for Space, <code>@=</code> refers to the expression register ({{help|1=@=}}). The following expression is evaluated when Enter is pressed (<code><CR></code>). The value of <code>foldlevel('.')</code> is determined (the fold level at the current line, or zero if the current line is not in a fold). If the result is nonzero (true), the result is <code>'za'</code> (toggle fold); otherwise it is <code>"\<Space>"</code>, which evaluates back to the default behaviour of the <Space> key (move forward by one character). {{help|expr1}}
 
In the first mapping for Space, <code>@=</code> refers to the expression register ({{help|1=@=}}). The following expression is evaluated when Enter is pressed (<code><CR></code>). The value of <code>foldlevel('.')</code> is determined (the fold level at the current line, or zero if the current line is not in a fold). If the result is nonzero (true), the result is <code>'za'</code> (toggle fold); otherwise it is <code>"\<Space>"</code>, which evaluates back to the default behaviour of the <Space> key (move forward by one character). {{help|expr1}}
   
==Saving folds before closing vim==
+
==Saving folds before closing the vim==
Vim provides a good mechanism called <code>view</code>. You can use it to save a fold. See [[VimTip991|Make views automatic]]. You may also want to use plugin {{script|id=4021|text=restore_view.vim}}.
+
Vim provides a good mechanism called <code>view</code>. You can use it to saving a fold. See [[VimTip991|Make views automatic]]. You may also want to use plugin {{script|id=4021|text=restore_view.vim}}.
   
 
==Commands over folds==
 
==Commands over folds==
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)