What is folding?

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

What is folding?

Jeremy-5
I have seen messages and also help files regarding folding, but I am not
sure what it is.  The help files just mention how to do it, but not what
it is.  I am hoping that it will hide a section of the file, like a
section of code that I am not working on at the moment.  Can someone
help me understand what it is?
Thanks,
Jeremy

Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

John (Eljay) Love-Jensen
Hi Jeremy,

>What is folding?

It's cool stuff.  :-)

http://www.vim.org/htmldoc/fold.html

--Eljay

kae
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

kae
In reply to this post by Jeremy-5
Jeremy wrote:

> I have seen messages and also help files regarding folding, but I am
> not sure what it is.  The help files just mention how to do it, but
> not what it is.  I am hoping that it will hide a section of the file,
> like a section of code that I am not working on at the moment.  Can
> someone help me understand what it is?


That's exactly what it does (hide sections). It really is a great boost
to your coding experience! Try setting your folds to activate on not
only functions, but also loops  and conditionals.

Kae

Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

Tim Chase-2
In reply to this post by Jeremy-5
> I have seen messages and also help files regarding folding,
> but I am not sure what it is.  The help files just mention how
> to do it, but not what it is.  I am hoping that it will hide a
> section of the file, like a section of code that I am not
> working on at the moment.  Can someone help me understand what

You are right...folding will collapse multiple lines into a
single line.  Very handy for nested text, such as most code, or
HTML/XML.

