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
-
3So in VSCode you can highlight the lines you want to comment then Ctrl+: and all lines will be comment. You can uncomment with the same way. if the shortcut does not work you can pass by Edit Menu – sadati boina Dec 02 '22 at 08:42
-
2I think you means to say CTRL+/ for comment not : – MikeF Feb 08 '23 at 17:00
-
1The CTRL + / (or CMD + / for mac) works for intellij too – bbnt Apr 27 '23 at 19:40
-
I just spent 5 minutes troubleshooting this, so for any other users with German keyboard layout: For you it is Strg+# – Discostu36 Aug 31 '23 at 07:54
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.

- 30,738
- 21
- 105
- 131

- 142,882
- 41
- 325
- 378
I am not trying to be smart about it, but if you use Sublime Text 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.

- 30,738
- 21
- 105
- 131

- 7,967
- 5
- 35
- 43
-
3If you're in Eclipse with the YEdit plugin, the standard Eclipse block-comment-toggler of ctrl-/ will toggle block comments in yaml files. – Matt Gibson Feb 07 '15 at 20:31
-
1
-
2
-
8
-
9
-
20
-
1Does not work in Sublime 3. See https://stackoverflow.com/questions/17742781/keyboard-shortcut-to-comment-lines-in-sublime-text-3/43707799 – nedenom Jul 02 '19 at 12:51
-
1I've been using **ctrl** + **k**, **c** to comment and **ctrl** + **k**, **u** to un-comment in VSCode on Windows. I didn't know about this shorter key combination, but I will use it from now on. – John Mills Nov 28 '19 at 21:51
-
13This answers a different question than asked. This question asks about the YAML grammar, not about implementation/user details based on a particular tool. (Note: the StackOverflow UX workflow is somewhat constraining, but this is by design. It is designed to promote direct answers to the asked question, not dozens of different answers discussing unnecessary particulars.) – David J. Feb 28 '21 at 18:00
-
-
-
1
-
2@DavidJ. I know I'm being pedantic here, but I would argue that the opposite is true: This answer DOES answer the question "How do I comment a block of lines in YAML?" bc it does explain how to turn a block of YAML into a comment, even though it's not the /* */ -type block comments that we expect. The accepted and more upvoted answers that say "YAML doesn't support block comments" don't actually answer the question. They sidestep it with an "NA" type answer. – Samurai Soul Sep 14 '22 at 21:28
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.

- 2,877
- 25
- 35

- 5,998
- 4
- 36
- 59
-
11least amount of thinking possible ;). `"Vim, I did a thing. do it here, and here and here and here and here..."` – Conrad.Dean Jul 14 '14 at 01:52
-
7
-
1@Conrad.Dean try to [learn to think in macros](http://www.thegeekstuff.com/2009/01/vi-and-vim-macro-tutorial-how-to-record-and-play/). Though virtually the same for this task, `qqI#
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). – Bruno Bronosky Nov 17 '17 at 02:50 -
1@BrunoBronosky: instead of all those `@q`, you can simply do `6@q` or `10000@q` if you like. – bodo Apr 26 '18 at 11:55
-
2@bodo you can indeed. Most vim commands accept `: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 http://vimcasts.org/episodes/operating-on-search-matches-using-gn/ – Bruno Bronosky Apr 26 '18 at 15:11
-
-
1another option is to select lines in normal visual mode, and do `:normal i#` – Coderino Javarino May 06 '22 at 08:57
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

- 1,024
- 8
- 5
-
9I guess this is the holly grail answer to the question; especially if one wants these comments to appear in JSON or XML if one is to transform from YAML to these two. – Mohd May 06 '20 at 15:23
-
2
-
5@mTvare Programming languages approach commenting in a different way of thinking than data serialization languages. A specific pattern that looks stupid in a domain, could be the best choice in another. – Dimitrios Tsalkakis Feb 11 '21 at 10:24
-
1
-
1You might have an app that validates against a schema and complains about unrecognised fields. – zakmck Jun 25 '22 at 18:45
-
1@mTvare that's basically one way to comment in Python--anonymous string literals (I think Ruby supports this, too) – nijave Jun 28 '22 at 19:39
-
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.

