:h treesitter-language-injections.
Overview
Language injections allow Tree-sitter to:- Highlight SQL queries inside Python strings
- Parse JavaScript code within HTML
<script>tags - Highlight regex patterns in language-specific string literals
- Enable proper syntax in template languages
- Support embedded languages in comments (like code blocks in documentation)
No setup is needed to enable language injections. They work automatically when the appropriate queries are present.
Valid Captures
Region for the injected language. This marks the node(s) whose content will be parsed with the injected language.
Dynamic detection of the injection language. The text of the captured node describes the language name.
Indicates that the captured node’s text may contain a filename. The corresponding filetype is looked up via
vim.filetype.match() and used as the language name.Basic Patterns
Static Language Injection
When you know the language ahead of time, use the#set! directive:
re module function calls.
Dynamic Language Injection
When the language is specified in the source code, use@injection.language:
Filename-based Injection
When the language should be determined by filename:Advanced Techniques
Combined Injection
Combine multiple nodes into a single injection using the#set! injection.combined directive:
Self Injection
Sometimes you need to re-parse content with the same language:Injection with Include Children
By default, only the captured node’s text is injected. To include child nodes:Real-World Examples
Python Regex Injection
Python Printf-style Formatting
JavaScript Template Literals
HTML Script Tags
Markdown Code Blocks
Comment Injection
Lua Heredocs
Directives Reference
#set! injection.language
Explicitly sets the language for the injection:
#set! injection.combined
Combines multiple matches into a single injection region:
#set! injection.include-children
Includes all child nodes in the injection, not just the direct text:
#offset!
Adjusts the boundaries of the injection region:
#offset! @capture start_row start_col end_row end_col
Inheriting Injections
Like other query files, you can inherit injection queries from a base language:Language Names
The language name used in injections must match:- A Tree-sitter parser installed via
:TSInstall - A filetype registered with
vim.treesitter.language.register()
javascript,typescript,python,lua,rust,go,c,cpphtml,css,json,yaml,tomlsql,graphql,regexbash,vim,markdowncomment(special language for comment-specific features)
Testing Injections
To test your injection queries:- Use
:InspectTreeto see the parsed tree and verify nodes are captured correctly - Use
:EditQuery injectionsto test patterns interactively - Check that the injected language’s parser is installed with
:TSInstall <language> - Use
:Inspectto see which highlight groups are applied
If injections aren’t working:
- Verify the injected language’s parser is installed
- Check that
@injection.contentcaptures the right nodes - Ensure the language name matches exactly
- Look for conflicting patterns that might be overriding your injection
Performance Considerations
- Keep injection patterns specific to avoid unnecessary re-parsing
- Use predicates to narrow matches (e.g.,
#eq?,#match?) - Avoid overly broad patterns that match too many nodes
- Consider the performance impact of deeply nested injections