Vim mode

Vim mode brings Vim-style modal editing to text notes: the motions, operators and commands of the Vim text editor, in Trilium's own editor. It understands the structure of rich text: a paragraph, a heading, a list item or a table cell is a line, and so is an image or an included note.

The most used keys are in the Cheat sheet below, and all of them in the Vim full reference.

Enabling Vim mode

  1. Go to Options → Text Notes → Features.
  2. Turn on Vim mode.

The option applies to the current device only, so repeat these steps on each device where you want Vim mode. It takes effect at once, without a reload. Code notes have an option of their own: Options → Code Notes → Editor → Vim keybindings.

Once Vim mode is on:

  • Newly opened text notes start in Normal mode: press i to type, and Esc to return to the commands.
  • The notes that were already open when Vim mode was turned on switch to Insert mode, so you can continue typing.
  • In Insert mode, the regular editor stays available, with its formatting, slash commands, Markdown-like formatting and toolbars. A few shortcuts follow Vim there too, see Shortcuts that change in the editor below.

Where Vim mode shows

Vim mode uses three places of the Status bar, at the bottom of the window:

  • The Vim badge, before the other indicators of the status bar, is highlighted while Vim mode is on for the current note. Click it to turn Vim mode off or on for that note, see Vim mode for a single note below. Right-click it for a menu with the same switch, a switch for the key hints, the settings and this page.
  • The mode indicator, after the badge, shows the current mode: NORMAL, INSERT, VISUAL, VISUAL LINE or REPLACE. Beside it appear the keys of a command that is not complete yet, short messages and the macro being recorded.
  • The area of the Breadcrumb is used for two things: the key hints, described below, and the command line, which opens there when you press :, / or ?.

In Zen mode, the key hints, the Vim badge and the mode indicator are visible.

Settings

The settings are in Options → Text Notes → Features. The last three appear below Vim mode once it is turned on. Vim mode itself is kept per device, while the other three are synchronized.

SettingDefaultApplies toWhat it does
Vim modeOffThis deviceTurns Vim mode on in text notes.
Key hintsOnAll your devicesShows the keys of the current mode, the keys that can follow a command and the suggestions of the command line. See Key hints below.
Vim keys in Insert modeOffAll your devicesMakes Ctrl+U, Ctrl+E, Ctrl+Y, Ctrl+K, Ctrl+V and Ctrl+A follow Vim while typing. When off, they keep the meaning the editor gives them, such as underline for Ctrl+U and paste for Ctrl+V.
Startup commandsEmptyAll your devicesRuns commands when Vim mode starts, mostly to set up key mappings.

Startup commands

Write one command per line, as it would be typed after :. A line that starts with " is a comment.

" Leave Insert mode by typing jk
imap jk <Esc>
" Make Y yank to the end of the line
nmap Y y$

When you edit the list, the commands run again as soon as you leave the field, and their mappings replace those of the previous list. Changing the other settings does not run them again. Only the commands of Vim mode are understood, so a .vimrc file made for Vim does not work as it is.

Vim mode for a single note

To decide for one note, whatever the option says, add a label to it:

  • #vimMode turns Vim mode on in that note.
  • #vimMode=false keeps that note in the regular editor.

The label can be inherited, so one label can cover a whole subtree. A label set on the note itself wins over an inherited one: with an inheritable #vimMode on a parent note, a child note that has #vimMode=false stays in the regular editor.

While the Vim mode option is on, the Vim badge in the status bar sets this label for you: a click on it gives the current note #vimMode=false, and another click removes that label again, or sets #vimMode=true where the note would inherit #vimMode=false. When the label of the note is inheritable, the click changes Vim mode for the notes under it too, and its menu says so.

Key hints

Key hints help while you learn, and can be turned off once the keys are familiar. They show in the status bar, in the area of the breadcrumb:

  • In Normal and Visual mode, the most used keys of the mode take the place of the breadcrumb. To switch between the keys and the breadcrumb, press Esc in Normal mode. This is the last thing Esc does: when something else applies, such as search highlights to clear, that happens first and it takes another press. See The Esc key below.
  • After a pause in the middle of a command, such as after d or g, the keys that can follow are shown.
  • The command line suggests the commands that match what is typed, and Tab or the arrow keys complete them. The search prompt tells how the pattern is read.
  • To see every key of the current mode, click the mode indicator or press Alt+F1.

