Code blocks

The code blocks feature allows entering pieces of code in text notes.
Note that this feature is meant for generally small snippets of code. For larger files such as an entire log, see the Code note type instead.
Inserting a code block
- Via the Formatting toolbar, look for the button.
- Pressing directly on the icon will insert a code block with the language that was selected most recently. If this is the first time a code block is inserted, the language will be “Auto-detected” by default.
- Pressing the arrow next to the icon, which will show a popup with the available languages.
- Type
```(as in Markdown).- Note that it's not possible to specify the language, as it will default to the last selected language.
- Paste several lines of code copied from Visual Studio Code.
- The code block uses the language of the file in Visual Studio Code.
- If that language is not in the list of languages, the code block uses Auto-detected.
- The colors of the Visual Studio Code theme are removed.
- A single line becomes inline code instead of a code block.
- In Firefox, the code block always uses Auto-detected. Firefox can't read the language from Visual Studio Code.
Exiting out of the code block
- To exit out of a code block and enter a normal paragraph, move the cursor at the end of the code block and press Enter twice.
- Similarly, to insert a paragraph above the note block, move the cursor at the beginning of the code block and press Enter twice.
Syntax highlighting & color schemes
Since TriliumNext v0.90.12, Trilium will try to offer syntax highlighting to the code block. Note that the syntax highlighting mechanism is slightly different than the one in Code notes as different technologies are involved.
Interaction:
- When the language is set to Auto-detected (by default), Trilium will try to identify the programming language (or similar) that corresponds to the given snippet of text and highlight it. Only the languages enabled in Options → Code Notes → Available MIME types in the dropdown are considered. If this is problematic, consider changing the language of the code block manually.
- When the language is set to Plain text, there will be no syntax highlighting.
Note that when editing a text note, syntax highlighting is automatically disabled if the code block is too big (somewhere around 500 lines). In Read-Only Notes, in print and on a shared page, a code block of more than 500 lines or 50,000 characters is shown without syntax highlighting. These limits are not configurable.
In order to configure this new feature, a section has been added in Options → Appearance to control the syntax highlighting. There the color scheme can be chosen, from a builtin selection of themes from Highlight.js. VS Code (Light) and VS Code (Dark) use the same colors as the default themes of Code notes, so a code block and a code note color the same code alike.
- It is possible to disable the syntax highlighting for all the notes by selecting “No syntax highlighting” in the “Color scheme” option.
- Word wrapping is disabled by default, but can be configured from the same section.
- The tab width can also be adjusted from Options.
Migrating from existing syntax highlight plugins
If you are already using a syntax highlighting plugin such as the Trilium-SyntaxHighlightWidget we are basing off of, it is important to disable that plugin before upgrading in order for it not to conflict with our implementation.
Should you encounter any issues after the migration, try running Trilium in safe mode.
Changing the language of a code block
Simply click anywhere inside the code block and press again the code block button in the Formatting toolbar:
Adjusting the list of languages
The code blocks feature shares the list of languages with the Code note type.
The supported languages, and the order in which they are listed, can be adjusted by going to Options → Code Notes → Available MIME types in the dropdown. See Code for how to add, remove and reorder them.
Note that the list of languages is not immediately refreshed, you'd have to manually refresh the application.
Text replacements and ligatures
Automatic replacements are a feature of text notes which converts some character sequences into another and some of them interfere with programming text, such as the conversion of quotes. While in a code block, the automatic replacements are not effect. The only way the automatic replacements can interfere is if the text is typed/pasted before being put in a code block.
A different feature is programming ligatures, which is simply a display affordance which turns != into ≠ or -> as → . These come from the font. The underlying text is not actually changed, and copying it out gives you back != and ->.
Ligatures can be disabled in Options→ Appearance → Fonts → Programming ligatures.
Extended code blocks
An extended code block is a code block that is edited with the code editor of a Code note, inside the text. It has the same syntax highlighting and code folding as a code note, and it suits longer pieces of code.
- To insert one, open the Insert menu of the Formatting toolbar and choose Extended code block, after Columns. It is also available as Extended code block in the Slash Commands.
- A new block is empty and editable, in a Full box, and takes the focus. Its language is the one set last on an extended code block, or Plain text.
- A click places the cursor in the code, as in a code block. Over a Small or Medium box, the mouse wheel scrolls the code.
- The block shows in a box like the one of an included code note, with the corners and the shadow of a code block. While the block is editable, its language shows in its top corner, as for a code block. While it is read-only, because Editable is unchecked or the note is one of the Read-Only Notes, that corner has a Copy to clipboard button instead, as a code block of a read-only note does.
- Its toolbar has the Change language mode and Change tab width menus of an embedded code file, described in Editing an include in place in Include Note, and the Copy to clipboard button, which copies the code as the one of a code block does. Its More actions menu has Editable, which shows the code read-only once unchecked, the Word wrap and Line numbers submenus, the Size submenu, with Resizable, Full and Default, the Position submenu, and Delete block, which removes the block from the note.
- The Position submenu places the block as it places an include, described in Positioning in Include Note: on lines of its own at the left, the center or the right, or at the left or the right with the text running beside it. Only a Resizable box can be placed, and it takes the full width of the note until it is resized narrower. In the mobile layout, a block with a position other than Align center takes the full width.
- The changes made in the block are part of the undo history of the text note.
The code stays in the note as a code block of its language. The Markdown export, shared pages and printing show it as a code block, and copying it into another application pastes a code block. Pasted into a text note, it becomes an extended code block again, with its settings.
Turning Extended code blocks off again removes them from the Insert menu and the slash commands only: the extended code blocks already in notes keep working. A version of Trilium without this feature shows them as code blocks, and saves them back as code blocks, without their box size and other settings.