Nunjucks for Visual Studio Code
Full editor support for Nunjucks templates: syntax highlighting, HTML IntelliSense, Emmet, formatting and snippets, in .njk files and in vscode.dev. On the Visual Studio Marketplace since 2015 with more than 260,000 installs, and on Open VSX. Free and open source under the MIT license.
code --install-extension ronnidc.nunjucks
Or search for Nunjucks by ronnidc in the Extensions view. VSCodium, Cursor, Windsurf and other editors that use Open VSX install it from open-vsx.org.
What it does
- Syntax highlighting built on VS Code's own HTML grammar. Nunjucks tags, expressions, filters, tests, comments and whitespace control are highlighted everywhere they can appear: in markup, inside attribute values, inside
<script>and<style>blocks and in inline styles.{% raw %}and{% verbatim %}blocks are left alone. - HTML IntelliSense. Tag and attribute completion, hover documentation, auto-closing and auto-renaming of tags, matching tag highlighting, document outline and folding, straight from VS Code's HTML language server. CSS and JavaScript inside
<style>and<script>get their own completion too. - Emmet. Abbreviations such as
ul>li*3expand in Nunjucks files out of the box. - Formatting that understands templates. Format Document keeps
{% %},{{ }}and multi-line{# #}blocks intact instead of folding them into one line. - Front matter. A YAML,
---jsor---jsonblock at the top of the file is highlighted as YAML, JavaScript or JSON, the way Eleventy and other static site generators expect. - Comment toggling that follows the cursor. Ctrl+/ or Cmd+/ gives
{# #}in markup,//inside<script>and/* */inside<style>. - Smart indentation and auto-closing delimiters. Pressing Enter between
{% if %}and{% endif %}indents the way it does in HTML, and typing{{,{%or{#inserts the closing pair with the cursor in the middle. - Snippets for every Nunjucks tag, for Eleventy's own tag and shortcodes, and for front matter.
- Works everywhere. No native code and no build step, so the extension runs in VS Code desktop, vscode.dev, github.dev and remote workspaces.
File extensions and settings
.njk, .nunjucks, .nunj and .nj open as Nunjucks. The extension deliberately does not take over .html files, so plain HTML keeps its own comments, IntelliSense and formatter. If your templates are .html files, associate them yourself in settings.json, for the whole workspace or for one folder:
"files.associations": {
"*.html": "nunjucks",
"src/_includes/**/*.html": "nunjucks"
}
The extension changes two VS Code defaults so that the features above work without configuration: emmet.includeLanguages gets { "nunjucks": "html" }, and html.format.templating is set to true. Both are ordinary settings you can override. Everything else is inherited from your HTML settings, so editor.formatOnSave under [nunjucks] formats templates on save.
With Eleventy
Nunjucks is the most used template language in Eleventy, and the extension knows the syntax Eleventy adds on top: {% setAsync %}, the paired shortcodes of the Render and Bundle plugins (renderTemplate, css, js and html) with CSS, JavaScript and Markdown highlighted inside them, Eleventy's universal filters such as url and slugify, and snippets for all of it. Other shortcodes and custom tags are highlighted as tags even though the extension cannot know their names.
Known limitations
- Custom tag delimiters set with
nunjucks.configure({ tags: ... })are not supported yet. The grammar assumes{{ }},{% %}and{# #}(issue 36). - Nunjucks inside JavaScript template literals is not highlighted (issue 22).
- Paired shortcodes you define yourself are highlighted as tags but do not fold or indent yet.
What comes next
Version 1.0 was a rewrite of the grammar and the editor integration. Version 1.1 adds a small language layer on top: syntax errors from the Nunjucks parser shown inline, go to definition for {% extends %}, {% include %} and {% import %} paths, hover documentation and completion for tags, filters and tests, folding of block pairs, and a setting that names your own paired shortcodes. Follow the issues and the changelog for progress.
Bug reports, feature requests and pull requests are welcome on GitHub.