Carvel YTT Language Support
VS Code language support for Carvel ytt: highlighting, folding, comment hints, snippets and optional ytt CLI validation, designed to sit next to a regular YAML extension (such as Red Hat YAML) without taking over plain YAML files.
Highlights
- Dedicated
ytt language mode for .ytt.yaml and .ytt.yml.
- ytt highlighting inside regular
.yaml files (injection grammar):
#@ directives: if, elif, else, for, def, end, load, return, pass, break, continue, and the one-line #@ if/end / #@ for/end forms
- annotations such as
#@overlay/match, #@data/values, #@schema/desc, #@yaml/text-templated-strings, #@template/replace, #@library/ref and #@assert/validate
- inline expressions (
name: #@ data.values.name, - #@ item), text templates ((@= value @)), and #! comments
- Folding that understands both ytt and YAML (see below).
- Region cues: unfolded lines are briefly highlighted, a bar marks the
#@ block around the cursor, and #@ headers show how far they reach.
- A hint, with a quick fix, for plain
# comments in ytt templates.
- Snippets for common ytt patterns.
- Optional validation with the
ytt CLI.
Folding
For ytt templates the extension provides folds that combine YAML structure with ytt control flow:
#@ if / #@ elif / #@ else branches, #@ for and #@ def blocks fold separately. Each fold stops before the next branch or #@ end, so the closing line stays visible. The exception is a top-level block that wraps a whole document: its fold also covers its closing #@ end, so it lines up with how YAML extensions fold the document.
- YAML keys fold up to their last content line, including compact lists (
key: followed by - item at the same indentation), list items, and block scalars (key: |).
- When a YAML key ends with a
#@ block, its fold also covers that block's #@ end (and any #! comments before it), so folding the key never leaves an #@ end without its #@ if. An #@ end whose #@ if is outside the key stays visible.
- Multi-line Starlark expressions (
#@ x = { … #@ }) fold.
--- documents fold.
- Templates embedded inside block scalars (for example an
overlay.yaml: | inside a kapp-controller App) fold as their own scope. Their --- and #@ lines don't affect the outer file.
- A
#@ branch that holds a single YAML node (for example one - secretRef: item) has one fold, on the #@ line, instead of two arrows for the same lines.
- Folds are always properly nested. With Red Hat YAML installed, VS Code uses these folds for ytt templates. Red Hat YAML treats
#@ lines as comments, so its folds can run through #@ end lines. The extension registers its folding again after Red Hat YAML starts (it never starts Red Hat YAML itself), and after its language server restarts.
For YAML files without ytt content, the extension provides no folds, and VS Code (or your YAML extension) folds them as usual.
Seeing where a region ends
Once a region is unfolded, its lines look like every other line. The extension adds a few cues so you don't have to select a region before unfolding it to see how far it reaches:
- Unfold highlight (any YAML file): the lines a fold reveals get a tinted background and a bar on the left, and the last revealed line shows a label such as
◀ end of #@ else · 3 lines, L26–L28. The highlight clears after folding.flashDuration, or when you edit, fold the region, or move the cursor out of the region. Scrolling, including scrolling past child regions that stay folded, keeps it. If the end is below the screen, the status bar shows where it is; click it to jump there. That entry stays after the highlight fades, until the end has been on screen, you leave the region, or you fold or edit. A fold that runs to the very last line of a file without a final newline is not highlighted, because nothing visible follows it. When an Unfold All opens many folds at once, nothing is highlighted, and the status bar says how many were opened (unless folding.highlightOnUnfold is off). For a second after an edit, fold changes are not treated as unfolds, because they usually come from the edit itself.
- Scope bar (ytt templates): a bar next to the
#@ if / elif / else / for / def branch that contains the cursor. It runs to the line before the next branch, or through the #@ end for the last branch. On an #@ end line it marks the whole block that line closes. Set folding.activeScope to innermost to follow every fold region (YAML keys included) instead.
- Region summaries (ytt templates): ghost text after the header of the branch the cursor is in, for example
▾ 6 lines → L17 · elif L18, and after folded #@ headers, for example ⋯ 6 hidden lines · L19–L24. With the cursor on an #@ end line, the block's first line shows the whole block, for example ▾ block L11–L29 · 3 branches.
- Hover on a
#@ if/elif/else/for/def/end line: the whole block with every branch's lines, plus links to select a branch, select the whole block, or jump to its #@ end.
Commands (in ytt and YAML files):
| Command |
What it does |
| YTT: Select Fold Region |
Selects the region at the cursor (full lines). Run it again to grow to the parent region. |
| YTT: Unfold and Select Region |
Unfolds the region at the cursor and selects it. |
| YTT: Go to Region Start / Go to Region End |
Moves to the first or last line of the region at the cursor. Repeat to move to the parent's start or end. |
| YTT: Go to Next #@ Branch / Previous #@ Branch |
Moves through #@ if → #@ elif → #@ else → #@ end, wrapping around. |
| YTT: Fold #@ Branches |
Folds every #@ branch that has no other #@ block inside it, except the one with the cursor. The outer #@ if / #@ for lines stay visible as an outline of the template's logic. |
Expand Selection (Shift+Alt+→, or Ctrl+Shift+Cmd+→ on macOS) also follows the fold regions in ytt templates, including whole #@ if … #@ end blocks.
The commands have no default key bindings, because the free chords differ between setups. To add some, open Preferences: Open Keyboard Shortcuts (JSON) and add entries such as:
[
{ "key": "ctrl+alt+s", "command": "yttLanguageSupport.selectFoldRegion", "when": "editorTextFocus" },
{ "key": "ctrl+alt+down", "command": "yttLanguageSupport.goToRegionEnd", "when": "editorTextFocus" },
{ "key": "ctrl+alt+up", "command": "yttLanguageSupport.goToRegionStart", "when": "editorTextFocus" }
]
Two built-in VS Code features combine well with these cues:
"editor.stickyScroll.enabled": true with "editor.stickyScroll.defaultModel": "foldingProviderModel" pins the enclosing #@ if …: header to the top of the editor while you scroll through its branch.
- Go to Parent Fold, Go to Next Folding Range and Go to Previous Folding Range move between folds.
Which files are treated as ytt
- Files using the
ytt language (.ytt.yaml, .ytt.yml) always are.
- A
.yaml file is treated as a ytt template when it has a #@ or #! marker outside block scalars.
- Kubernetes JSONPath filters such as
[?(@.type=="Ready")], and shebangs or comments inside key: | scripts, don't count as markers.
- A
.yaml file whose only ytt content sits inside block scalars gets ytt folding but no comment hints.
Highlighting is a TextMate grammar that is injected into every YAML file; a grammar cannot check file contents. It only colours ytt syntax (#@, #!, (@ … @)), so plain YAML files look the same as before.
#@ runs ytt directives and annotations.
#! is the ytt comment syntax.
# is a plain YAML comment. ytt accepts it, but #! makes intent clear in templates, so the extension shows a hint with a "Convert comment to #!" quick fix. Comments inside block scalars are left alone.
In the ytt language mode, Toggle Line Comment uses #!.
#! this is a ytt comment
#@ load("@ytt:data", "data")
#@ if data.values.enabled:
apiVersion: v1
kind: ConfigMap
metadata:
name: demo
data:
owner: #@ data.values.owner
#@ else:
#! fallback branch for disabled config
data: {}
#@ end
Commands
- YTT: Preview Markers: lists the ytt directives, annotations, comments and text templates in the active file and jumps to the selected one.
- YTT: Validate Current File: runs
ytt and shows errors as diagnostics on the matching lines. It reports whether validation passed, found problems, or could not run (for example, ytt not installed or the file not saved).
Validation settings
Most templates need their data values to render, so configure what else ytt should load:
| Setting |
Default |
Description |
yttLanguageSupport.validation.mode |
manual |
off, manual (command only) or onSave. |
yttLanguageSupport.validation.target |
file |
Pass the current file, or its whole directory, to ytt -f. |
yttLanguageSupport.validation.additionalFiles |
[] |
Extra -f paths such as values or schema files. Relative paths resolve against the workspace folder. Supports ${workspaceFolder}, ${fileDirname} and ${file}. |
yttLanguageSupport.validation.extraArgs |
[] |
Extra arguments, for example ["--data-value", "env=dev"]. |
yttLanguageSupport.validation.binaryPath |
ytt |
Path to the ytt binary. User settings only. |
Example workspace settings:
{
"yttLanguageSupport.validation.target": "directory",
"yttLanguageSupport.validation.additionalFiles": ["${workspaceFolder}/values/default-values.yaml"]
}
Validation results stay until the next validation run. If ytt can't run (not installed, or it times out), the file shows a warning instead, and with onSave a notification appears once per problem. Validation is disabled in untrusted workspaces, and binaryPath can't be set from workspace settings, so opening a repository can't make the extension run a different program.
Other settings
yttLanguageSupport.comments.warnOnPlainYamlComments: show the hint for plain # comments (default true).
yttLanguageSupport.folding.highlightOnUnfold: flash (default) or off.
yttLanguageSupport.folding.flashDuration: how long the unfold highlight stays, in milliseconds (default 1500).
yttLanguageSupport.folding.activeScope: control (default, #@ branches), innermost (any fold region) or off.
yttLanguageSupport.folding.regionSummary: cursor (default), always (every #@ branch header) or off.
yttLanguageSupport.folding.hover: show the block structure on hover over #@ lines (default true).
The colors can be changed in workbench.colorCustomizations: ytt.unfoldedRegionBackground, ytt.unfoldedRegionBorder, ytt.activeScopeBorder and ytt.regionSummaryForeground.
Snippets
ytt-if, ytt-if-else, ytt-for, ytt-def, ytt-load, ytt-overlay-match, ytt-data-values, ytt-data-values-schema, ytt-comment.
Development
Requires Node.js 22 or later.
npm ci
npm test # VS Code with no other extensions, then with Red Hat YAML
npm run package # builds ytt-language-support-<version>.vsix
npm run test:vsix # installs that .vsix into a clean VS Code and runs the suite
Test options (environment variables):
YTT_TEST_SKIP_REDHAT=1: skip the run with Red Hat YAML.
YTT_TEST_REDHAT_EXTENSION_PATH=<folder>: use an already-installed Red Hat YAML folder instead of downloading it from the Marketplace.
VSCODE_TEST_VERSION=<version>: VS Code version to test against (default stable).
| |