PyCharm 2024.1 Help

Language injections

Language injections let you work with pieces of code in other languages embedded in your code. When you inject a language (such as HTML, CSS, XML, RegExp, and so on) into a string literal, you get comprehensive coding assistance for the injected code even though it is not the main language of your project.

Temporarily inject a language

By default, the IDE injects a language temporarily. It means that if you delete the embedded element, the injection will be lost as well.

  1. Place the caret inside the string literal, tag, or attribute, in which you want to inject a language and press Alt+Enter (or use the intention action icon Intention action icon).

  2. Select Inject language or reference and choose the language you want to inject.

    Inject HTML code in a Python script
  3. Type some code. Note that code completion if available for the language constructs:

    Code completion in the injected language fragment

Use language injection comments

  • Add a blank line before the target string literal, and type the following comment:

    # language=<language_ID>
    HTML injection comment

For comments, use the syntax of the language you want to inject. Language IDs are generally intuitive, for example, SQL, RegExp, XML, HTML.

You can also learn language IDs in settings. Press Ctrl+Alt+S to open the IDE settings and then select Editor | language Injections. Double-click an injection rule for a language; the language ID is specified in the ID field.

A language fragment may be combined with a prefix and a suffix that act together as a wrapper, turning the fragment into a syntactically complete language unit. When editing your code, you can see prefixes and suffixes only in the fragment editor. They are not shown in the main editor.

The prefix and the suffix can be included in the injection comment as follows:

# language=<language_ID> prefix=<prefix> suffix=<suffix>
Using prefixes and suffixes in language injection

Open an injection editor

  1. Place the caret within the injected code piece and press Alt+Enter (or use the intention action icon Intention action icon).

  2. Select Edit <language ID> Fragment.

    PyCharm will open a dedicated editor section for editing the code with the injected language. This editor provides full coding assistance, including code completion, inspections, intentions and code style actions.

    Edit the HTML fragment in the dedicated editor

Cancel injections

  1. Place the caret at the code fragment and press Alt+Enter (or use the intention action icon the Intention action button).

  2. Select Uninject language or reference.

To cancel a language injection, you can also delete the injection comment or annotation.

Configure rules for automatic injections

In PyCharm, there is a set of predefined rules according to which the IDE automatically injects languages in certain places in your code.

You can configure language injection rules on the Editor | Language Injections page of the IDE settings  Ctrl+Alt+S.

Language injection settings

All pre-defined injection rules are configured for the Built-in scope. In other words, they are global (and therefore available in all PyCharm projects). Custom rules can be configured for the IDE or for one project only. To change the scope of custom injections, use the the Move to Project/IDE scope button.

To share a custom rule through VCS, move it to the Project scope using the corresponding option on the toolbar. By doing so, you will create a new file in the .idea directory named IntelliLang.xml with your custom rules that you can place under version control.

If you don’t use a VCS, you can share your rules by exporting them to XML files (click Export on the toolbar) and then importing them to another project.

To configure custom injection rules, click the Add button to add a new rule, or copy a predefined rule and change its settings.

Last modified: 05 April 2024