Vim Tips Wiki
Register
No edit summary
 
 
(15 intermediate revisions by 6 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=797
 
|id=797
  +
|previous=796
|title=All folds open at startup or just openen file
 
  +
|next=798
|created=October 1, 2004 7:11
+
|created=October 1, 2004
 
|complexity=basic
 
|complexity=basic
 
|author=Pim Snel
 
|author=Pim Snel
 
|version=6.0
 
|version=6.0
 
|rating=6/7
 
|rating=6/7
  +
|category1=Folding
|text=
 
  +
|category2=
when you set foldmethod=indent all folds are closed when you open a file. If you set the foldlevel to a high setting files are always started with opened folds. 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>
 
set foldmethod=indent
 
set foldlevel=20
  +
</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:
set foldmethod=indent
 
   
  +
<pre>
set foldlevel=20
 
  +
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
   
== Comments ==
+
==References==
  +
*{{help|'foldlevel'}}
See comments toward the bottom of [[VimTip80]]
 
  +
*{{help|'foldlevelstart'}}
   
  +
==Comments==
Suresh Govindachar
 
, October 1, 2004 13:31
 
----
 
<!-- parsed by vimtips.py in 0.649179 seconds-->
 

Latest revision as of 20:24, 16 January 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 also[]

  • Folding presents an overview of how to use folding

References[]

Comments[]