Vim supports multiple methods of folding.  It should default to
manual folding, which means you manaully mark areas to be
folded/collapsed.  Vim also supports syntax folding (as defined
by the syntax highlighting file), folding simply by the
indentation levels, explicit folding markers, etc. (see ":help
foldmethod" for more info)

Much of what you need can be found in the help at

        :help fold.txt

So for a starter, you can highlight the sections of your file you
want to fold/collapse, and use

        zf

to create the fold (and close it).  You can then use "zo" to open
and "zc" to close the folds you've created.

-tim







Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

Jeremy-5
In reply to this post by John (Eljay) Love-Jensen
Eljay Love-Jensen wrote:

> Hi Jeremy,
>
>
>>What is folding?
>
>
> It's cool stuff.  :-)
>
> http://www.vim.org/htmldoc/fold.html
>
> --Eljay
>
>
Thanks for that link.  I have seen it before (both online and in the
help files), but for some reason I didn't check out the link to Chapter
28 of the user manual where it mentions what folding actually does.
Thanks,
Jeremy

Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

A.J.Mechelynck
In reply to this post by Jeremy-5
----- Original Message -----
From: "Jeremy" <[hidden email]>
To: <[hidden email]>
Sent: Thursday, July 14, 2005 5:04 PM
Subject: What is folding?


>I have seen messages and also help files regarding folding, but I am not
>sure what it is.  The help files just mention how to do it, but not what it
>is.  I am hoping that it will hide a section of the file, like a section of
>code that I am not working on at the moment.  Can someone help me
>understand what it is?
> Thanks,
> Jeremy

"Folding" is a way to "fold away" a part of a file, so that Vim doesn't
display it (or rather, displays only its first line), even though it still
exists in the file. The helpfile "fold.txt" explains the folding methods
(what folds depend on), and the commands that apply to them. You may think
of a "fold" in a file as analogous to a fold in a plaited skirt: the fabric
is still there, but it is folded away from view.

Folds can be nested, for instance you may define them in C code so that each
{} block is a fold.

You may want to try playing around with the commands described in fold.txt:
start, for instance, with "manual" folding so you can define folds, hide
them away (i.e., "close" them), make them reappear ("open" them) etc. Then
you can try finding a file with folds predefined in it (using "marker"
folding and a modeline) or, if you think you are starting to understand the
mechanism, try to create one yourself. If you use Python, or "properly"
indented code, I believe you will see some good examples of "indent"
folding. For some languages, the Vim syntax script supports "syntax" folding
(i.e., depending on syntax features, e.g., on { and } in C code). And so on.
Don't be afraid of "hands-on" experimentation.

HTH,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

Jeremy-5
In reply to this post by Jeremy-5
Now that I have had a chance to play around with folding, I really like
it.  Just what I was looking for.  Now I have but two wants.  Is there a
syntax file available for Python that has folding in it?  I know I can
do folding by indent, but I don't want all my if statements folded, just
my functions.  Secondly, how can I change the color of the folded lines?
  The white bar in the vim window is too blaring for my likes.
Thanks,
Jeremy

Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

A.J.Mechelynck
----- Original Message -----
From: "Jeremy" <[hidden email]>
To: <[hidden email]>
Sent: Thursday, July 14, 2005 6:00 PM
Subject: Re: What is folding?


> Now that I have had a chance to play around with folding, I really like
> it.  Just what I was looking for.  Now I have but two wants.  Is there a
> syntax file available for Python that has folding in it?  I know I can do
> folding by indent, but I don't want all my if statements folded, just my
> functions.

I'm not sure. You might try taking a look at $VIMRUNTIME/syntax/python.vim

>  Secondly, how can I change the color of the folded lines? The white bar
> in the vim window is too blaring for my likes.

Change the highlight for the Folded group (":hi Folded" will show you what
it is set to; or add settings to change them). For instance to use nonbright
blue on cyan in console Vim:

        :hi Folded ctermbg=darkcyan ctermfg=darkblue

> Thanks,
> Jeremy

My pleasure,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

John (Eljay) Love-Jensen
Hi Tony & other Vim gurus,

>Change the highlight for the Folded group (":hi Folded" will show you what it is set to; or add settings to change them). For instance to use nonbright blue on cyan in console Vim:

>       :hi Folded ctermbg=darkcyan ctermfg=darkblue

Silly fold question (which Jeremy may be interested in as well).

When I fold this:

void foo()
{
  ...yada yada yada...
}

Into this:

--+ 13 lines: void foo()

I would MUCH rather have...

void foo() <FOLD:13>

Where the "void foo() " part retains its normal beautiful syntactic colorization, and the <FOLD:13> part abides by Folded.

Is that possible?

Thanks,
--Eljay

Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

Matt Foster
Quoting Eljay Love-Jensen ([hidden email]):

> Hi Tony & other Vim gurus,
>
> >Change the highlight for the Folded group (":hi Folded" will show you what it is set to; or add settings to change them). For instance to use nonbright blue on cyan in console Vim:
>
> >       :hi Folded ctermbg=darkcyan ctermfg=darkblue
>
> Silly fold question (which Jeremy may be interested in as well).
>
> When I fold this:
>
> void foo()
> {
>   ...yada yada yada...
> }
>
> Into this:
>
> --+ 13 lines: void foo()
>
> I would MUCH rather have...
>
> void foo() <FOLD:13>
>
> Where the "void foo() " part retains its normal beautiful syntactic colorization, and the <FOLD:13> part abides by Folded.
>
> Is that possible?
I think it is, see: help foldtext. I haven't got an example though.

Cheers,

Matt

--
Time exists so that everything doesn't happen at once,
space exists so that everything doesn't happen to you.
Matt Foster | http://www.mattfoster.clara.co.uk 


signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

A.J.Mechelynck
In reply to this post by John (Eljay) Love-Jensen
----- Original Message -----
From: "Eljay Love-Jensen" <[hidden email]>
To: "Tony Mechelynck" <[hidden email]>; <[hidden email]>
Sent: Thursday, July 14, 2005 6:36 PM
Subject: Re: What is folding?


> Hi Tony & other Vim gurus,
>
>>Change the highlight for the Folded group (":hi Folded" will show you what
>>it is set to; or add settings to change them). For instance to use
>>nonbright blue on cyan in console Vim:
>
>>       :hi Folded ctermbg=darkcyan ctermfg=darkblue
>
> Silly fold question (which Jeremy may be interested in as well).
>
> When I fold this:
>
> void foo()
> {
>  ...yada yada yada...
> }
>
> Into this:
>
> --+ 13 lines: void foo()
>
> I would MUCH rather have...
>
> void foo() <FOLD:13>
>
> Where the "void foo() " part retains its normal beautiful syntactic
> colorization, and the <FOLD:13> part abides by Folded.
>
> Is that possible?
>
> Thanks,
> --Eljay

Not that I know of -- but I don't know everything, and folding is not my
forte.

Best regards,
Tony.


kae
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

kae
In reply to this post by John (Eljay) Love-Jensen
Eljay Love-Jensen wrote:

>--+ 13 lines: void foo()
>
>I would MUCH rather have...
>
>void foo() <FOLD:13>
>
>Where the "void foo() " part retains its normal beautiful syntactic colorization, and the <FOLD:13> part abides by Folded.
>  
>
:set foldtext=getline(v:foldstart).'\ <'.((v:foldend)-(v:foldstart)).'\ lines>'


gives you the syntax anyway...

Kae
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

John (Eljay) Love-Jensen
Hi Kae,

>gives you the syntax anyway...

Thanks for the trick!

#define SOAPBOX

Too bad coloring is lost.  The most annoying thing about folding is its interruption due to loss of syntactic colorization and instead using hi Folded for the entire line (instead of the fold portion, such as the <FOLD:n> postfix annotation I prefer).  Besides, :set fdc=8 does all I need for visualization of folds -- no need to disrupt the continuity of the text with a "HEY!  THERE'S A FOLD HERE!" highlighting.

#undef SOAPBOX

Hmm, leading tabs are disappearing.  Is there a way to retain the leading tab (leading whitespace)?  Including honoring listchars settings for tab representation?

Sincerely,
--Eljay

kae
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

kae
Eljay Love-Jensen wrote:

>Hmm, leading tabs are disappearing.  Is there a way to retain the leading tab (leading whitespace)?  Including honoring listchars settings for tab representation?
>  
>
this should do it:

:set foldtext=substitute(getline(v:foldstart),'\ ','\ \ ','g').'\
<FOLD:'.((v:foldend)-(v:foldstart)).'>'

Kae
kae
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

kae
kae verens wrote:

>:set foldtext=substitute(getline(v:foldstart),'\ ','\ \ ','g').'\
><FOLD:'.((v:foldend)-(v:foldstart)).'>'
>  
>

sorry... should all be on one line

Kae
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

John (Eljay) Love-Jensen
Hi Kae,

Thanks again!  That gives me ideas.

I think I can rig up something to incorporate the &tabstop value and retain non-boundary tabs correctly.  But I'll probably have to do it with a function instead of a one liner.  My one-liner-vim-fu isn't that strong.

Sincerely,
--Eljay

Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

John L. Clark
On Thu, Jul 14, 2005 at 02:29:17PM -0500, Eljay Love-Jensen wrote:
> Thanks again!  That gives me ideas.
>
> I think I can rig up something to incorporate the &tabstop value and
> retain non-boundary tabs correctly.  But I'll probably have to do it
> with a function instead of a one liner.  My one-liner-vim-fu isn't
> that strong.

I'd definitely be interested in seeing the results of your work (at some
indefinite point in the future), if you needed any encouragement to
share with the list when you're done.  :)

