ruamel.yaml is possible with
$ pip install ruamel.yaml
ruamel.yaml is python3 compatible, i.e.
pip3 install ruamel.yaml works as well.
If you want to install it only in your home directory, use
pip install --user ruamel.yaml.
import ruamel.yaml works for the python version vim sees.
equalprg of vim for yaml files with
autocmd FileType yaml setlocal equalprg=python\ -c\ \'import\ sys;\ from\ ruamel.yaml\ import\ YAML;yaml=YAML();yaml.dump(yaml.load(sys.stdin),sys.stdout)\'
Alternatively, place following line in
setlocal equalprg=python\ -c\ \'import\ sys;\ from\ ruamel.yaml\ import\ YAML;yaml=YAML();yaml.dump(yaml.load(sys.stdin),sys.stdout)\'
Now you should be able to press
=ip for your example to get the desired output.
I have tried it on my machine with python3 and it works.
Note you could also use
setlocal equalprg=python\ -c\ \'import\ yaml,sys;print(yaml.dump(yaml.safe_load(sys.stdin),default_flow_style=False))\'
pyyaml changes order, removes comments and other things.
ruamel.yaml explicitly tries to support the roundtrip of parsing and emitting without modifying these things.
There is also the project
pretty-yaml with a convenient command line interface
$ python -m pyaml demo.yaml
Note the unfortunate similarity of the name
pyyaml. The author has made the decision that lists should be indented and therefore does not help you:
- List items get indented, as they should be.
Finally, you could contact the maintainer of
$VIMRUNTIME/indent/yaml.vim to ask him whether he would consider to add an option to achieve your style:
" Vim indent file
" Language: YAML
" Maintainer: Nikolai Pavlov <firstname.lastname@example.org>
" Last Change: 2017 Jun 13