vim folding or: How I Learned to Stop Worrying and Love the Fold

Categories: General, Misc, screenshots

sup people

i wanted to write a quick article on why i love folds in vim especially in config files.

vim folding gives you the ability to temporarily hide parts of files while leaving only certain lines visible. this helps a ton when you are elbow deep in the muck that are certain files.

i will show you an example of unfolded config files then an example of it cleaned up with folds and also offer some explanations.

Unfolded tmux config:
Screen Shot 2016-01-27 at 13.59.21
Click to enlarge
As you can see above, its not bad but its just a ton of stuff that would require a bit of back and forth with searching and not really knowing where things are.
Here is the original .tmux.conf before cleaning up a bit in github.

Here is the folded tmux config:
Screen Shot 2016-01-27 at 14.02.54
Click to enlarge
Now what you see are defined sections that are a bunch easier to get back and forth on and its also hiding the cruft.
Here is the updated .tmux.conf after clean up.

Some the the folding magic is done here:

## Modeline and Notes {
# vim: set foldmarker={,} foldlevel=0 spell:
# }

The key bit being foldmarker which states that the folds occurs in the brackets. But since in this config file the brackets would make the conf file fail, i put the brackets after hash marks to comment them out.

pretty straight forward.

Then controlling the folds with Vim folding commands:

zf#j creates a fold from the cursor down # lines.
zf/string creates a fold from the cursor to string .
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.

this was taken from here

again, pretty straight forward.


    Leave a Reply

    Your email address will not be published. Required fields are marked *