- 30,738
- 21
- 105
- 131

- 409
- 1
- 6
- 13
-
23
-
And FWIW, the keyboard shortcut for that (in np++) would be ctrl-shift-Q (on windows. For other platforms, see the edit>comment/uncomment menu). – charlie arehart Jul 20 '19 at 17:27
-
3For grammar-based questions, a correct answer only needs to discuss the grammar. Other details about a particular tool (such as a particular text editor) are unnecessarily specific and thus inapplicable to the question as asked. – David J. Feb 28 '21 at 17:58
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 (#).

- 1,964
- 16
- 17
-
2Again, the OP asked a simple YAML grammar question, not a text editor question. (Imagine if every general language question included answers coupled to all the editors in use... maybe some people want such a world, but that isn't the design of StackOverflow.) – David J. Feb 28 '21 at 18:05
-
Most of us know there is no such thing as block-comment in YAML. I'd suggest that at this point we edit the question to "How does one comment out multiple lines in a YAML file with different editors/tools" instead. That way we keep the answers here useful while we answer both the original question and provide suggestions to people to get the job done. – Lester Cheung Sep 21 '22 at 06:38
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.

- 30,738
- 21
- 105
- 131

- 4,333
- 16
- 71
- 144
-
2That works for all JetBrains IDE's I think. I know it works for PyCharm as well :) Works on Mac OSX as well. – Subtubes Nov 24 '16 at 00:30
-
4It is better to answer a grammar question directly, without unnecessarily mentioning a text editor. (I would make a *very* rough guess that there are at least 25 widely used editors, *very* roughly defined as having over 50,000 users per year.) On the other hand, there is only one dominant edition of YAML -- the 3rd edition -- released in 2009. – David J. Feb 28 '21 at 18:17
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.

- 30,738
- 21
- 105
- 131

- 85
- 1
- 5
-
4Again, the OP asked a YAML *grammar* question, not a *text editor* question. – David J. Feb 28 '21 at 18:03
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]

- 30,738
- 21
- 105
- 131

- 7,295
- 4
- 71
- 112
-
6I interpret the question as asking how to do a block comment in *any* YAML file; therefore, answers should not be coupled to any particular tool. – David J. Feb 28 '21 at 01:39
-
@david j, is that even possible not to mention a tool? In any of these answers? – Blue Clouds Feb 28 '21 at 05:52
-
2Yes: a correct answer only needs to discuss the YAML grammar. See the other answer at https://stackoverflow.com/a/2276604/109618. Other details based on the specific text editor used are unnecessarily specific and thus inapplicable to a general audience. – David J. Feb 28 '21 at 17:56
-
Those who end up on this link are those who are using a text editor. Clearly the one you pointed out has 2000+ votes which of course is the right answer. But the one with 159 votes is about sublime editor and there are many others on different tools. Or in other words you have more downvotes to do. – Blue Clouds Mar 01 '21 at 06:17
-
1The StackOverflow UX workflow is somewhat constraining, but this is by design. It is designed to promote direct answers to the asked question, not dozens of different answers based on tooling. – David J. Mar 01 '21 at 10:56
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

- 30,738
- 21
- 105
- 131

- 2,003
- 1
- 16
- 18
-
9That keyboard shortcut will work for some specific editor, not some specific YAML file. – Quentin Jan 15 '21 at 13:42
-
1@Quentin I mentioned gitlab yaml file and not any other yaml file. One can access and update .gitlab-ci.yaml file on gitlab interface on any browser. – vinsinraw Jan 16 '21 at 14:28
-
4So this is specific to the Gitlab browser-based editor **and** a specific file? Doesn't seem to be a very useful answer to a general question about YML files. – Quentin Jan 16 '21 at 17:52
-
Yes. Question was general and it did not mention specific editor or environment. Others gave their observations w.r.t. to their editors like notepad++, sublime text, Azure pipeline editor etc. My observation was w.r.t. to gitlab yaml file on gitlab environment which has its own pipeline editor. Could be useful for someone working on gitlab yaml file in gitlab environment. – vinsinraw Jan 16 '21 at 22:32
-