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?
6 Answers
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.

- 3,794
- 24
- 28
-
1There is an indent folding mode too which I like because it works on most everything. – Zan Lynx Apr 17 '09 at 22:01
-
2Also 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 Apr 17 '09 at 22:08
-
1BTW, `zM` closes all open folds and `zR` opens all folds. (Handy to have those listed here too) – Sebastián Grignoli Aug 30 '11 at 22:41
-
1If you have the Vim documentation, try `help z` and `help zf` for a full explanation of folding. +1 – Keith Pinson Jul 02 '12 at 15:54
-
What do you think about Luc Hermitte's proposal in the answer? – Léo Léopold Hertz 준영 Mar 16 '17 at 15:53
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.

- 60,248
- 49
- 165
- 242
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:
- all of the functions fold-ed. Basically, it looks like the outline in IDE. (and you can also
set foldlevel=1
or more, if you do not want to fold everything at the beginning)
- this is what a normal function looks like when you open it with level-1 via
zo
.
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.

- 2,930
- 2
- 22
- 32
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 :(

- 31,979
- 7
- 69
- 83
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

- 730,956
- 141
- 904
- 1,278
-
1Good tips, but I think OP is asking about code folding, not line wrapping. – ephemient Apr 17 '09 at 22:06
-
OK; 'code folding' is not a term I'd come across before, so I'd accept the amendment. – Jonathan Leffler Apr 18 '09 at 04:46
-
What do you think about Luc Hermitte's proposal in the new answer? – Léo Léopold Hertz 준영 Mar 16 '17 at 15:53
-
1@LéoLéopoldHertz준영: I have no view on it whatsoever. I've not used the plugin, nor do I have any plans to do so. – Jonathan Leffler Mar 16 '17 at 16:51
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.

- 9,643
- 9
- 35
- 39