How do I comment a block of lines in YAML?
12 Answers
YAML supports inline comments, but does not support block comments.
From Wikipedia:
Comments begin with the number sign (
#
), can start anywhere on a line, and continue until the end of the line
A comparison with JSON, also from Wikipedia:
The syntax differences are subtle and seldom arise in practice: JSON allows extended charactersets like UTF-32, YAML requires a space after separators like comma, equals, and colon while JSON does not, and some non-standard implementations of JSON extend the grammar to include Javascript's
/* ... */
comments. Handling such edge cases may require light pre-processing of the JSON before parsing as in-line YAML.
# If you want to write
# a block-commented Haiku
# you'll need three pound signs
The spec only describes one way of marking comments:
An explicit comment is marked by a “#” indicator.
That's all. There are no block comments.
In Vim you can do one of the following:
- Comment all lines:
:%s/^/#
- Comment lines 10 - 15:
:10,15s/^/#
- Comment line 10 to current line:
:10,.s/^/#
- Comment line 10 to end:
:10,$s/^/#
or using visual block:
- Select a multiple-line column after entering visual block via Ctrl+v.
- Press r followed by # to comment out the multiple-line block replacing the selection, or Shift+i#Esc to insert comment characters before the selection.
An alternative approach:
If
- your YAML structure has well defined fields to be used by your app
- AND you may freely add additional fields that won't mess up with your app
then
- at any level you may add a new block text field named like "Description" or "Comment" or "Notes" or whatever
Example:
Instead of
# This comment
# is too long
use
Description: >
This comment
is too long
or
Comment: >
This comment is also too long
and newlines survive from parsing!
More advantages:
- If the comments become large and complex and have a repeating pattern, you may promote them from plain text blocks to objects
- Your app may -in the future- read or update those comments
In Azure Devops browser(pipeline yaml editor),
Ctrl + K + C Comment Block
Ctrl + K + U Uncomment Block
There also a 'Toggle Block Comment' option but this did not work for me.
There are other 'wierd' ways too: right click to see 'Command Palette' or F1
Now it is just a matter of #
or even smarter [Ctrl + k] + [Ctrl + c]