To turn the hints off, use the Key hints setting or type :set nohints. :set hints turns them back on.

Cheat sheet

These are the keys and commands used most. Keys typed one after another are written together, such as dd. Unless stated otherwise, they are typed in Normal mode.

Modes

  • i / a: type before / after the cursor.
  • I / A: type at the start / end of the line.
  • o / O: open a line below / above and type.
  • v / V: select characters / whole lines.
  • R: type over the existing text.
  • Esc: return to Normal mode.

Moving

  • h j k l: left, down, up, right.
  • w / b / e: next word, previous word, end of the word.
  • 0 / ^ / $: start of the line, first character, end of the line.
  • gg / G: first / last line of the note. 12G goes to line 12.
  • { / }: previous / next block.
  • f and a character: jump to that character in the line. ; repeats the jump.
  • %: go to the matching bracket.
  • Ctrl+D / Ctrl+U: half a page down / up.
  • Ctrl+O / Ctrl+I: back / forward through the places you jumped from.

Editing

  • x: delete the character under the cursor.
  • dd / dw / D: delete the line, the word, the rest of the line.
  • cc / cw / C: change the line, the word, the rest of the line.
  • ciw: change the word under the cursor, wherever the cursor is in it.
  • r and a character: replace one character.
  • J: join the next line to this one.
  • >> / <<: indent / outdent the line.
  • ~: switch between upper and lower case.
  • u / Ctrl+R: undo / redo.
  • .: repeat the last change.
  • A number repeats a command: 3dd deletes three lines, 2w moves two words.

Copying and pasting

  • yy / yw / y$: yank (copy) the line, the word, the rest of the line.
  • p / P: put (paste) after / before the cursor.
  • "ayy / "ap: yank the line into the register a / put from it.
  • "+y / "+p: copy to / paste from the system clipboard.

Yanked text keeps its formatting, such as bold, links and colors.

Selecting

  • v / V: start selecting characters / lines. Any motion extends the selection.
  • iw, ip, i", i(: select a word, a block, the text inside quotes or parentheses. a in place of i also takes what surrounds it.
  • y / d / c: yank, delete or change the selection.
  • > / <: indent / outdent the selected lines.
  • o: jump to the other end of the selection.
  • gv: select the previous selection again.

Searching and replacing

  • /text / ?text: search forward / backward, then press Enter.
  • n / N: next / previous match.
  • * / #: search for the word under the cursor, forward / backward.
  • :s/old/new/: replace in the current line.
  • :%s/old/new/g: replace everywhere in the note. Add c to confirm each match.
  • :noh: clear the search highlights.

