YAML Comments: How to Comment, and Comment Out, YAML

A YAML comment starts with a hash sign (#) and runs to the end of the line. It can sit on its own line or after a value, as long as whitespace separates it from the value. YAML has no block comment syntax, so commenting out several lines means putting # at the start of each one.

Where # starts a comment in YAML and where it does not
LineIs the # a comment?Why
# Database settingsYesStarts the line
port: 5432 # defaultYesWhitespace before the #
url: https://x.example/#topNoNo whitespace before the #, so it is part of the value
color: "#4F98FB"NoInside a quoted string
tag: #blueYesThe value is empty (null) and the rest is a comment

Updated

Comments are the main reason teams pick YAML over JSON for configuration, yet the rules are easy to trip on. Paste a file into the tool below to comment out a range of lines or restore them, then check the reference for the cases where a # quietly becomes part of your data.

Comment out YAML lines

Paste YAML, pick a line range and switch those lines between active and commented out. Indentation is preserved, so the structure stays easy to read.

Comment patterns you will use

Full-line and inline comments

A full-line comment explains a section; an inline comment needs at least one space before the #.

# Retry policy for outgoing webhooks
retries: 5        # the receiving API times out under load
backoff_seconds: 30
brand_color: "#4F98FB"   # quoted, so the # stays in the value

Disabling a block temporarily

Every line of the disabled service carries its own #, kept at the original indentation so it can be restored cleanly.

services:
  web:
    image: nginx:1.27
  # cache:
  #   image: redis:7
  #   ports:
  #     - "6379:6379"

Inline comments and the space rule

The YAML spec says comments must be separated from other tokens by white space. That one rule explains almost every surprise: a # glued to the text before it is data, and a # after a space is the start of a note.

A tab counts as white space here too, but because tabs are not allowed for indentation it is simpler to use spaces everywhere.

Documenting a config file well

Write comments that say why a value is set, not what the key name already says. A line such as # 5 retries because the payment API times out under load saves the next person a search through history.

Put a short header comment at the top saying which program reads the file and where its full options are documented.

How it works

  1. Paste your YAML into the box, or edit the sample.
  2. Enter the first and last line numbers you want to change.
  3. Press Comment out to add # after each line's indentation, or Uncomment to remove it.
  4. Copy the result back into your file. Blank lines in the range are left untouched.

Tips

Frequently asked questions

Does YAML have multi-line or block comments?

No. The specification only defines line comments that start with #. To disable a block, prefix every line with #. Most editors do this for a selection with a single shortcut, and the tool on this page does it for any range of lines.

Why did my comment become part of the value?

A # only starts a comment when whitespace comes before it. In url: https://site.example/#faq the # follows a slash, so it stays inside the string. Add a space before the # if you meant a comment.

Are comments kept when YAML is converted to JSON?

No. Comments are a presentation detail, not data, and JSON has no place to put them. RFC 9512 notes that comments are discarded when YAML is serialized as JSON.

Can I comment inside a block scalar?

No. Inside a | or > block, a line that starts with # is ordinary text and becomes part of the string. Comments only work outside the block's content lines.

What is the shortcut to comment YAML in an editor?

It belongs to the editor, not to YAML. In Visual Studio Code the Toggle Line Comment command is Ctrl+/ on Windows and Linux and Cmd+/ on macOS; other editors set their own keys.

Related: YAML vs JSON, YAML multiline strings, YAML front matter, YAML to JSON converter.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap