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.
| Line | Is the # a comment? | Why |
|---|---|---|
| # Database settings | Yes | Starts the line |
| port: 5432 # default | Yes | Whitespace before the # |
| url: https://x.example/#top | No | No whitespace before the #, so it is part of the value |
| color: "#4F98FB" | No | Inside a quoted string |
| tag: #blue | Yes | The 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.
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.
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 valueEvery 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"
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.
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.
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.
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.
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.
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.
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.
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.