How do I comment a block of lines in YAML?
11 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 5 Comments
The specification only describes one way of marking comments:
An explicit comment is marked by a “#” indicator.
That's all. There aren't any block comments.
Comments
I am not trying to be smart about it, but if you use Sublime Text or Visual Studio Code for your editor, the steps are:
- Select the block
- Cmd + / on Mac or Ctrl + / on Linux and Windows
- Profit
I'd imagine that other editors have similar functionality too. Which one are you using? I'd be happy to do some digging.
15 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.
7 Comments
"Vim, I did a thing. do it here, and here and here and here and here...": and type s/^/# to comment current selection.qqI#<esc>jq then @Q@Q@Q@Q@Q@Q (because it's faster to not let off the shift key), is a habit you can extend to much more complex tasks. Start small. Practice often. Soon you will create very complex macros perfectly first try. If you clear the register first qqq, you can include @q before the last q to get recursion (but only to the end of the file).@q, you can simply do 6@q or 10000@q if you like.:h count multipliers. But for me, visually anything over 6 and I have to do it be hand because I can't reliably guess it. The exception being that I know I have 60+ rows on my terminal so I will use that to estimate for very large marco playbacks. As a bonus, I'm going to suggest everyone learn about :h gn which make it easy to do . repetition on search matches. See vimcasts.org/episodes/operating-on-search-matches-using-gnAn 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
8 Comments
One way to block commenting in YAML is by using a text editor like Notepad++ to add a # (comment) tag to multiple lines at once.
In Notepad++ you can do that using the "Block Comment" right-click option for selected text.
3 Comments
Emacs has comment-dwim (Do What I Mean) - just select the block and do a:
M-;
It's a toggle - use it to comment AND uncomment blocks.
If you don't have yaml-mode installed you will need to tell Emacs to use the hash character (#).
2 Comments
For RubyMine users on Windows:
Open the file in the editor.
Select the block and press:
Ctrl + /,
And you will have the selected block starting with #.
Now if you want to uncomment the commented block, press the same key combination Ctrl + forward slash again.
2 Comments
In the 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 'weird' ways too: Right-click to see 'Command Palette' or F1
Then choose a cursor option.
Now it is just a matter of #.
Or even smarter [Ctrl + K] + [Ctrl + C]
5 Comments
If you are using Eclipse with the YEdit plugin (an editor for .yaml files), you can comment-out multiple lines by:
- selecting lines to be commented, and then
- Ctrl + Shift + C
And to uncomment, follow the same steps.
1 Comment
In a .gitlab-ci.yml file, the following works:
To comment out a block (multiline): Select the whole block section > Ctrl K C
To uncomment already commented out block (multiline): Select the whole block section > Ctrl K U



