Vim Tips Wiki
Tip 519 Printable Monobook Previous Next

created 2003 · complexity basic · author Eric Boucher · version 6.0

If you are using Microsoft Visual Studio .NET for editing Visual Basic .NET files, the Window Form Designer Generated Code is folded by default. If you want to have the same nice feature when editing the same file in Vim you can make use of a fold expression.

Create a file ~/.vim/ftplugin/vb.vim (Unix) or $HOME/vimfiles/ftplugin/vb.vim (Windows) with the script below. Create any directories, that do not exists and make sure, your .vimrc contains

filetype plugin indent on

The indent keyword is optional. Include it only, if your Vim should also source filetype specific indent scripts (if they exist).

The Folding Script[]

The folding script considers lines starting with '#Region' as beginning of folded regions and lines starting with '#End Region' as end markers for those folds:

fu! VBFold(lnum)
    if getline(a:lnum) =~# '^\s*#Region'
	return '>1'
    elseif getline(a:lnum) =~# '^\s*#End Region'
        return  '<1'
	return '='

fu! VBFoldText()
    return substitute(v:folddashes,'-',' ','g'). matchstr(getline(v:foldstart), '^\s*#Region\s\+\zs.*')

setl foldenable foldmethod=expr foldexpr=VBFold(v:lnum) foldtext=VBFoldText()