Take care,

    John

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

A.J.Mechelynck
In reply to this post by John (Eljay) Love-Jensen
----- Original Message -----
From: "Eljay Love-Jensen" <[hidden email]>
To: "kae verens" <[hidden email]>
Cc: "Tony Mechelynck" <[hidden email]>; <[hidden email]>
Sent: Thursday, July 14, 2005 8:07 PM
Subject: Re: What is folding?


> Hi Kae,
>
>>gives you the syntax anyway...
>
> Thanks for the trick!
>
> #define SOAPBOX
>
> Too bad coloring is lost.  The most annoying thing about folding is its
> interruption due to loss of syntactic colorization and instead using hi
> Folded for the entire line (instead of the fold portion, such as the
> <FOLD:n> postfix annotation I prefer).  Besides, :set fdc=8 does all I
> need for visualization of folds -- no need to disrupt the continuity of
> the text with a "HEY!  THERE'S A FOLD HERE!" highlighting.
>
> #undef SOAPBOX
>
> Hmm, leading tabs are disappearing.  Is there a way to retain the leading
> tab (leading whitespace)?  Including honoring listchars settings for tab
> representation?
>
> Sincerely,
> --Eljay

About colouring: try ":hi Folded term=NONE cterm=NONE ctermfg=NONE
ctermbg=NONE gui=NONE guibg=NONE guifg=NONE"

I think that in Vim 7 (but not in Vim 6) it will make the fold colouring
"transparent", showing any underlying syntax colouring. But I'm not sure it
works: it might show the "default" colors instead (the same as for the
Normal group).

Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

John (Eljay) Love-Jensen
Hi Tony,

I tried your suggestion.

It appears that Vim pulls the folded line completely out of the syntactic parsing, so that it no longer participates in the regular syntactic coloring (unlike search highlighting).  Rather, the line that is displayed is drawn from the foldtext setting, and interjected onto the display (and colored via Folded).

I've seen the "pass-thru" or "fall-back" trick work for highlighting in other situations though, so the general concept is a good one to keep in mind.

Sincerely,
--Eljay

Reply | Threaded
Open this post in threaded view
|

Re: What is folding?

John (Eljay) Love-Jensen
In reply to this post by John L. Clark
Hi John,

>I'd definitely be interested in seeing the results of your work (at some indefinite point in the future), if you needed any encouragement to share with the list when you're done.  :)

Here's what I came up with for my ~/.vimrc file.

WARNING:  I suspect this is egregiously and horribly suboptimal, and there are probably better ways to extract portions of a string, increment variables, and append characters to a string.  I did it the "brain dead simple" straightforward newbie way.  And I neglected using <SID> and such (bad Eljay, no biscuit).

Note to self:  string concatenation is the . operator.  Not the + operator.  Repeat three times, then tap my heels.  And there is a big big big difference between "\t" and '\t'.

I've also put in my preferences for my fold settings, just as a FYI reference.  YMMV.

" ----------------------------------------------------------------
" Function for sensibly expanding tabs in the
" displayed headline of a folded text block
" ----------------------------------------------------------------
set fdc=8
set fdl=99
set fdm=indent
set foldtext=MyFoldText().'\ <FOLD:'.((v:foldend)-(v:foldstart)+1).'>'
function MyFoldText()
  let lcstab = "  "
  if substitute(&listchars, '^.*\(tab:\).*$', '\1', "") == "tab:"
    let lcstab = substitute(&listchars, '^.*tab:\(..\).*$', '\1', "")
  endif

  let ix = 0
  let line = getline(v:foldstart)
  let nx = 0
  let newline = ""

  while ix < strlen(line)
    if line[ix] == "\t"
      let newline = newline.lcstab[0]
      let nx = nx + 1
      while nx % &tabstop
        let newline = newline.lcstab[1]
        let nx = nx + 1
      endwhile
    else
      let newline = newline.line[ix]
      let nx = nx + 1
    endif
    let ix = ix + 1
  endwhile

  return newline
endfunction
" ----------------------------------------------------------------

Sincerely,
--Eljay

You know you use Vim too much when you have this alias in your ~/.bashrc file:
alias :e=/bin/vim

12