Vim Tips Wiki

This tip is deprecated for the following reasons:

Syntax folding is a much better way to accomplish this. Many syntax files (including C and Perl) already define this folding, or you can make your own.

Tip 916 Printable Monobook Previous Next

created 2005 · complexity basic · author Ethan Mallove · version 6.0

Ever have a giant file with many function definitions you want to hide while you edit only a few functions? Folding (see :help :fold) is a great way to "shrink" a file down to size, while you concentrate on a few fragments of the file you're editing. Putting the following Vim function definition in a file (I always name it ".vimrc") in the same directory as the file you're editing, allows you to hide all the functions you want (provided you bracket using the style below, notice the {}'s on their own lines):

sub foo
  some code ...

To use it, source the file, then do :call HideFunctionDefs() to invoke the function, which will hide the bodies of the functions you don't care to see.

The following example hides foo1, foo2, foo3, and foo4.

function! HideFunctionDefs()
  if search("sub foo1") > 0
    normal! jv%zf
  if search("sub foo2") > 0
    normal! jv%zf
  if search("sub foo3") > 0
    normal! jv%zf
  if search("sub foo4") > 0
    normal! jv%zf

If you bracket like this:

sub foo {
  some code ...

Replace all instances of normal! jv%zf with normal! f{v%zf.

To hide more functions, add more if-endif blocks substituting sub foo_ with the names of the functions you want to hide.


You might find it easier to manage if you do something like:

:set foldmethod=marker
:set foldmarker={,}

Alternatively, consider passing in the bit after "sub " as a parameter to the function, and then simply call HideFunctionDefs( 'foo\d\+' ) -- you'll have to change HideFunctionDefs to go over all instances of the search, though, which means you should probably change your foldmethod to expr and set your foldexpr to another function that you write.

I fear you've been getting less-than-stellar reviews because it's too manual a process to fold away the functions using your technique.


Personally, I use the standard foldmarkers ({{{ and }}}) and place them in comments. This is much more flexible as it is language independent and you can better define what is folded. For example, you may have a block of code that is quite long, not necessarily a function, and you want to hide it. I've done this with long queries in ColdFusion pages.

Also, using the above suggestion, you can have a lot of nested folds since every time you have a {,} combination, it will fold it. This includes if statements, case statements, etc.

Perhaps it is worth mentioning zf<motion> as a convenient means of folding something. In particular I've found zfa{ to be convenient.

Perhaps :help :syn-fold will help you. Here is an extract:

:syn region myFold start="{" end="}" transparent fold
:syn sync fromstart
:set foldmethod=syntax

I'm using Vim to program C#. Visual Studio uses some markers as the following:

#region My Region

I'm using the .cs-Syntax file for Vim and defined my foldexpr so that all #regions are folded automagically. It's very useful especially if one is working in a team with others using Visual Studio.