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
- Go to Options → Text Notes → Features.
- 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
ito 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 LINEorREPLACE. 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.
| Setting | Default | Applies to | What it does |
|---|---|---|---|
| Vim mode | Off | This device | Turns Vim mode on in text notes. |
| Key hints | On | All your devices | Shows 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 mode | Off | All your devices | Makes 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 commands | Empty | All your devices | Runs 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:
#vimModeturns Vim mode on in that note.#vimMode=falsekeeps 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
dorg, 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
hjkl: 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.12Ggoes to line 12.{/}: previous / next block.fand 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.rand 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:
3dddeletes three lines,2wmoves 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 registera/ 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.ain place ofialso 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. Addcto confirm each match.:noh: clear the search highlights.
Trilium-specific features
:w: save the note now. Trilium also saves by itself.:qorZZ: 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.ioraon 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,jandkact on logical lines, whilegjandgkmove by screen line. - An image, a diagram, a horizontal rule or an included note is also one line:
jandkstop on it anddddeletes it.ioraenters its caption or its content. To leave an included note, press Esc in its Normal mode,kon its first line orjon its last line. oandOadd what Enter would add at that place, such as a paragraph, a list item or a code line.Jjoins lines the way Backspace does.>>and<<change the level of a list item or the indentation of a paragraph.{,},ipandapwork with blocks instead of blank lines: a paragraph, a list item, a quote, a table cell, a code block. On a heading,apis the heading together with its section.- In a table,
handlstay inside the cell, andjandkmove down and up the column. - A collapsed heading or list item counts as one line for
jandk. 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:
| Shortcut | Without Vim mode | In Normal and Visual mode | In Insert mode |
|---|---|---|---|
| Ctrl+R | Reload the app (F5 still does) | Redo | Insert the content of a register |
| Ctrl+O | Create a note after the current one | Go back to the previous jump | Run one Normal-mode command |
| Ctrl+P | Create a child note | Move up a line, as k | Nothing |
| Ctrl+F | Search in note (desktop app) | Scroll a page down | Unchanged |
| Ctrl+B, Ctrl+I, Ctrl+U | Bold, italic, underline | Scroll a page up, go forward to the next jump, scroll half a page up | Unchanged |
| Ctrl+A, Ctrl+X | Select all, cut | Increase, decrease the number at the cursor | Unchanged |
| Ctrl+Z, Ctrl+Shift+Z | Undo, redo | The same as u and Ctrl+R | Unchanged |
| Ctrl+W | Close the tab | Nothing | Delete the word before the cursor |
| Ctrl+T, Ctrl+D | Open a new tab, nothing | Unchanged, scroll half a page down | Indent, outdent the line |
| Ctrl+C | Copy | Copy the selection and leave Visual mode. With nothing selected, the same as Esc | Copy the selection. With nothing selected, leave Insert mode |
/ and : | Slash commands and emoji completion | Search and the command line | Unchanged |
With Vim keys in Insert mode turned on, six more shortcuts follow Vim while typing. In Normal and Visual mode, the setting changes nothing.
| Shortcut | In Insert mode, setting off | In Insert mode, setting on |
|---|---|---|
| Ctrl+U | Underline | Delete from the cursor to the start of the line |
| Ctrl+E | Nothing | Insert the character that is below the cursor |
| Ctrl+Y | Redo | Insert the character that is above the cursor |
| Ctrl+K | Create a link | Start a digraph, a character typed as two keys |
| Ctrl+V | Paste | Insert the next key as it is |
| Ctrl+A | Select all | Insert 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:
- 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.
- Cancels the keys typed so far of a command.
- Leaves Visual or Insert mode.
- Clears the search highlights.
- In an included note, returns to the note around it.
- 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!andZQonly show a message: notes are saved as you type, so there is nothing to discard.
See also
- Vim beginner guide, for the modes and the first keys to learn.
- Vim full reference, for all the keys and commands.