Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fold YAML items in VIM?

Tags:

vim

yaml

folding

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!

like image 879
wkranec Avatar asked Jul 09 '10 11:07

wkranec


People also ask

How do I create a fold in Vim?

Vim uses the same movement commands to define folds. Folding also works in visual mode. If you enter visual mode using v or V , then select a few lines of text using the movement keys, and type zf , Vim will create a fold comprising those lines.

How do I save a fold in Vim?

The problem is that when you close Vim, your artfully folded code returns to its unfolded state. The solution is quite simple - when you are ready to save your folds run the :mkview command. This will save your folds in the current buffer to your viewdir ( :h viewdir ) depending on your environment.

How do I expand all lines in Vim?

To expand the lines, put the cursor over the fold and hit spacebar (in vim terminology, this deletes the fold). (Side note: you may want to change the look of collapsed folds.


2 Answers

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---------------------------
like image 53
ZyX Avatar answered Sep 21 '22 13:09

ZyX


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

like image 33
Eric LaForce Avatar answered Sep 19 '22 13:09

Eric LaForce