A small thing with YAML which I stumbled about and which bugged me for too long.

I had a line like this, which I tried to put in YAML:

map \4 yyp:s/[ ]*$// \| s/[ ]*$// \| s/[^ ]/'/g \| s/' /''/g<cr>o<esc>'

This line (comming from my .vimrc) contains single quotes and escape characters. And: Yes, I have my vimrc defined in YAML.

But back to the problem at hand. Putting this in YAML syntax, you need to enclose it in single or double quotes.

  • If you put it in single quotes, the single quotes within the line need to be escaped. They cannot be replaced (because I need them there). Escaping with e.g. a backspace does not work in single quotes:
# This does not work
  - 'map \4 yyp:s/[ ]*$// \| s/[ ]*$// \| s/[^ ]/'/g \| s/' /''/g<cr>o<esc>''
  • For escaping you have to put them into double quotes. This also puts the back spaces in effect, that are already there. And you do not need to worry about the single quotes any more, but you have a now the back-space problem at-hand:
# This does not work either
  - "map \4 yyp:s/[ ]*$// \| s/[ ]*$// \| s/[^ ]/'/g \| s/' /''/g<cr>o<esc>'"

Escaping the back-space does not work either:

# This does not work either
  - "map \\4 yyp:s/[ ]*$// \\| s/[ ]*$// \\| s/[^ ]/'/g \\| s/' /''/g<cr>o<esc>'"

There could be a way to escape the back-spaces multiple times to end up with the origin line. I stopped investigating this direction, since it shifts the focus to using the right syntax instead of just addressing the problem.

It turns out, there is a way to escape single quotes within single quotes in YAML.

# This does work
  - 'map \4 yyp:s/[ ]*$// \| s/[ ]*$// \| s/[^ ]/''/g \| s/'' /''''/g<cr>o<esc>'''

But pre-seeding the single quote within another single-quote they are escape and can be used even within a string that it itself single-quoted.