YAML comments are not natively supported; rather, support is dependent on extensions from certain parsers.

The support for YAML comments relies on parsers written in a programming language.

YAML comments

Comments are used to convey meaningful messages about a line of code. Comment text is ignored by the processor during the parsing of YAML files.

In most programming languages, there are two types of comments:

  • Inline or single-line comments
  • Block-level or multiple-line comments

YAML’s syntax allows for inline comments. However, block-level comments are not supported.

Single or Inline Comments in YAML

Inline comments are declarations made on the same line as the code. The # symbol denotes the start of a comment.

Here is an example of inline comments in YAML:

#  comments Syntax example in YAML file

or
#### comments example

Here is an example with array declaration.

# comments for array declaration
key1:
   value1 # Arry element
  - value2
  - value3
  - value4
  - value5

These are referred to as single-line comments. Each comment line begins with a # symbol followed by a description and ends with a new line break.

Block Level Comments in YAML

Block comments are used to comment on multiple lines. YAML does not support block comments by default; however, you can use multiple inline comments with single-line syntax.

# block level comment example
# comment line 1
# comment line 2
# comment line 3

Important notes on comments in YAML

  • The YAML processor ignores comments.
  • Comments are meant to document the code to make it easier for developers to understand.
  • Support for shortcut commands varies among different IDEs.

Commenting and Uncommenting in Editors

Let’s explore how to comment or uncomment using different popular editors. Various shortcut keys are used in these editors.

Comment in eclipse

Eclipse does not have native YAML support. First, install the YEdit plugin from the Eclipse Marketplace.

This editor provides features such as editing, syntax highlighting, checking, and content highlighting with multiple colors.

Select the line in which you want to comment code.

To comment, select the line you want to comment and issue the shortcut command CTRL + Shift + C. Repeat the command to remove the comments.

Alternatively, use the menu or context menu option.

  • Select the line of code
  • Right-click and select toggle comments

Commenting and Uncommenting in Visual Studio Code

VSCode has built-in support for YAML editor features.

Select the line or lines you want to comment and issue the shortcut command CTRL + /. To uncomment, rerun the previous command.

How to comment in ATOM editor

In Atom editor, select multiple lines and use the following commands to comment:

  • Ctrl + / in Windows and Linux
  • cmd + / in macOS