IntelliJ syntax highlighting for jinja files (.jinja, .j2)

Viewed 14758

Question is pretty much in the title. Does IntelliJ have native syntax highlighting for .jinja and .j2 files?

If not is there a plugin?

4 Answers

Try the Python plugin with IntelliJ IDEA Ultimate, that has support for Jinja2 Templates.

This plugin: https://plugins.jetbrains.com/plugin/7792-yaml-ansible-support offers some support for jinja files.

To ensure that this plugin is associated with jinja files:

  • Go to File > Preferences > Editor > File Types
  • Under Recognized File Types scroll down to YAML/Ansible and select it
  • Under Registered Patterns click the + icon and enter *.jinja.

    enter image description here

With this configuration in place your jinja files will open into this plugin. If they do not, then jinja files must be already associated with one of the other Recognized File Types in which case you'll need to scroll through them to find the culprit and remove *.jinja from its Registered Patterns.

With recent version of Intellij, you don't need any plugin any more.

  • Go to File > Settings > Editor > File Types
  • Under Recognized Files Types, choose Jinja 2 Template
  • Under File Name Patterns add *.j2 for jinja2, and/or *.jinja for jinja

For Ansible yaml jinja 2 templates:

  • Under Recognized Files Types, choose YAML/Ansible
  • Under File Name Patterns add *.yaml.j2 and *yml.j2

Just for sanity sake. I found my solution in a combination of both answers previously posted.

Besides the Python plugin as @Andrei states it is needed that the file extension is recognized as explained above by @glytching: File > Preferences > Editor > File Types, for me the pattern *.j2 was missing.

As a good thing to do also would be to mark the directory as Template as described in section To define template directories on the referenced templates link given by @Andrei:

1. In the Settings/Preferences dialog, click the Project Structure page.

2. Choose the directory to be marked as a template root.

3. Do one of the following:

  * Click Templates on the toolbar of the Content roots pane.

  * Choose Templates on the directory's context menu. 
Related