tags:

views:

566

answers:

4

I regularly run into C-codes without folding. It is irritating to read them if there is no folding, particularly with long files. How can I fold them?

+1  A: 

In vi (as opposed to vim) the answer was:

:set wm=1

This sets the wrap margin to one character before the end of the line. This isn't the world's best specification with variable sized windows (it made sense with green screens when it was hard to change the size).

That means there is also an alternative way to do it in vim:

:set textwidth=30

See: VimDoc User Manual Section 25.1

Jonathan Leffler
Good tips, but I think OP is asking about code folding, not line wrapping.
ephemient
OK; 'code folding' is not a term I'd come across before, so I'd accept the amendment.
Jonathan Leffler
+3  A: 

I think you may have mixed the terminology. Do you need "wrapping" or "folding". Wrapping is the one where lines that wouldn't usually fit on screen due to their length, are wrapped, i.e. shown on several consecutive lines on screen (actually, it is one line, in several lines - hard to explain, best to see in practice).

In vim wrapping is set by

:set wrap

to turn it on, and

:set textwidth=80

to determine where vim should wrap the text (80 characters is usually a nice measure).

Folding on the other hand is a completely different matter. It is the one where vim folds several lines of code (for example, a function) into one line of code. It is useful for increasing readability of code. Vim has several folding methods, you can see all of them if you

:help folding

What you are looking for, I think would be, syntax folding, but I could be wrong. I recommend reading the help page, it is not long, and very useful.

ldigas
+7  A: 

To fold according to syntax

:set foldmethod=syntax

If you want to do it manually on the bits you want to fold away

:set foldmethod=manual

then create new folds by selecting / moving and pressing zf e.g.

shift-v j j zf

(ignoring the spaces)

Edit: Also see the comments of this answer for indent and marker foldmethods.

Andy
There is an indent folding mode too which I like because it works on most everything.
Zan Lynx
Also worth mentioning is foldmethod=marker. I use this and manually tag sections with {{{ and }}} in comments when I want to ignore large blocks.
Dan Olson
A: 

The you probably want the setting

:set foldmethod=syntax

But don't put that in manually! Thats missing out on one of Vims biggest features which is having custom settings for hundreds of file types already builtin. To get that, add this to your ~/.vimrc

filetype plugin on
filetype indent on

filetype detection is mostly based on extension, in this case *.c files. See :help :filetype for more info. You can also customize these filetype based settings.

Ian Kelling