Bryan Says...

Rants, opinions, and other stuff I find interesting.

Code folding in vim

I’ve been pushing off learning code folding in vim for a few weeks now. I ended that procrastination and I present to you a short tutorial on code folding in vim.

Code folding in vim from Bryan Liles on Vimeo.

The import things to remember here are to get your settings correct:

"folding settings
set foldmethod=indent   "fold based on indent
set foldnestmax=10      "deepest fold is 10 levels
set nofoldenable        "dont fold by default
set foldlevel=1         "this is just what i use

Then you can toggle folding with za. You can fold everything with zM and unfold everything with zR. zm and zr can be used to get those folds just right. Always remember the almighty help file at “help :folding” if you get stuck.

Happy hacking

  • nap

    stop showing off… you and your scala code :)

  • lian

    thanks for sharing the snippet. the fold-everything and unfold from there sound usefull..

  • Evan Light

    I was working on some hairy code in VIM, was thinking “I could really go for some code folding here”, and remembered seeing your article in Google Reader. Saved me some yak shaving. Thanks!

  • mitjai

    It's amusing how pretty much any VIm related post is certain to end with “help :whatever”. Not criticising but rather admiring the built-in help system.

  • Praise Bob

    Thanks for the folding info ;)

  • Amit

    Great one. Thanks.

  • Aleksey V. Zapparov AKA ixti

    Thanks for your tip!

  • skrat

    Thanks for the video, very helpful! Your wish has been granted, it helped me.

  • n0dl

    Thanks this helped out a lot, I no longer have to scroll through a mountain of text to get the peice of code I want

  • Rick R


  • Tanel

    thanks, very helpful!

  • Dimrirahul

    you Rock .. m/ thanks

  • Waseem

    I am using from past four years yet it still does not fail to surprise me. Thanks to people like you.

  • Hian-Kun Tenn

    Thank you for the post and setting examples. It’s simple and useful.

  • David Rivers

    Does anyone know what are the semantics of these “z” commands? It always helps me to remember the commands if I have mnemonics.

  • bryanl

    per the docs, (:help fold-commands), ‘All folding commands start with “z”. Hint: the “z” looks like a folded piece of paper, if you look at it from the side.’

    I hope this helps.

  • David Rivers

    Very helpful. Thanks! I suppose the “m” can be likened to “minimize”, but I’m not sure about the “r”.

  • Joel Holder

     Thanks..  this is very helpful.  Is there a reason you prefer 
    set foldmethod=indent instead of set foldmethod=syntax? Trying to learn. Thanks..

  • Guest

    “r” as in Restore

  • sLim

    Thanks a lot , i have been wondering how to do this, finally got it with perfection

  • Test


  • Ray Rashif

    Thanks for this. I’ve been a long-time vim user but never tried code folding, which I missed from IDEs and others editors like Kate.

    Anyway, more importantly (for me), what colour scheme is that? If it’s custom, could I have the vim file for it? Thanks!

  • imissmyjuno

    I think that’s xoria256 but I’m not a 100% sure.

  • A11142176

    Thanks a lot. This really helps!

  • Alex Lee

    Thanks for the tip!

  • Keoghsie

    Thanks, this helped a lot!

  • Minh L

    Thanks for the tip. Also, I noticed that you can click your mouse in vim, and also select a block of text using just your mouse. Can you show me what you put in .vimrc to do that? Thanks so much.

  • bryanl

    I thought that was a MacVim thing.

  • Minh L

    Oh i though you were using Vim from Mac’s terminal

  • Jl353

    set mouse=a

  • Abhishek Srivastava


  • Brian Knoles

    Nice video!  Thanks!

  • Zach Morek

    For anyone googling and getting here…

    Some common key bindings:
    `za` – toggles

    `zc` – closes

    `zo` – opens

    `zR` – open all

    `zM` – close all

  • Rahul Kumar

    thanks, simple to the point

  • KHS

    Thank you… its nice to know vim has everything (nearly) a programmer needs… :)

  • Anthony DiSanti

    I don’t think your foldlevel setting is doing anything if you have nofoldenable set. Also, you may want to look at foldlevelstart as that won’t clobber modeline settings. Thanks for the video, good stuff.

  • Terry Shuttleworth

    Nice. Thank you

  • Casey Driscoll

    per the docs >> :help usr_28

    |zm| = fold More
    |zr| = fold Reduce

  • David Becerra 

    Thank you so much! This helps greatly :)

  • Fear Of A Black Planet

    Dearest Brother @bryanl, Thank you for the GREAT post about code folding in Vim, VERY useful!! It is NOT possible to beg for respect, you can only COMMAND it; “I don’t want acceptance, I want to belong”? Related to what??!?!!? (The Last Poets). And to quote another GREAT group: “Free Your Mind… and Your Ass Will Follow” (Funkadelic). BLACK POWER starts from within. @hotrodimusconvo

  • harish

    I love this feature in kate. And I used to wonder why vim doen’t have this feature. Today I got the answer. Thank You.