VS CodeでYAMLを快適に編集するおすすめ拡張機能|Lint・補完・スキーマ設定まで徹底解説

プログラミング・IT

「YAMLファイルをVisual Studio Code(VS Code)で編集しているけど、なんか使いにくい…」
「インデントミスが多発して、毎回エラーになる」
「キーの補完が出ないから、いちいち覚えて打たないといけない」
「どこが間違っているのか分かりにくい」

YAMLファイルを編集していて、こんなふうに感じたことはありませんか?

実は、VS Codeに適切な拡張機能を入れるだけで、YAMLの編集体験が劇的に向上します。インデントの自動補正、キーの補完、リアルタイムなエラーチェックなど、「なぜ最初から入ってないの?」と思うほど便利な機能が使えるようになります。

この記事では、VS CodeでYAMLを編集するときにぜひ入れたい便利な拡張機能を、使い方や設定方法と一緒に詳しく紹介します。

スポンサーリンク

なぜYAML専用の拡張機能が必要なのか?

YAMLの特徴と課題

YAMLは設定ファイルやデータ交換で広く使われていますが、編集時に特有の課題があります:

課題問題点VS Code標準での対応
インデント依存スペース数で構造が決まる基本的なハイライトのみ
スキーマが複雑Kubernetes、GitHub Actionsなど補完機能なし
文法エラー微妙な記述ミスでパースエラーエラー表示が不十分
階層が深い大規模なファイルで迷子になるナビゲーション機能なし

拡張機能で解決できること

適切な拡張機能を導入することで、以下のような劇的な改善が期待できます:

Before(標準のVS Code):

  • 手動でインデントを調整
  • キー名を暗記して入力
  • エラーは実行時に発覚
  • 大きなファイルで現在位置が不明

After(拡張機能導入後):

  • 自動インデント補正
  • スキーマベースの補完
  • リアルタイムエラーチェック
  • パスナビゲーション機能

必須のYAML拡張機能

1. YAML(Red Hat製) – 最強のオールインワン拡張

基本情報

項目内容
開発元Red Hat
ダウンロード数500万+
評価★★★★★
最終更新定期的に更新

主な機能

🎨 シンタックスハイライト

# 色分けが美しく表示される
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: app
    image: nginx:latest

🔧 インデント自動補正

  • 不正なインデントを自動検出
  • 修正候補を提案
  • 保存時の自動修正も可能

💡 スキーマベースの入力補完

  • Kubernetes
  • GitHub Actions
  • Docker Compose
  • Azure Pipelines
  • 500+ のスキーマに対応

🚨 リアルタイムLint(文法チェック)

  • 赤波線でエラー箇所を表示
  • ホバーで詳細な説明
  • 修正提案も表示

インストール方法

  1. VS Codeの拡張機能パネル(Ctrl+Shift+X)を開く
  2. 「yaml」と検索
  3. 「YAML by Red Hat」を選択
  4. 「Install」をクリック

基本設定例

// settings.json
{
  // YAML拡張の基本設定
  "yaml.format.enable": true,
  "yaml.validate": true,
  "yaml.hover": true,
  "yaml.completion": true,
  
  // スキーマの自動検出を有効化
  "yaml.schemaStore.enable": true,
  
  // カスタムスキーマの設定
  "yaml.schemas": {
    "https://json.schemastore.org/github-workflow.json": "/.github/workflows/*",
    "https://json.schemastore.org/docker-compose.json": "/docker-compose*.yml",
    "https://json.schemastore.org/kustomization.json": "/kustomization.yaml"
  }
}

実践的な活用例

GitHub Actionsでの使用:

# .github/workflows/ci.yml
name: CI
on: [push, pull_request]  # ← ここで補完が効く

jobs:
  test:
    runs-on: ubuntu-latest  # ← 選択肢が表示される
    steps:
    - uses: actions/checkout@v3  # ← バージョンも補完
    - name: Run tests
      run: |  # ← 構文エラーも即座に検出

2. Prettier – Code formatter – 自動整形の定番

基本情報と特徴

主な機能:

  • 保存時の自動フォーマット
  • 一貫したコードスタイル
  • 多言語対応(YAML、JSON、JavaScript、HTML等)

YAMLに特化した設定

// settings.json
{
  // YAMLのデフォルトフォーマッターにPrettierを設定
  "[yaml]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true
  },
  
  // Prettierの設定
  "prettier.tabWidth": 2,
  "prettier.printWidth": 80,
  "prettier.trailingComma": "none"
}

