šÆ Basic YAML Editing
Starting a New YAML File
When you open a YAML file, the configuration automatically applies optimizations:
1services:
2 web:
3 image: nginx
4 ports:
5 - "80:80"
š” Pro Tips
- 2-space indentation is automatically set for YAML files
- Syntax highlighting makes structure easier to see
- Line numbers and indentation guides are enabled by default
- 80-character column marker helps maintain readability
š® Try It Yourself
Key shortcuts to remember:
Tab - Indent current line
Shift+Tab - Unindent current line
Ctrl+Space - Trigger completion
š§ Navigation Techniques
Folding YAML Sections
Manage complex nested structures with folding:
1apiVersion: apps/v1
2kind: Deployment
3metadata: [+] --- 5 lines folded ---
9spec:
10 replicas: 3
11 template:
12 spec: [+] --- 15 lines folded ---
za - Toggle fold at cursor
zR - Open all folds
zM - Close all folds
š Efficient Navigation Workflow
- Open large YAML file
- Use zM to collapse all sections
- Navigate to section of interest
- Use za to expand only that section
Block Navigation
Jump efficiently between YAML blocks:
]} - End of current block
[{ - Beginning of current block
% - Jump between matching brackets
File Navigation
For projects with multiple YAML files:
,n - Open NERDTree
,f - Fuzzy file finder
Ctrl+p - Quick file search
š Indentation Management
Visual Selection Indentation
Adjust indentation of multiple lines at once:
1version: '3.8'
2services:
3web:
4image: nginx
5ports:
6- "80:80"
Steps to Indent Multiple Lines:
- Enter visual mode with V
- Select lines with j or k
- Indent with Tab or unindent with Shift+Tab
Auto-indent Entire File
Fix indentation throughout the entire file:
1services:
2web:
3image: nginx
4ports:
5- "80:80"
gg=G to fix all indentation
1services:
2 web:
3 image: nginx
4 ports:
5 - "80:80"
šÆ Indentation Best Practices
- Use 2 spaces for YAML indentation (never tabs)
- Enable visual indentation guides for better alignment
- Use auto-indent features to maintain consistency
- Validate indentation with linters in real-time
ā
Validation and Error Checking
Automatic Linting with ALE
Real-time YAML validation as you type:
1database:
2 host: localhost
3 port "5432" ā ļø Missing colon
4 username: admin
5 password: ā ļø Missing value
ALE Features:
- Syntax errors highlighted in real-time
- Error symbols in the gutter
- Hover over errors for detailed messages
- Integration with YAML linters
Manual YAML Formatting
Format and validate with a single command:
,y - Invoke YAML formatter
services:
web:
image: nginx:latest
ports:
- "80:80"
- "443:443"
environment:
NODE_ENV: production
DEBUG: false
š”ļø Validation Checklist
- Check for proper indentation (2 spaces per level)
- Ensure colons are followed by spaces
- Validate array syntax with proper dashes
- Check for matching quotes and brackets
- Verify no tabs are mixed with spaces
š Advanced Editing Techniques
Surrounding Text Operations
Modify, add, or remove surrounding characters efficiently:
1database_url: "postgresql://user:pass@localhost"
2api_keys:
3 - secret_key_1
4 - secret_key_2
cs"' - Change double to single quotes
ds" - Delete surrounding quotes
ysiw] - Surround word with brackets
Multiple-line Editing with Visual Block
Edit multiple lines simultaneously:
1# services:
2# web:
3# image: nginx
4# ports:
5# - "80:80"
Visual Block Workflow:
- Press Ctrl+v for visual block mode
- Select column with j/k
- Press I to insert at beginning
- Type changes (e.g.,
# for comments)
- Press Esc to apply to all lines
Find and Replace in YAML
Powerful search and replace operations:
:%s/old_key/new_key/gc
old_database_host: localhost
old_database_port: 5432
old_database_name: myapp
new_database_host: localhost
new_database_port: 5432
new_database_name: myapp
šļø Real-world Examples
Kubernetes Configuration Workflow
Complete workflow for editing Kubernetes YAML files:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.20
ports:
- containerPort: 80
š K8s Editing Workflow:
- :e deployment.yaml - Open file
- ,y - Format and validate
- zM - Collapse all sections
- /spec<Enter> - Search for spec
- za - Expand spec section
Docker Compose Best Practices
Working efficiently with docker-compose files:
version: '3.8'
services:
webapp:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DB_HOST=postgres
- DB_USER=admin
- DB_PASS=secret
depends_on:
- postgres
- redis
postgres:
image: postgres:13
environment:
- POSTGRES_DB=myapp
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=secret
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
š³ Docker Compose Tips
- Use indentation guides to align environment variables
- Use rainbow brackets for matching service blocks
- Use column highlighting to maintain consistent formatting
- Fold services when working on specific sections
CI/CD Pipeline Configuration
GitHub Actions and GitLab CI optimization:
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 16, 18]
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
š CI/CD Best Practices:
- Use NERDTree to navigate between workflow files
- Use folding to focus on specific job stages
- Validate syntax with YAML formatter
- Use visual block mode for bulk edits to similar steps
YAML Anchors and References
Advanced YAML features for reducing duplication:
common: &common_config
logging: true
timeout: 30
retry_attempts: 3
services:
service1:
<<: *common_config
port: 8080
name: web-service
service2:
<<: *common_config
port: 9090
name: api-service
šÆ Performance Tips for Large Files
- Disable cursor highlighting for very large files: :set nocursorline
- Use lazy redraw for smoother scrolling: :set lazyredraw
- Use marks for quick navigation: ma to set, 'a to jump
- Use line numbers for direct jumping: :123 goes to line 123
š Mastery Checklist
- ā
Master basic YAML syntax and indentation
- ā
Use folding effectively for large files
- ā
Leverage real-time validation and error checking
- ā
Apply advanced editing techniques (surround, visual block)
- ā
Optimize workflows for specific use cases (K8s, Docker, CI/CD)
- ā
Use YAML anchors for configuration reuse