0

I'm trying to work with indenting yaml in vim, and I'm trying to convert it to JSON.

Say I have a file like this:

---
  foo: bar
  baz:
    - qux
    - quxx
  corge: null
  grault: 1
  garply: true
  waldo: "false"
  fred: undefined
  literal_block: |
   blah wee  `bib` 
   blah
  # Multiple-line strings can be written either as a 'literal block' (using |),
  # or a 'folded block' (using '>').
  test: blah

The literal block part (as well as the indentation overall) are giving me a hard time.

When typing a literal_block I usually start off with all the content un-indented.

  literal_block: |
blah wee  `bib` 
blah

Then what I was hoping to do was to indent 3 spaces when I press either the tab key or by pressing >> to indent like this:

  literal_block: |
   blah wee  `bib` 
   blah

And that should result in an output from the yaml-to-json parser of something like this:

{
  "waldo": "false", 
  "baz": [
    "qux", 
    "quxx"
  ], 
  "fred": "undefined", 
  "corge": null, 
  "literal_block": "blah wee  `bib` \nblah\n", 
  "grault": 1, 
  "test": "blah", 
  "garply": true, 
  "foo": "bar"
}

However, that's not what happens when I press those keys, instead I get

  literal_block: |
        blah wee  `bib` 
        blah

...and a parser error.

I've even installed the vim-ansible-yaml plugin as suggested by this post.

Should I try a different plugin for my indents? It seems like literal-block indents have 3 spaces beyond the parent, and everything else gets 2 spaces.

2
  • If I run :set filetype=ansible, I find that the indenting works better, but...when I'm doing literal_blocks I still have to add another space on every single line in the literal block.
    – leeand00
    Jan 4 '18 at 0:40
  • Not to say that I can't select all that text and do this: '<,'>s/^.*$/ \0/g, but it does seem like whatever plugin I'm using should handle that when working with yaml.
    – leeand00
    Jan 4 '18 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.