Trilium-specific features

  • :w: save the note now. Trilium also saves by itself.
  • :q or ZZ: close the tab of the note, or its split.
  • gf: follow the link or link preview under the cursor.
  • ]] / [[: go to the next / previous heading.
  • za: collapse or expand the heading, list item or collapsible block at the cursor.
  • :sort: sort the selected paragraphs or list items, or the ones around the cursor.
  • :ck bold: run a command of the text editor by its name, here on the selection.
  • i or a on an image or an included note: edit its caption or its content.
  • Alt+F1: list every key of the current mode.

For the other keys and commands, see Vim full reference.

How Vim reads a text note

Vim works with lines, and a text note has none of its own. This is how the two fit together:

  • Each paragraph, heading, paragraph of a list item, paragraph of a table cell and line of a code block is one line, however many lines it takes on the screen. This is sometimes called a logical line, as opposed to a screen line. dd, yy, j and k act on logical lines, while gj and gk move by screen line.
  • An image, a diagram, a horizontal rule or an included note is also one line: j and k stop on it and dd deletes it. i or a enters its caption or its content. To leave an included note, press Esc in its Normal mode, k on its first line or j on its last line.
  • o and O add what Enter would add at that place, such as a paragraph, a list item or a code line. J joins lines the way Backspace does. >> and << change the level of a list item or the indentation of a paragraph.
  • {, }, ip and ap work with blocks instead of blank lines: a paragraph, a list item, a quote, a table cell, a code block. On a heading, ap is the heading together with its section.
  • In a table, h and l stay inside the cell, and j and k move down and up the column.
  • A collapsed heading or list item counts as one line for j and k. Searching also looks inside it, and expands the one that holds a match.
  • Registers are shared by all text notes, but not with code notes. To carry text between the two, use the clipboard register "+.

Shortcuts that change in the editor

Vim uses some of the Ctrl shortcuts of Trilium. While the cursor is in a text note, Vim's meaning wins, as it already does in code notes. The last column shows Insert mode with the Vim keys in Insert mode setting off, which is the default:

ShortcutWithout Vim modeIn Normal and Visual modeIn Insert mode
Ctrl+RReload the app (F5 still does)RedoInsert the content of a register
Ctrl+OCreate a note after the current oneGo back to the previous jumpRun one Normal-mode command
Ctrl+PCreate a child noteMove up a line, as kNothing
Ctrl+FSearch in note (desktop app)Scroll a page downUnchanged
Ctrl+B, Ctrl+I, Ctrl+UBold, italic, underlineScroll a page up, go forward to the next jump, scroll half a page upUnchanged
Ctrl+A, Ctrl+XSelect all, cutIncrease, decrease the number at the cursorUnchanged
Ctrl+Z, Ctrl+Shift+ZUndo, redoThe same as u and Ctrl+RUnchanged
Ctrl+WClose the tabNothingDelete the word before the cursor
Ctrl+T, Ctrl+DOpen a new tab, nothingUnchanged, scroll half a page downIndent, outdent the line
Ctrl+CCopyCopy the selection and leave Visual mode. With nothing selected, the same as EscCopy the selection. With nothing selected, leave Insert mode
/ and :Slash commands and emoji completionSearch and the command lineUnchanged

With Vim keys in Insert mode turned on, six more shortcuts follow Vim while typing. In Normal and Visual mode, the setting changes nothing.

ShortcutIn Insert mode, setting offIn Insert mode, setting on
Ctrl+UUnderlineDelete from the cursor to the start of the line
Ctrl+ENothingInsert the character that is below the cursor
Ctrl+YRedoInsert the character that is above the cursor
Ctrl+KCreate a linkStart a digraph, a character typed as two keys
Ctrl+VPasteInsert the next key as it is
Ctrl+ASelect allInsert the last inserted text again

To paste while the setting is on, press Esc first: in Normal and Visual mode, Ctrl+V always pastes.

  • Shortcuts with Alt, with Ctrl+Shift or Ctrl+Alt, and the function keys keep their meaning in every mode, apart from Ctrl+Shift+Z above. So do the shortcuts that Vim does not use, such as Ctrl+L for a link to a note. Ctrl+K for a link is one of them too, except in Insert mode with the setting above turned on.
  • On macOS, the shortcuts of Trilium use ⌘ and those of Vim use Ctrl, so nothing is taken over. ⌘+Z and ⌘+Shift+Z undo and redo.
  • In a browser on Windows and Linux, Ctrl+W, Ctrl+T and Ctrl+N always belong to the browser.

The Esc key

Esc does one thing per press, the first of these that applies:

  1. Closes what is open: the command line or the search prompt, a popup such as the link balloon, a completion list or the list of keys.
  2. Cancels the keys typed so far of a command.
  3. Leaves Visual or Insert mode.
  4. Clears the search highlights.
  5. In an included note, returns to the note around it.
  6. Switches between the key hints and the breadcrumb, when the key hints are on.

So with a link balloon open in Insert mode, the first press closes the balloon and the second one leaves Insert mode. Ctrl+[ does the same as Esc.

Limitations

  • Vim mode imitates Vim, it does not run Vim itself. Plugins and scripts made for Vim are not available, and some of its commands are missing: see Not supported in the Vim full reference.
  • Vim mode is not available in the mobile layout, and read-only notes have no modes.
  • Visual Block mode is not supported. Ctrl+V pastes, except in Insert mode when Vim keys in Insert mode is on.
  • Search patterns are JavaScript regular expressions, not Vim's own, and a match cannot span more than one line.
  • :q! and ZQ only show a message: notes are saved as you type, so there is nothing to discard.

See also