Home

Awesome

vscode-multi-cursor

This plugin will help you create multiple selections (multi-cursor) in VSCode.

Installation

Install the plugin with your preferred package manager:

lazy.nvim:

{
  'vscode-neovim/vscode-multi-cursor.nvim',
  event = 'VeryLazy',
  cond = not not vim.g.vscode,
  opts = {},
}

Configuration

Default options:

require('vscode-multi-cursor').setup { -- Config is optional
  -- Whether to set default mappings
  default_mappings = true,
  -- If set to true, only multiple cursors will be created without multiple selections
  no_selection = false
}

Default mappings:

local cursors = require('vscode-multi-cursor')

local k = vim.keymap.set
k({ 'n', 'x' }, 'mc', cursors.create_cursor, { expr = true, desc = 'Create cursor' })
k({ 'n' }, 'mcc', cursors.cancel, { desc = 'Cancel/Clear all cursors' })
k({ 'n', 'x' }, 'mi', cursors.start_left, { desc = 'Start cursors on the left' })
k({ 'n', 'x' }, 'mI', cursors.start_left_edge, { desc = 'Start cursors on the left edge' })
k({ 'n', 'x' }, 'ma', cursors.start_right, { desc = 'Start cursors on the right' })
k({ 'n', 'x' }, 'mA', cursors.start_right, { desc = 'Start cursors on the right' })
k({ 'n' }, '[mc', cursors.prev_cursor, { desc = 'Goto prev cursor' })
k({ 'n' }, ']mc', cursors.next_cursor, { desc = 'Goto next cursor' })
k({ 'n' }, 'mcs', cursors.flash_char, { desc = 'Create cursor using flash' })
k({ 'n' }, 'mcw', cursors.flash_word, { desc = 'Create selection using flash' })

You can refer to customize the mappings.

Usage

[!Important] This plugin is purely for assisting in creating multiple cursors in vscode and does not include any editing functionality.

All examples use the default mappings.

The basic usage flow is as fllows: 1. Add cursors 2. Start editing

Add selections (cursors)

It can be used in operator pending mode.

For example, you can use mciw to add a selection of the word under the cursor, use mcl to add the current cursor position, mce mcj, etc.

You can define selections and cursors by selecting any content in visual mode and then pressing mc.

basic

Tips:

  1. Add current word range and go to next
vim.keymap.set('n', '<C-d>', 'mciw*<Cmd>nohl<CR>', { remap = true })
<!-- vim.keymap.set('x', '<C-d>', [[y/\V<C-r>=escape(@",'/\')<CR><CR>gNmcgn<Cmd>nohl<Cr>]], { remap = true }) -->

C-d

Start editing

Note You can press above keys in visual mode to start editing directly.

All start functions accept an optional options. Fields are the same as the plugin options.

For example, remapping vim's I and A in visual mode:

local k = vim.keymap.set
k({ 'x' }, 'I', function()
    local mode = api.nvim_get_mode().mode
    M.start_left_edge { no_selection = mode == '\x16' }
end)
k({ 'x' }, 'A', function()
    local mode = api.nvim_get_mode().mode
    M.start_right { no_selection = mode == '\x16' }
end)

Clear all selections (cursors)

You can clear the existing cursor range by redefining the cursor within the existing cursor range.

clear

Navigate through cursors

Flash integration

You need to install folke/flash.nvim first.

Wrapped VSCode commands

[!Important] It is recommended to use vscode-neovim's vscode.with_insert.

Wraped some VSCode commands used for multi-cursor to make them work properly.

local C = require 'vscode-multi-cursor'

For example, use <C-d> to add selection to next find match:

nvim config:

vim.keymap.set({ "n", "x", "i" }, "<C-d>", function()
  require("vscode-multi-cursor").addSelectionToNextFindMatch()
end)

vscode keybindings.json:

{
  "args": "<C-d>",
  "command": "vscode-neovim.send",
  "key": "ctrl+d",
  "when": "editorFocus && neovim.init"
}

Highlights

GroupDefaultDescription
VSCodeCursorbg: #177cb0 fg: #ffffffLeft and right cursor
VSCodeCursorRangebg: #48c0a3 fg: #ffffffSelected region