How to Create a YAML File
Step-by-step guide to creating and working with YAML files
Introduction
YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files. This guide covers the essential steps to create a YAML file, from basic file creation to writing proper YAML syntax.
Creating a YAML File
You can create a YAML file using any text editor. Here are common methods:
Using Command Line
# Create a new YAML file
touch config.yaml
# Or create and open in editor
nano config.yaml
vim config.yaml
code config.yaml # VS Code
Using Text Editors
You can use any text editor to create YAML files:
- VS Code, Sublime Text, Atom (with YAML syntax highlighting)
- Notepad++ (Windows)
- TextEdit (macOS) - ensure plain text mode
- Gedit, Nano, Vim (Linux)
File Extensions
YAML files typically use these extensions:
.yaml- Recommended extension.yml- Common alternative
Basic YAML Syntax
Understanding YAML syntax is essential for creating valid files:
Key-Value Pairs
name: John Doe
age: 30
email: john@example.com
Nested Objects
person:
name: John Doe
age: 30
address:
street: 123 Main St
city: New York
zip: 10001
Lists
fruits:
- apple
- banana
- orange
# Inline list
tags: [python, yaml, tutorial]
Multi-line Strings
description: |
This is a multi-line
string that preserves
line breaks
summary: >
This is a folded
string that converts
line breaks to spaces
Complete Example
A complete YAML file example demonstrating common structures:
# Application Configuration
app:
name: My Application
version: 1.0.0
environment: production
# Server Settings
server:
host: localhost
port: 8080
ssl:
enabled: true
certificate: /path/to/cert.pem
# Database Configuration
database:
type: postgresql
host: db.example.com
port: 5432
name: myapp_db
credentials:
username: admin
password: secret123
# Features
features:
enabled:
- authentication
- logging
- caching
disabled:
- debug_mode
- test_endpoints
# Metadata
metadata:
author: John Doe
created: 2024-01-15
description: |
This is a sample configuration file
demonstrating YAML structure and syntax
Common Mistakes to Avoid
Incorrect: Tabs instead of spaces
YAML requires spaces for indentation, not tabs:
# Wrong - using tabs
person:
\tname: John # This will cause errors
# Correct - using spaces
person:
name: John
Incorrect: Inconsistent indentation
Maintain consistent indentation (typically 2 spaces):
# Wrong - mixed indentation
person:
name: John
age: 30 # Too much indentation
# Correct - consistent indentation
person:
name: John
age: 30
Incorrect: Missing colons
Key-value pairs must be separated by colons:
# Wrong - missing colon
name John Doe
# Correct - with colon
name: John Doe
Best Practices
-
•
Use consistent indentation - typically 2 spaces per level
-
•
Never use tabs - always use spaces for indentation
-
•
Use meaningful keys - choose descriptive names for better readability
-
•
Add comments - use
#for documentation -
•
Validate your YAML - use online validators or linters before using in production
Validating Your YAML File
Before using your YAML file, validate its syntax:
Online Validators
Use online YAML validators to check syntax:
- YAML Lint - yaml.cc
- YAML Validator - yaml.cc
Command Line Tools
# Using Python
python -c "import yaml; yaml.safe_load(open('config.yaml'))"
# Using yamllint (install: pip install yamllint)
yamllint config.yaml
# Using yq (install: brew install yq)
yq eval . config.yaml