Ova

How to Multi-Cursor in Vim?

Published in Vim Editing 3 mins read

To use multi-cursor functionality in Vim, you can typically leverage specific keybindings provided by a plugin that extends Vim's capabilities. A common method involves using CTRL-D (on Windows/Linux) or CMD-D (on macOS), or alternatively gb, to add additional cursors and begin editing multiple locations simultaneously.

Understanding Multi-Cursor in Vim

Native Vim does not inherently support a multi-cursor feature like modern editors such as VS Code or Sublime Text. This powerful functionality is almost exclusively introduced through community plugins, which integrate seamlessly into your Vim workflow. These plugins allow you to select multiple instances of a word or pattern and edit them all at once, drastically speeding up refactoring and repetitive edits.

Activating Multi-Cursor

The primary way to initiate multi-cursor editing, as provided by popular plugins, involves a simple key combination:

  1. Start Selection: Position your cursor on the first word or text you wish to edit.
  2. Add First Cursor: Type CTRL-D (on Windows/Linux) or CMD-D (on macOS) while in Normal mode. Alternatively, you can use gb. This action will select the current word, add a cursor, and put Vim into Visual mode, ready for the next operation.
  3. Add Subsequent Cursors: To add more cursors, simply repeat CTRL-D / CMD-D or gb. Each time you press the key combination, another instance of the selected word will be highlighted, and a new cursor will be added there. Continue pressing until all desired instances are selected.
  4. Edit Simultaneously: Once all cursors are active, any text you type will be inserted at all cursor positions. You can also delete, change, or perform other common Vim operations on all selected areas simultaneously.
  5. Exit Multi-Cursor Mode: Press ESC to exit multi-cursor mode and return to normal Vim operation.

Key Multi-Cursor Commands

Here's a quick reference for the essential commands:

Command Operating System Action
CTRL-D Windows, Linux Add a cursor to the current word/selection.
CMD-D macOS Add a cursor to the current word/selection.
gb All (plugin dependent) Add a cursor to the current word/selection.
ESC All Exit multi-cursor mode and return to Normal mode.

Practical Applications and Examples

Multi-cursor is incredibly useful for:

  • Renaming Variables: Quickly change all occurrences of a variable name within a function or file.
  • Adding Prefixes/Suffixes: Insert self. before multiple method calls, or append ; to the end of several lines.
  • Batch Editing: Modify specific parts of structured data (e.g., changing values in a JSON array or SQL insert statements).
  • Commenting/Uncommenting: Add or remove comment markers from multiple lines that share a common pattern.

Example Scenario: Renaming a variable oldVar to newVar

  1. Place your cursor on an instance of oldVar.
  2. Press CTRL-D (or CMD-D / gb) repeatedly until all oldVar instances you want to change are highlighted.
  3. Press c (for "change") to delete the selected oldVar and enter Insert mode.
  4. Type newVar. All instances will be updated simultaneously.
  5. Press ESC to exit Insert mode and multi-cursor mode.

Extending Vim with Plugins

To gain multi-cursor functionality, you'll need to install a Vim plugin. Popular choices include:

  • vim-multiple-cursors: This is one of the most widely used plugins for this feature, and its keybindings (CTRL-D / CMD-D and gb) are exactly what the reference describes.
  • vim-visual-multi: Another robust option that offers extensive multi-cursor features.

You can install these plugins using a Vim plugin manager like Vim-Plug, Pathogen, or Vundle. For instance, with Vim-Plug, you would add Plug 'terryma/vim-multiple-cursors' to your .vimrc file and then run :PlugInstall in Vim.

Multi-cursor editing dramatically enhances productivity for repetitive tasks, making Vim an even more powerful tool for developers and writers alike.