简体   繁体   中英

How can I fold YAML items in VIM?

I have a YAML formatted text file, and would like to define custom folding for VIM, but I'm not sure how to go about it (despite reading the VIM documentation for folding). The file consists of YAML "documents", like so:

---
title: My Title
attr1: value1
attr2: value2
---
title: Next Item
attr1: value3
---
title: One More Item
...

I would like the resulting folded text to look something like this:

+---- 2 lines: My Title ----
+---- ? lines: Next Item ---

Any suggestions are appreciated! Thanks!

Do

%s/---\(.*\)\(\_.\{-}title: \)\(.*\)/---\1 #{{{1 \3\2\3/g
set foldmethod=marker

or

%s/\(---\_.\{-}title: \)\(.*\)/#{{{1 \2\r\1\2/g
set foldmethod=marker

That will add comment with title to the beginning of every YAML document and leave document still valid. foldmarker option must be left untouched.

Result:

1.

--- #{{{1 My Title
title: My Title
attr1: value1
attr2: value2
--- #{{{1 Next Item
title: Next Item
attr1: value3
--- #{{{1 One More Item
title: One More Item
...

Folded:

+--  4 строк: --- My Title-----------------------------
+--  3 строк: --- Next Item----------------------------
+--  3 строк: --- One More Item------------------------

2.

#{{{1 My Title
---
title: My Title
attr1: value1
attr2: value2
#{{{1 Next Item
---
title: Next Item
attr1: value3
#{{{1 One More Item
---
title: One More Item
...

Folded:

+--  5 строк: My Title--------------------------------
+--  4 строк: Next Item-------------------------------
+--  4 строк: One More Item---------------------------

If you want to use a foldmethod other than "manual" all the time, add this line to your ~/.vimrc:
set foldmethod=foldoption

I would recommend using foldmethod=indent. This will fold based on any indent. Then if you change your input to include the indents where you want folds to happen. For instance if you change your input to

---
title: My Title
    other attrs: other values
---
title: Next Item
---
title: One More Item
...

It will fold as you described

Check out pedro's yaml-vim-plugin for improved folding that matches your exact request.

Read more about the plugin on pedro's blog or find it directly on github as pedrohdz/vim-yaml-folds

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: 来自博文的折叠 yaml 示例

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM