Folding is the code editor’s ability to let you hide/show some parts of the text, allowing you to focus on something specific and navigate thru your code structure with more ease.
Vim folding can be set up to group text (fold) based on different concepts:
fold-syntax option, and applying some configurations.
How to Set Up:
.vimrc file and add the following lines:
With the above configuration you’re:
- Setting up fold indication on the left side of your window, it helps you identify fold’s status and level.
- Telling vim to keep all folds opened when you start the file.
(To know more about the options for each one of these settings, type
:h <setting name>).
How to use it:
The actions are based on the command
z associated with the action option:
zc— close the fold (where your cursor is positioned)
zM—close all folds on current buffer
zo— open the fold (where your cursor is positioned)
zR— open all folds on current buffer
zj— cursor is moved to next fold
zk— cursor is moved to previous fold
On the left side of the window, you can see whenver a fold is opened/closed and it’s folding level.
Useful links & references:
- Vim Documentation (
- This tip was adapted from content found on “Mastering Vim Quickly” Book (and screencasts) by Jovica Ilic. It’s an awesome book, make sure you subscribe to the newsletter to get free Vim tips and book excerpts on your email.
(👆 This is an affiliate link. If you got any value from this post and are also interested in buying the MVQ book/screencasts, please consider visiting the affiliate link and you’ll be gifting me a coffee ☕ or a beer 🍺.)
- Follow me on Twitter to get more posts like this and other quick tips in your feed.
- alldrops.info is the TRUE and FOREVER FREE home for the previously used medium.com/vim-drops publication. At alldrops.info you’ll always find all posts without Medium.com restrictions.
- If you have any doubts or tips about this article, I’d appreciate knowing and discussing it via email.
- Do you have any other Vim tips? Would you like to publish that in this blog? Please send an email to vim drops.
- As English is not my native language, I apologize for the errors. Corrections are welcome.
- Contact: vim [@] alldrops [.] info.