Visual mode is one of Vim’s most powerful features, allowing you to select text and perform operations on the selection. CodeMirror Vim supports all three types of visual mode: character-wise, line-wise, and block-wise.
CodeMirror Vim implements three visual modes, matching standard Vim behavior:
1
Character-wise Visual Mode (v)
Select characters individually. Press v in normal mode to enter.
// In normal mode, press:// v - enter visual mode// motion commands (h, j, k, l, w, e, etc.) to extend selection// operator (d, y, c, etc.) to act on selection
Use cases:
Selecting specific words or characters
Precise text manipulation
Selecting parts of lines
2
Line-wise Visual Mode (V)
Select entire lines at a time. Press V (Shift+v) in normal mode.
// In normal mode, press:// V - enter visual line mode// j/k to select more lines// operator to act on complete lines
Use cases:
Deleting multiple lines
Moving blocks of code
Indenting sections
3
Block-wise Visual Mode (Ctrl+v)
Select rectangular blocks of text. Press Ctrl+v in normal mode.
// In normal mode, press:// Ctrl+v (or Ctrl+q) - enter visual block mode// motion commands to define rectangle// operator to act on block
From normal mode, use these keys to enter different visual modes:
v - Character-wise visual mode
V - Line-wise visual mode
<Ctrl-v> - Block-wise visual mode
<Ctrl-q> - Alternative for block-wise (useful when Ctrl+v is paste)
gv - Reselect the last visual selection
// From the default keymap:{ keys: 'v', type: 'action', action: 'toggleVisualMode'}// Usage:// 1. Press 'v' in normal mode// 2. Use motions (w, e, $, etc.) to select// 3. Press operator (d, y, c) to act
// Delete 5 lines:// V - Enter line-wise visual// 4j - Select 5 lines total (current + 4 down)// d - Delete selection// Or use count directly:// V5j - Select 6 lines// d - Delete
If you are not using basicSetup, you must include the drawSelection plugin to correctly render visual mode selections.
From the README:
import { basicSetup, EditorView } from 'codemirror';import { vim } from "@replit/codemirror-vim";import { drawSelection } from "@codemirror/view";let view = new EditorView({ doc: "", extensions: [ vim(), // If NOT using basicSetup, add drawSelection: drawSelection(), // ... other extensions ], parent: document.querySelector('#editor'),});
The basicSetup bundle already includes drawSelection, so you only need to add it explicitly if you’re building a custom extension setup.
// From defaultKeymap:{ keys: 'gv', type: 'action', action: 'reselectLastSelection'}// Usage:// 1. Make a visual selection and perform operation// 2. Press 'gv' to reselect the same area
Your custom operators automatically work in visual mode:
import { Vim } from "@replit/codemirror-vim";Vim.defineOperator("uppercase", function(cm, operatorArgs, ranges) { for (const range of ranges) { const text = cm.getRange(range.anchor, range.head); cm.replaceRange(text.toUpperCase(), range.anchor, range.head); }});// Add to keymapdefaultKeymap.push({ keys: 'gU', type: 'operator', operator: 'uppercase'});// Now works with visual mode:// v3w - Select 3 words// gU - Make uppercase