Today I have been working on my new project SysInfoRM and for this I am using Python as my language of choice and VIM as my editor. In an attempt to improve my experience with using VIM I have been checking out code folding after a bit of digging around I tracked down a good vimrc file. I found a good post on Amitu Blog detailing the vimrc and other associated details. Unfortunately the site was down when I visited and I had to rely on the version that is cached by Google.

So I thought I would take a few points from that blog post and expand on it a bit in case the copy in Google’s cache copy goes as well. I give full credit to the original author and would like to thank him for his time and effort on the original post.

Here are the important new lines for your .vimrc

" automatically save and restore folds
au BufWinLeave * mkview
au BufWinEnter * silent loadview

" this lets us put the marker in the file so that
" it can be shared across and stored in version control.
set foldmethod=marker
" this is for python, put
" # name for the folded text # {{{
" to begin marker and
" # }}}
" close to end it.
set commentstring=\ #\ %s
" default fold level, all open, set it 200 or something
" to make it all closed.
set foldlevel=0

" share clipboard with windows clipboard
set clipboard+=unnamed

" set viminfo='100,f1
" minibufexplorer settings:j
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchWindows = 1

These are the keyboard command for using the folds.

  • zf create the fold, useful for manual and marker methods. Select any piece of text, [press v or shift-v, then use arrow keys], and then press zf. It will place the markers around the fold for you in marker mode; in case of manual, it will store fold location in memory. Remember f by saying this command “forms” the fold, or just remember fold :-)
  • zc close the fold at the cursor.
  • zo open the fold at the cursor.
  • zr increment the fold level by one, so if all classes are folded, they will opened, but function definitions will be kept folded.
  • zm reverse of the above, if one or more function folds are open, they will be closed, but classes will be kept open.
  • zR open all folds.
  • zM close all folds.
  • zj and zk can be used to jump from one fold to another.

Here is an example of what should expect. This is some example Python code.

from xml.parsers.xmlproc import xmlproc
from xml.parsers.xmlproc import xmlval
from xml.parsers.xmlproc import xmldtd

# XML Structure Checking # {{{
class MyApp(xmlproc.Application):
  def handle_start_tag(self,name,attrs):
    pass
  def handle_end_tag(self,name):
    pass
  def handle_data(self,data,start,end):
    pass
  def handle_comment(self,data):
    pass
# }}}

Placing the cursor at and point after line 5 then using zc, would result in.

from xml.parsers.xmlproc import xmlproc
from xml.parsers.xmlproc import xmlval
from xml.parsers.xmlproc import xmldtd

+-- 11 lines: XML Structure Checking-----------------------------------------------

That is all for now I plan to expand a bit on these vimrc modifications tomorrow and will then publish my entire vimrc which is becoming quite extensive.