2.8 KiB
| date | tags | ||
|---|---|---|---|
| 2025-03-21 |
|
#Vim #Learning
What is code folding?
Code folding is when you can take several lines of code and collapse them. Folding is super useful for situations where code takes up space when you do not need to see it for your current editing. Common cases are things like function definitions, for loops, or large tables.
How does Vim handle folding?
Folding Modes
Vim has several folding modes:
manual- you pick where folds goindent- fold by indented blockssyntax- fold by semantic markers in codeexpr- fold by user expressionmarker- fold based on Markers that are placed by a userdiff- used to fold unchanged text when using Views
For the most part, I'm just going to use manual mode. I know enough of the motions that I can get around easier, and plus since I write in a variety of languages things like indent or syntax might not be sufficient or expressive enough.
Creating and Deleting Folds
To create a fold, use zf{motion}, where, you guessed it, you can use
just about any of the Motions you like.
Folds can also be created using Visual mode. Select the text, then zf.
zd deletes folds
zD deletes folds recursively. So if you have folds within folds, it will
delete those too. If used in visual mode, it will delete all the folds
in the window.
zE will eliminate all folds in the current window.
Opening and Closing Folds
There are three main commands: za, zc, and zo. zc and zo
close and open folds respectively, and have a recursive cousin using
a capital letter instead of the lower case letter. za is different:
it is a toggle between open and closed. Recursively, it may be less useful
if there is a mixing of open and closed nested folds. zA will switch the
the folded status of all of them based on the parent fold (I think.)
There are additional commands for opening and closing folds that may be useful.
zm will fold 'more', where it will fold to a certain nested
level. zM will close all folds. zr will fold 'less', where it will reveal
more. zR will open all folds.
There are two commands that will also change the enabled-ness of folding.
zn will open all folds and disable folding, while zN will set all
folds back to how they were before, and enable folding again
Moving over folds
[z and ]z will move to the beginning and end of the current open fold,
or to the start / end of the parent fold from the child fold. zj and zk
will move to the next fold in that direction.zj and zk can also be used
with operators before hand (3zj is down 3 folds).
Editing and Commands on Folds
Closed folds act a lot like single lines with commands. dl for example will
not delete the current line if on a closed fold, it will delete everything in
the fold, period.