.prettierrcファイルでの設定

# .prettierrc.yml
printWidth: 80
tabWidth: 2
useTabs: false
semi: false
singleQuote: true
trailingComma: "none"
bracketSpacing: true
arrowParens: "avoid"

Before/Afterの例

Before(整形前):

apiVersion:v1
kind:   Pod
metadata:
name:my-pod
    labels:
app:nginx
spec:
containers:
-name:app
  image:nginx:latest
    ports:
-containerPort:80

After(Prettier適用後):

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: nginx
spec:
  containers:
    - name: app
      image: nginx:latest
      ports:
        - containerPort: 80

3. YAML Path – 大規模ファイルのナビゲーション

基本情報

大きなYAMLファイルで現在位置を把握するための拡張機能です。

主な機能

📍 リアルタイムパス表示

  • ステータスバーに現在のYAMLパスを表示
  • 例:spec.template.spec.containers[0].image

🔍 パスコピー機能

  • 右クリックメニューから現在のパスをコピー
  • ドキュメント作成やデバッグに便利

📋 パス履歴

  • 最近編集した場所の履歴を保存
  • 素早く前の編集箇所に戻れる

実用例

# 大きなKubernetesマニフェスト
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: app
        image: nginx:latest  # ← カーソルがここにある時
        # パス表示: spec.template.spec.containers[0].image
        ports:
        - containerPort: 80
        env:
        - name: ENV_VAR
          value: "example"

その他の便利な拡張機能

4. indent-rainbow – インデントの可視化

機能

  • インデントレベルを色分け表示
  • YAMLの階層構造が一目瞭然
  • 設定可能なカラーテーマ

設定例

{
  "indentRainbow.colors": [
    "rgba(255,255,64,0.07)",
    "rgba(127,255,127,0.07)",
    "rgba(255,127,255,0.07)",
    "rgba(79,236,236,0.07)"
  ]
}

5. YAML ♥ JSON – 形式変換

機能

  • YAMLとJSONの相互変換
  • 右クリックメニューから簡単変換
  • 大量データの形式変換に便利

6. Better Comments – コメント強化

機能

# TODO: 後で修正が必要
# ! 重要な注意事項
# ? 確認が必要な項目
# * ハイライトしたい情報
apiVersion: v1
kind: ConfigMap

用途別の推奨設定

Kubernetes開発者向け設定

{
  "yaml.schemas": {
    "https://raw.githubusercontent.com/instrumenta/kubernetes-json-schema/master/v1.18.0/all.json": "/*.k8s.yaml"
  },
  "yaml.customTags": [
    "!And",
    "!If",
    "!Not",
    "!Equals",
    "!Or",
    "!FindInMap",
    "!Base64",
    "!Cidr",
    "!Ref",
    "!Sub",
    "!GetAtt",
    "!GetAZs",
    "!ImportValue",
    "!Select",
    "!Split",
    "!Join"
  ]
}

CI/CD パイプライン向け設定

{
  "yaml.schemas": {
    "https://json.schemastore.org/github-workflow.json": "/.github/workflows/*",
    "https://json.schemastore.org/azure-pipelines.json": "/azure-pipelines*.yml",
    "https://json.schemastore.org/gitlab-ci.json": "/.gitlab-ci.yml"
  }
}

Docker Compose向け設定

{
  "yaml.schemas": {
    "https://json.schemastore.org/docker-compose.json": "/docker-compose*.yml"
  },
  "[yaml]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2,
    "editor.autoIndent": "advanced"
  }
}

効率的なワークフロー設定

自動保存と自動フォーマット

{
  // 自動保存を有効化
  "files.autoSave": "onDelay",
  "files.autoSaveDelay": 1000,
  
  // 保存時の自動処理
  "editor.formatOnSave": true,
  "editor.codeActionsOnSave": {
    "source.fixAll": true
  },
  
  // YAMLファイル専用設定
  "[yaml]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2,
    "editor.detectIndentation": false,
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  }
}

キーボードショートカットの活用

// keybindings.json
[
  {
    "key": "ctrl+shift+f",
    "command": "editor.action.formatDocument",
    "when": "editorTextFocus && resourceLangId == yaml"
  },
  {
    "key": "ctrl+shift+v",
    "command": "yaml.validate",
    "when": "editorTextFocus && resourceLangId == yaml"
  }
]

トラブルシューティング

よくある問題と解決法

1. スキーマ補完が効かない

症状: キーの補完候補が表示されない

