How to Add Comments in YAML
New to YAML? See how to add single-line, inline comments in YAML. Also learn about commenting out multiple lines quickly.
YAML is widely used to write configuration files for containerized applications.
Now, there are several reasons you want to comment out code lines, such as to explain the use of a specific line or ignore the line causing the error.
YAML only supports single-line comments. All you have to do is to add # at the beginning of a line. You can also put it in the middle of a sentence. The text after # till the end of the line is disregarded.
# This is a single line comment foo: bar # This is an inline comment
You cannot add a comment block as you do in C, C++ and other languages. However, using a text editor of your choice, you can comment out multiple lines in fewer keystrokes.
I'll be covering both in this quick tutorial:
- Comment single line
- Add inline comments
- Comment out multiple lines
So let's start with commenting on a single line.
Comment a single line in YAML
You will have to use the hash
# symbol to add comments in YAML.
For reference, here, I will add a comment at the beginning of a file and will also show you how you can comment out single lines:
Neat way. Right?
But what about commenting out multiple lines? Well, here's how you do it.
Add inline comments in YAML
You can also add an 'inline comment' in YAML. This method adds the comment in the same line but after the code.
foo: bar windows: discard # Inline comment linux: debian: ubuntu arch: manjaro
Comment multiple lines in YAML (using nano)
In C and many other languages, you can comment out hundreds of lines by putting them between /* and */.
YAML doesn't have the block comments feature. If you have to comment out 20 lines, you have to add # at the beginning of all those 20 lines.
You can do that one by one manually or you can use your text editor and add # to all those lines in fewer keystrokes.
Here, I'm using nano editor, and to comment out multiple lines, you will have to follow three simple steps.
1. Use Alt + A to set the mark
To select lines in a nano text editor, first, you have to set a mark indicating where you want to start the selection:
Once you press
Alt + A, nano will indicate that you have made a mark on that line.
2. Use the Arrow keys to select lines
Once you set the mark, you can use the up or down arrow keys to select multiple lines:
3. Use Esc +3 to comment out selected lines
Once you are done selecting multiple lines, you can use the
Esc + 3 to comment out selected lines:
4. Unset the mark
Once you comment out selected lines, you will have to unset the mark which can be done using
Alt + A:
And finally, you can save changes and exit from the nano text editor.
But what about other text editors?
I love nano but that can't be the same for everyone. The good thing is that most text editor has this functionality of adding comments to multiple lines.
If you are a VSCode user, we have a detailed guide on how you can comment out multiple lines (for any programming language):
Time to comment
Like adding comments in bash scripts, the inability to add comment block pinches here as well. But well, we cannot do much about it, can we?
Since you are reading about adding comments in YAML, I advise brushing up the YAML basics 👇
🗨 Now that you know how to add comments in YAML files, how about adding a comment to this article and telling us if it was helpful or not?