Wikia

Vim Tips Wiki

Changes: Folding like in Visual Basic .NET

Edit

Back to page

(CleanUp)
(remove dodgy category)
 
Line 11: Line 11:
 
|category2=LanguageSpecific
 
|category2=LanguageSpecific
 
}}
 
}}
{{dodgy|This functionality ''ought to be'' covered by syntax folding files. See [[Folding|Tip 108: Folding]] for more information. However it appears the vb.vim syntax file lacks any folding information. It would best be handled with an extension to or an alternate syntax script, not this awkward method.}}
 
 
 
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 {{help|prefix=no|fold-expr|label=fold expression}}.
 
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 {{help|prefix=no|fold-expr|label=fold expression}}.
   

Latest revision as of 14:06, August 14, 2012

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 ScriptEdit

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'
    else
	return '='
    endif
endfu

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

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

Around Wikia's network

Random Wiki