Follow these steps to create a working CodeMirror editor with Vim keybindings.
1
Import required packages
Import the Vim extension and CodeMirror core packages:
import { basicSetup, EditorView } from 'codemirror';import { vim } from "@replit/codemirror-vim";
2
Create the editor
Initialize a new EditorView with the Vim extension:
let view = new EditorView({ doc: "", extensions: [ // make sure vim is included before other keymaps vim(), // include the default keymap and all other keymaps you want to use in insert mode basicSetup, ], parent: document.querySelector('#editor'),})
The vim() extension must be included before other keymaps in the extensions array. This ensures Vim keybindings take precedence in normal mode while allowing other keymaps to function properly in insert mode.
3
Add an editor container to your HTML
Create a DOM element where the editor will be mounted:
<div id="editor"></div>
4
Test Vim functionality
Open your application and verify Vim keybindings are working:
Here’s a full working example you can copy and paste:
import { basicSetup, EditorView } from 'codemirror';import { vim } from "@replit/codemirror-vim"let view = new EditorView({ doc: "", extensions: [ // make sure vim is included before other keymaps vim(), // include the default keymap and all other keymaps you want to use in insert mode basicSetup, ], parent: document.querySelector('#editor'),})