Code blocks

Article Image

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 continue with a paragraph below a code block, move the cursor to the end of the code and press Enter three times. The first two presses add empty lines; the third removes them and moves the cursor to a new paragraph below the block.
  • To insert a paragraph above a code block, move the cursor to the beginning of the code and press Enter, which adds an empty first line. Move the cursor up to that line and press Enter again: the line is removed and the cursor moves to a new paragraph above the block.

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

Click anywhere inside the code block, then open the arrow next to the Code block button in the Formatting toolbar and pick the language.

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.

While the feature is on, every code block of a text note is an extended code block: the ones already in the notes, and the new ones.

  • To insert one, insert a code block in any of the ways described in Inserting a code block.
  • A new block is editable, in a Full box, and takes the focus. Its language is the one picked from the list of the button, otherwise the one set last on a code block, or Plain text.
  • The list of languages has no Auto-detected entry. A code block saved as Auto-detected takes the language that Trilium detects in its code, or Plain text, once its note is opened for editing. The language can then be changed from the toolbar of the block.
  • 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.
  • To leave the block for a new paragraph below or above it, press Enter at the end or the beginning of the code, as described in Exiting out of the code block. The arrow keys move the cursor out of the block from its first and last line, into the text around it. In an empty block, Backspace turns the block back into a paragraph.
  • 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 menu 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 Tab width submenu, with the items of the Change tab width menu of an embedded code file, 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.

Turning Extended code blocks off again shows the code blocks as before, as does a version of Trilium without this feature. Saving a note there drops the box size and the other settings of its blocks. An extended code block inserted from the Insert menu of Trilium 0.107 stays one while the feature is off.