Vim Tips Wiki
m (Category: Folding)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{Deprecated|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.}}
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=916
 
|id=916
  +
|previous=915
|title=Expose only what you're editing
 
  +
|next=917
|created=April 18, 2005 14:53
+
|created=2005
 
|complexity=basic
 
|complexity=basic
 
|author=Ethan Mallove
 
|author=Ethan Mallove
 
|version=6.0
 
|version=6.0
 
|rating=-3/12
 
|rating=-3/12
  +
|category1=Folding
|text=
 
  +
|category2=
Ever have a giant file with many function definitions you want to hide while you edit only a few functions? Folding (see :h 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):
 
 
}}
 
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):
   
  +
<pre>
 
sub foo
 
{
 
some code ...
 
}
  +
</pre>
   
 
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.
sub foo
 
   
  +
<pre>
{
 
 
function! HideFunctionDefs()
 
if search("sub foo1") > 0
 
normal! jv%zf
  +
endif
 
if search("sub foo2") > 0
 
normal! jv%zf
  +
endif
 
if search("sub foo3") > 0
 
normal! jv%zf
  +
endif
 
if search("sub foo4") > 0
 
normal! jv%zf
  +
endif
 
endfunction
  +
</pre>
   
 
If you bracket like this:
some code ...
 
   
  +
<pre>
}
 
 
sub foo {
 
some code ...
 
}
  +
</pre>
   
 
Replace all instances of <code>normal! jv%zf</code> with <code>normal! f{v%zf</code>.
   
 
To hide more functions, add more if-endif blocks substituting <code>sub foo_</code> with the names of the functions you want to hide.
   
 
==Comments==
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.
 
 
You might find it easier to manage if you do something like:
   
  +
<pre>
 
:set foldmethod=marker
 
:set foldmarker={,}
  +
</pre>
   
 
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.
The following example hides foo1, foo2, foo3, and foo4.
 
   
 
 
function! HideFunctionDefs()
 
 
if search("sub foo1") &gt; 0
 
 
normal jv%zf
 
 
endif
 
 
if search("sub foo2") &gt; 0
 
 
normal jv%zf
 
 
endif
 
 
if search("sub foo3") &gt; 0
 
 
normal jv%zf
 
 
endif
 
 
if search("sub foo4") &gt; 0
 
 
normal jv%zf
 
 
endif
 
 
endfunction
 
 
 
 
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.
 
}}
 
 
== Comments ==
 
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...
 
 
salmanhalim--AT--hotmail.com
 
, April 19, 2005 7:23
 
 
----
 
----
  +
<pre>
 
g/^sub/.,/^}/fo
 
g/^sub/.,/^}/fo
  +
</pre>
   
no--AT--mail.aa
 
, April 20, 2005 4:03
 
 
----
 
----
Personnally, 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, but necessarily a function, and you want to hide it. I've done this with long queries in ColdFusion pages.
+
Personally, I use the standard foldmarkers <nowiki>({{{ and }}})</nowiki> 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.
 
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.
   
'''Anonymous'''
 
, April 21, 2005 20:44
 
 
----
 
----
Perhaps it is worth mentioning zf&lt;motion&gt; as a convent means of folding
+
Perhaps it is worth mentioning <code>zf<motion></code> as a convenient means of folding something. In particular I've found zfa{ to be convenient.
something; In particular I�ve found zfa{ to be convent.
 
   
 
sreny--AT--srenyqernzf.pbz (Rot13ed)
 
, April 22, 2005 2:39
 
 
----
 
----
Perhaps, "help :syn-fold" will help you. Here is extract from there:
+
Perhaps {{help|:syn-fold}} will help you. Here is an extract:
:syn region myFold start="{" end="}" transparent fold
 
:syn sync fromstart
 
:set foldmethod=syntax
 
   
  +
<pre>
 
:syn region myFold start="{" end="}" transparent fold
 
:syn sync fromstart
 
:set foldmethod=syntax
  +
</pre>
   
Ivan Tishchenko
 
, April 22, 2005 2:57
 
 
----
 
----
I'm using VIM to program C&#35;. Visual Studio uses some markers as the following:
+
I'm using Vim to program C#. Visual Studio uses some markers as the following:
   
  +
<pre>
&#35;region My Region
+
#region My Region
..code..
+
..code..
&#35;endregion
+
#endregion
  +
</pre>
   
I'm using the .cs-Syntax file for VIM and defined my foldexpr so that all &#35;regions are folded automagically.
+
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.
It's very useful especially if one is working in a team with others using Visual Studio.
 
 
E Jacobs
 
, April 25, 2005 7:43
 
----
 
tpop: related to show what you edit, how about highlight lines w/ changes:
 
Is there any way to "color bar" what you have edited... many editors like old CodeWright
 
had a "changed line marker" which colors the left edge by lines that you modified or added/deleted.
 
 
Have you guys/gals seen anything like that for VIM?
 
 
 
'''Anonymous'''
 
, April 28, 2005 0:19
 
----
 
May be you'll try vimdiff? It help greatly, especially if you have your sources (or whatever you edit) in some repository. Like CVS.
 
 
Ivan Tishchenko
 
, April 28, 2005 23:17
 
----
 
Thanks. Yes vm's visual differ is great! But I'm talking about something else. Its hard to describe
 
...so I searched the web to see how the Codewright editor described it:
 
 
That "Mark Changed Lines" feature was pretty nice. It colored the left margin with a color band on changed lines.
 
And had options to reset (or not) when you saved the file depending on your desired behavior.
 
 
Google found it on --AT--url: http://www.codewright.com/support/faq.asp
 
 
__________________________________________________
 
Q: 41. Is there a way to highlight changed code in your document?
 
 
A: Yes. You can set this up by file type or for individual files:
 
 
* By file type: The Tools|Customize|Language dialog allows you to set the option for a specific file type (e.g. .C, .TXT, .HTML, etc). Highlight the desired file type, select the Coloring tab, then mark the box Changed Lines to change the color of the entire changed line in a document. Also mark Language Dependent ChromaCoding to display change bars only in the left margin of the document.
 
* For individual files: The Document/Window|Manager|Display dialog allows you to set the option for individual files only. Highlight the desired file, then select the option Mark Changed Lines to display change bars in the left margin.
 
 
 
 
-tpop
 
 
'''Anonymous'''
 
, April 30, 2005 15:00
 
----
 
Folding is great in Vim, but I cannot force Vim to fold &#35;regions and methods in a way .NET does it. Can you mail me your private .cs syntax files with improved foldings? That would help me a lot. Thanks. tomasz2k at poczta dot onet dot pl
 
 
Tom
 
 
tomasz2k--AT--poczta.onet.pl
 
, June 28, 2005 2:52
 
----
 
I would also like to see how the &#35;region &#35;endregion folding is done
 
(and I bet there are more people here who would like to see that, so maybe you should just post it here)
 
   
3david--AT--gmail.com
 
, July 16, 2006 6:39
 
 
----
 
----
<!-- parsed by vimtips.py in 1.249642 seconds-->
 
[[Category:Folding]]
 

Latest revision as of 05:57, 13 July 2012

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
  endif
  if search("sub foo2") > 0
    normal! jv%zf
  endif
  if search("sub foo3") > 0
    normal! jv%zf
  endif
  if search("sub foo4") > 0
    normal! jv%zf
  endif
endfunction

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.

Comments[]

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.


g/^sub/.,/^}/fo

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
  ..code..
#endregion

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.