解決法:

{
  // スキーマストアを有効化
  "yaml.schemaStore.enable": true,
  
  // 手動でスキーマを指定
  "yaml.schemas": {
    "https://json.schemastore.org/your-schema.json": "/your-pattern*.yml"
  }
}

2. インデントエラーが消えない

症状: 正しく見えるのにエラーが表示される

解決法:

{
  // タブとスペースの混在を防ぐ
  "editor.insertSpaces": true,
  "editor.detectIndentation": false,
  "editor.renderWhitespace": "all"
}

3. パフォーマンスが悪い

症状: 大きなYAMLファイルで動作が重い

解決法:

{
  // バリデーションの制限を設定
  "yaml.maxItemsComputed": 5000,
  "yaml.suggest.parentSkeletonSelectedFirst": true
}

デバッグ機能の活用

YAML言語サーバーのログ確認

  1. コマンドパレット(Ctrl+Shift+P)を開く
  2. “YAML: Open Log”を選択
  3. エラーの詳細情報を確認

スキーマの検証

{
  // 詳細なスキーマ情報を表示
  "yaml.trace.server": "verbose"
}

実践的な使用例

GitHub Actionsワークフローの作成

# .github/workflows/deploy.yml
name: Deploy to Production

# on: の補完候補が表示される
on:
  push:
    branches: [main]
  workflow_dispatch:

env:
  # 環境変数の補完も効く
  NODE_VERSION: '18'

jobs:
  deploy:
    # runs-on の選択肢が表示される
    runs-on: ubuntu-latest
    
    steps:
    # アクションの補完とバージョン確認
    - uses: actions/checkout@v4
    
    # 入力ミスをリアルタイムで検出
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ env.NODE_VERSION }}

Kubernetesマニフェストの編集

# deployment.yaml
apiVersion: apps/v1  # ← バージョンの補完
kind: Deployment     # ← Kindの選択肢表示

metadata:
  name: my-app
  labels:
    app: my-app

spec:
  replicas: 3        # ← 数値の検証
  selector:
    matchLabels:     # ← 必須フィールドの補完
      app: my-app
  
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: app
        image: nginx:1.21  # ← イメージ名の検証
        ports:
        - containerPort: 80
        
        # リソース制限の補完
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"

VS Codeの設定ファイル完全版

完全版settings.json

{
  // === YAML基本設定 ===
  "yaml.format.enable": true,
  "yaml.validate": true,
  "yaml.hover": true,
  "yaml.completion": true,
  "yaml.schemaStore.enable": true,
  
  // === エディタ設定 ===
  "[yaml]": {
    "editor.insertSpaces": true,
    "editor.tabSize": 2,
    "editor.detectIndentation": false,
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "editor.formatOnSave": true,
    "editor.autoIndent": "advanced"
  },
  
  // === スキーマ設定 ===
  "yaml.schemas": {
    "https://json.schemastore.org/github-workflow.json": "/.github/workflows/*",
    "https://json.schemastore.org/docker-compose.json": "/docker-compose*.yml",
    "https://json.schemastore.org/kustomization.json": "/kustomization.yaml",
    "https://json.schemastore.org/azure-pipelines.json": "/azure-pipelines*.yml",
    "https://json.schemastore.org/gitlab-ci.json": "/.gitlab-ci.yml"
  },
  
  // === Prettier設定 ===
  "prettier.tabWidth": 2,
  "prettier.printWidth": 80,
  "prettier.trailingComma": "none",
  
  // === その他の便利設定 ===
  "files.autoSave": "onDelay",
  "files.autoSaveDelay": 1000,
  "editor.renderWhitespace": "boundary",
  "editor.guides.indentation": true,
  
  // === インデント虹色表示 ===
  "indentRainbow.colors": [
    "rgba(255,255,64,0.07)",
    "rgba(127,255,127,0.07)",
    "rgba(255,127,255,0.07)",
    "rgba(79,236,236,0.07)"
  ]
}

まとめ:VS CodeでYAML編集を劇的に改善しよう

適切な拡張機能を導入することで、VS CodeでのYAML編集体験は劇的に向上します。

今回紹介した必須拡張機能

拡張機能主な用途重要度
YAML (Red Hat)文法チェック・補完・スキーマ対応★★★★★
Prettier自動コードフォーマット★★★★☆
YAML Path大規模ファイルでのナビゲーション★★★☆☆
indent-rainbowインデント可視化★★☆☆☆

コメント

タイトルとURLをコピーしました