Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

I've rolled up a fold plugin for C and C++. It goes beyond what is done with syntax folding (may be it could be improved, I don't know), and leaves less noisy and not really useful things unfolded, compared to indentation and marker based folding.

The caveat: in order to have decent reaction times, I had to make some simplifications, and sometimes the result is quite messed-up (we have to type zx to fix it).

Here is a little screencast to see how the plugin folds a correctly balanced C++ source code, which is not currently being modified :(

enter image description here

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

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.

Actually, there is another very straight forward and effective way, which is using foldmethod = marker and set foldmarker to be {,}. Then the fold result would looks like:

  1. all of the functions fold-ed. Basically, it looks like the outline in IDE. (and you can also set foldlevel=1or more, if you do not want to fold everything at the beginning)

enter image description here

  1. this is what a normal function looks like when you open it with level-1 via zo.

enter image description here

In addition, to do folding by syntax needs a bit of extra work, and here is a good tutorial about it. But I think fold by marker={,} is quite enough, and most importantly, it's simple and neat.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top