The default Vim folding rules for YAML files were always a bit of an eye strain for me. The folding commands do not behave as one would expect them to as well. More about all this in the Explanation section below.
This got me to throw together a quick and simple Vim plugin to handle YAML folding more cleanly, vim-yaml-folds. Here is what YAML folding looks like with vim-yaml-folds installed:
A fold contains the beginning of a YAML section with everything underneath it included.
If you know what I'm talking about and do not care for an explanation, feel free to skip the Explanation and install vim-yaml-folds if you would like.
Explanation
Here is what it looks like YAML folding looks like with the default Vim configuration:
The most obvious issue is the format of the fold lines. Take a look at the following line:
Starting with the plus symbol (+
) followed by a bunch of dashes (-
) then
the number of lines just looks like noise. Also, shouldn't it just be folded
up into the test_self
section? Same should apply to list of rules
.
The default folding behaviour for YAML files in Vim is that the folding
level matches the indentation level of the line. What ends up happening is
that the folding level begins on the line following the start of a YAML
block. This default folding rules can lead to some unexpected behaviour. Say
you move the cursor to the test_self:
and try and fold everything under it
(zc
). What actually happens is that everything under secgroups
gets
folded.
vim-yaml-folds is an attempt at cleaning all this up.
Further reading
- Guides to utilizing folds in Vim:
- How to implement folding rules in Vim:
- Vim fold documentation
- vim-yaml-folds Vim Scripts listing