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.
| 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.
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 valueDisabling 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
- Paste your YAML into the box, or edit the sample.
- Enter the first and last line numbers you want to change.
- Press Comment out to add # after each line's indentation, or Uncomment to remove it.
- Copy the result back into your file. Blank lines in the range are left untouched.
Tips
- Keep the # aligned with the code it disables. Commented lines at the same indentation are much easier to restore later.
- Put a space after the # in your own notes. It is not required, but it makes commented-out code and remarks easy to tell apart.
- Quote any value that contains a # you want to keep, such as hex colors, so a later edit cannot turn it into a comment.
- Remember that comments are dropped when a file is loaded and saved again by most libraries. Do not store anything important only in a comment that tooling rewrites.
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.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $20 a month.
More in Guides
- YAML Front Matter: Metadata at the Top of a Markdown File
- YAML Multiline Strings: | and >, Explained with a Live Preview
- YAML vs JSON: Which One, and Why
- Your Connection Is Not Private: Causes and Fixes
- 400 Bad Request: What Was Wrong With the Request
- 401 vs 403: Pick the Right Status for a Refused Request
- 403 Forbidden: Why the Server Refuses and How to Fix It
- 404 Not Found: Why Pages Go Missing and How to Fix It
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App