šŸš€ YAML Vim Mastery Tutorial

Interactive guide to master YAML editing in Vim

šŸŽÆ Basic YAML Editing

Starting a New YAML File

When you open a YAML file, the configuration automatically applies optimizations:

vim new-config.yaml
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

šŸ“ Indentation Management

Visual Selection Indentation

Adjust indentation of multiple lines at once:

docker-compose.yaml
1version: '3.8'
2services:
3web:
4image: nginx
5ports:
6- "80:80"
Steps to Indent Multiple Lines:
  1. Enter visual mode with V
  2. Select lines with j or k
  3. Indent with Tab or unindent with Shift+Tab

Auto-indent Entire File

Fix indentation throughout the entire file:

Before: messy-config.yaml
1services:
2web:
3image: nginx
4ports:
5- "80:80"
gg=G to fix all indentation
After: fixed-config.yaml
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:

config.yaml - ALE Enabled
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:

Before Surround Operations
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:

Multi-line Comment Example
1# services:
2# web:
3# image: nginx
4# ports:
5# - "80:80"
Visual Block Workflow:
  1. Press Ctrl+v for visual block mode
  2. Select column with j/k
  3. Press I to insert at beginning
  4. Type changes (e.g., # for comments)
  5. Press Esc to apply to all lines

Find and Replace in YAML

Powerful search and replace operations:

:%s/old_key/new_key/gc
# Before replacement old_database_host: localhost old_database_port: 5432 old_database_name: myapp # After: :%s/old_/new_/gc 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:
  1. :e deployment.yaml - Open file
  2. ,y - Format and validate
  3. zM - Collapse all sections
  4. /spec<Enter> - Search for spec
  5. 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:

# Define common configuration common: &common_config logging: true timeout: 30 retry_attempts: 3 services: service1: <<: *common_config # Inherit common config port: 8080 name: web-service service2: <<: *common_config # Inherit 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