vimtricks.wiki Concise Vim tricks, one at a time.

How do I edit multiple lines at once using multiple cursors in Vim?

Answer

<C-n>

Explanation

The vim-visual-multi plugin (formerly vim-multiple-cursors) brings VS Code-style multiple cursor editing to Vim. Press <C-n> on a word to select it, press <C-n> again to select the next occurrence, and then edit all selections simultaneously — making bulk renaming and repetitive edits dramatically faster.

How it works

The plugin operates in two modes: cursor mode (multiple cursors with individual positions) and extend mode (multiple visual selections). You switch between them with <Tab>.

Basic workflow

  1. Place your cursor on a word
  2. Press <C-n> to select the word and create the first cursor
  3. Press <C-n> again to add the next occurrence to the selection
  4. Repeat until all desired occurrences are selected
  5. Press c, s, I, A, or any editing command to modify all selections at once

Example

Given this code:

const user = getUser();
console.log(user.name);
console.log(user.email);
return user;

With cursor on the first user, press <C-n> four times to select all four occurrences. Then press c and type account — all four instances change simultaneously to account.

Key mappings

<C-n>          " select the word under cursor / add next occurrence
<C-Down>       " create a cursor on the line below
<C-Up>         " create a cursor on the line above
q              " skip the current match and jump to the next one
Q              " remove the current cursor/selection
<Tab>          " switch between cursor mode and extend mode
<Esc>          " exit visual-multi mode

Selecting all occurrences at once

Instead of pressing <C-n> repeatedly, select every occurrence in the file in one step:

  1. Press <C-n> to select the first occurrence
  2. Press <M-A> (Alt+Shift+A) to select all remaining occurrences in the buffer

Then type your replacement and every instance updates simultaneously.

Creating cursors from visual selection

Select a block of lines with V (linewise visual mode), then press <C-n> to create a cursor at the same column on every selected line. This is perfect for adding the same text to multiple lines.

Regex selection

Press \/ to enter a regex pattern. Every match in the file gets a cursor, letting you perform complex multi-cursor edits based on patterns rather than exact words.

Alignment

With multiple cursors active, press \a to align all cursors at the same column. This is useful for aligning assignments, declarations, or table data across multiple lines.

Why not just use :s or macros?

Multiple cursors shine when:

  • You need visual feedback while editing — you see all changes live as you type
  • The changes are irregular — not every occurrence needs the same transformation
  • You want to skip some occurrences selectively with q
  • You need to edit different text at each cursor position using cursor mode

Tips

  • Use q to skip an occurrence you do not want to modify — this is the equivalent of pressing "no" in a :s///gc confirmation
  • Press <Tab> to switch between cursor mode and extend mode — extend mode lets you use visual-mode operations on all selections
  • All standard Vim operators work in visual-multi mode: d, c, y, ~, gu, gU, >, <
  • The plugin provides its own undo history, so u undoes the last multi-cursor edit correctly
  • For simpler cases, consider using cgn (built-in Vim) with . repeat — it covers many of the same use cases without a plugin
  • The plugin supports dot-repeat natively — after a multi-cursor edit, pressing . in normal mode repeats the entire multi-cursor operation

Next

How do I run tests directly from Vim without switching to a terminal?