FANDOM


(reviewed)
 
(9 intermediate revisions by 5 users not shown)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
When you set <tt>'foldmethod'</tt> to something like "indent" or "syntax" that defines folds as soon as you open the file, all folds are closed by default. If you set the foldlevel to a high setting, files are always loaded with opened folds. For example, you could put the settings below in your vimrc:
+
When you set <code>foldmethod</code> to something like <code>indent</code> or <code>syntax</code>, which defines folds as soon as you open the file, all folds are closed by default. If you set the <code>foldlevel</code> to a high setting, files are always loaded with opened folds. For example, you could put the settings below in your <code>vimrc</code>:
   
 
<pre>
 
<pre>
Line 17: Line 17:
 
set foldlevel=20
 
set foldlevel=20
 
</pre>
 
</pre>
  +
  +
This is not ideal, however, as the <code>foldlevel</code> option is local to the window, and additionally gets modified every time you use a command that adjusts the fold level, like <code>zm</code>, <code>zr</code>, and their friends.
  +
  +
A better method is to set the <code>foldlevel</code> whenever you load a buffer into a window. You could use autocommands for this, but there is a built-in option that does this for you automatically:
  +
  +
<pre>
  +
set foldlevelstart=20
  +
</pre>
  +
  +
This is ''much'' closer to an optimal solution, but one problem remains: even with the <code>foldlevelstart</code> option, it will take several iterations of the <code>zm</code> command to hide any of a file if you have set the <code>foldlevel</code> high enough. To fix this, you need to use autocmds. For example, to open all folds for a list of file types, but only up to the maximum fold level so that <code>zm</code> and related commands work right away, you could do the following:
  +
  +
<pre>
  +
" Note, perl automatically sets foldmethod in the syntax file
  +
autocmd Syntax c,cpp,vim,xml,html,xhtml setlocal foldmethod=syntax
  +
autocmd Syntax c,cpp,vim,xml,html,xhtml,perl normal zR
  +
</pre>
  +
  +
Tweak the event and filetypes matched to your liking.
  +
  +
You could use a combination of this method and the <code>foldlevelstart</code> option to get just enough fold levels open on certain filetypes, but default to having them all open for files which are not of one of your chosen types.
  +
  +
==See also==
  +
* [[Folding]] presents an overview of how to use folding
  +
  +
==References==
  +
*{{help|'foldlevel'}}
  +
*{{help|'foldlevelstart'}}
   
 
==Comments==
 
==Comments==

Latest revision as of 20:24, January 16, 2013

Tip 797 Printable Monobook Previous Next

created October 1, 2004 · complexity basic · author Pim Snel · version 6.0


When you set foldmethod to something like indent or syntax, which defines folds as soon as you open the file, all folds are closed by default. If you set the foldlevel to a high setting, files are always loaded with opened folds. For example, you could put the settings below in your vimrc:

set foldmethod=indent
set foldlevel=20

This is not ideal, however, as the foldlevel option is local to the window, and additionally gets modified every time you use a command that adjusts the fold level, like zm, zr, and their friends.

A better method is to set the foldlevel whenever you load a buffer into a window. You could use autocommands for this, but there is a built-in option that does this for you automatically:

set foldlevelstart=20

This is much closer to an optimal solution, but one problem remains: even with the foldlevelstart option, it will take several iterations of the zm command to hide any of a file if you have set the foldlevel high enough. To fix this, you need to use autocmds. For example, to open all folds for a list of file types, but only up to the maximum fold level so that zm and related commands work right away, you could do the following:

" Note, perl automatically sets foldmethod in the syntax file
autocmd Syntax c,cpp,vim,xml,html,xhtml setlocal foldmethod=syntax
autocmd Syntax c,cpp,vim,xml,html,xhtml,perl normal zR

Tweak the event and filetypes matched to your liking.

You could use a combination of this method and the foldlevelstart option to get just enough fold levels open on certain filetypes, but default to having them all open for files which are not of one of your chosen types.

See alsoEdit

  • Folding presents an overview of how to use folding

ReferencesEdit

CommentsEdit

Community content is available under CC-BY-SA unless otherwise noted.