From Fedora Project Wiki

Revision as of 05:06, 11 October 2010 by Sgordon (talk | contribs) (Added content on using folds. Still requires expansion of use of other foldmethod values.)

Cog.png
This page needs some love
This page should be revised or reconstructed to be more helpful. Problems may include being out of step with current team or project status or process.
Warning.png
This page is a draft only
It is still under construction and content may change. Do not rely on the information on this page.

Folding

As of VIM 7.0 support for folds is included in the editor. Folds allow sections of a document which are not being actively worked on to be 'folded' into themselves and hidden. Programmers often use folds to hide and show functions, classes and structures but the functionality is equally useful when editing DocBook XML.

Whether VIM folds content and how it folds it depends on the foldmethod setting. There are a number of valid options:

  • manual - Folds are created manually.
  • indent - Folds are created based on indentation levels.
  • expr - Folds are created based on the value of foldexpr.
  • marker - Folds are created based on markers.
  • syntax - Folds are created based on syntax highlighting items.
  • diff - Folds are created based on whether text has/hasn't changed, text that hasn't changed is folded.

When working with consistently indented XML content the indent method is very easy to invoke and requires no additional setup. It can be accessed by issuing:

set foldmethod=indent

Issuing this command while a consistently indented XML file is open in the buffer will result in a number of folds being created. Folds can be opened by placing the cursor on the line denoting the fold. For example in the following XML block there is an appendix opened on line 4 and closed on line 184. The enclosed content is a fold which contains 179 lines of text.

  4 <appendix id="chap-RedHat_Enterprise_Virtualization-Administration_Guide-Backup_and_Recovery">
  5 +--179 lines: <title>&PRODUCT; Manager backup and recovery</title>----------------
184 </appendix>     

Opening the fold is as simple as placing the cursor on the fold in command mode and entering zo. Folds can be closed by issuing zc in command mode while the cursor is within the fold.

By default folds are discarded when exiting. Folds can be explicitly written to disk by issuing :mkview. When the file is next opened :loadview can be issued to reload previously saved folds for the document. Alternatively the following lines can be placed in the users .vimrc file to ensure that folds are automatically loaded and saved when buffers are opened and closed respectively.

au BufWinLeave * mkview
au BufWinEnter * silent loadview

There are many other options to facilitate modification of the way folds behave in VIM. Further information on the use of folds in VIM can be accessed by issuing :help fold from command mode.

Vi config file hacks

For convenient editing, I add the following lines to the bottom of any DocBook files I'll be editing in Vi(m):

<!-- 
vim: softtabstop=2:shiftwidth=2:expandtab:textwidth=72 
-->

Quick key tricks

Misc. tips