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:

View of YAML folding with new folding rules

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:

View of Vim's default YAML folding rules

The most obvious issue is the format of the fold lines. Take a look at the following line:

Line 11 of YAML folding example

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