Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jake-stewart/multicursor.nvim

multiple cursors in neovim
https://github.com/jake-stewart/multicursor.nvim

multicursor neovim neovim-plugin nvim nvim-plugin

Last synced: about 10 hours ago
JSON representation

multiple cursors in neovim

Awesome Lists containing this project

README

        

# multicursor.nvim

Multiple cursors in Neovim which work how you expect. Now with help pages! `:h multicursor`.

https://github.com/user-attachments/assets/a8c136dc-4786-447b-95c0-8e2a48f5776f

## Features

- Visual and select modes with char/line/block selections
- Normal, insert, replace modes
- Undo/redo
- Virtualedit
- Autocompletion
- Snippet expansion (use `vim.snippet.expand`)
- Cursor specific registers for searching and yanking
- Match & split cursor selections with regex
- Transpose cursor selections
- Align cursor columns
- Easily extended with the Cursor API
- Works with most plugins and remaps

## Example Config (lazy.nvim)

```lua
{
"jake-stewart/multicursor.nvim",
branch = "1.0",
config = function()
local mc = require("multicursor-nvim")

mc.setup()

local set = vim.keymap.set

-- Add or skip cursor above/below the main cursor.
set({"n", "v"}, "",
function() mc.lineAddCursor(-1) end)
set({"n", "v"}, "",
function() mc.lineAddCursor(1) end)
set({"n", "v"}, "",
function() mc.lineSkipCursor(-1) end)
set({"n", "v"}, "",
function() mc.lineSkipCursor(1) end)

-- Add or skip adding a new cursor by matching word/selection
set({"n", "v"}, "n",
function() mc.matchAddCursor(1) end)
set({"n", "v"}, "s",
function() mc.matchSkipCursor(1) end)
set({"n", "v"}, "N",
function() mc.matchAddCursor(-1) end)
set({"n", "v"}, "S",
function() mc.matchSkipCursor(-1) end)

-- Add all matches in the document
set({"n", "v"}, "A", mc.matchAllAddCursors)

-- You can also add cursors with any motion you prefer:
-- set("n", "", function()
-- mc.addCursor("w")
-- end)
-- set("n", "", function()
-- mc.skipCursor("w")
-- end)

-- Rotate the main cursor.
set({"n", "v"}, "", mc.nextCursor)
set({"n", "v"}, "", mc.prevCursor)

-- Delete the main cursor.
set({"n", "v"}, "x", mc.deleteCursor)

-- Add and remove cursors with control + left click.
set("n", "", mc.handleMouse)

-- Easy way to add and remove cursors using the main cursor.
set({"n", "v"}, "", mc.toggleCursor)

-- Clone every cursor and disable the originals.
set({"n", "v"}, "", mc.duplicateCursors)

set("n", "", function()
if not mc.cursorsEnabled() then
mc.enableCursors()
elseif mc.hasCursors() then
mc.clearCursors()
else
-- Default handler.
end
end)

-- bring back cursors if you accidentally clear them
set("n", "gv", mc.restoreCursors)

-- Align cursor columns.
set("n", "a", mc.alignCursors)

-- Split visual selections by regex.
set("v", "S", mc.splitCursors)

-- Append/insert for each line of visual selections.
set("v", "I", mc.insertVisual)
set("v", "A", mc.appendVisual)

-- match new cursors within visual selections by regex.
set("v", "M", mc.matchCursors)

-- Rotate visual selection contents.
set("v", "t",
function() mc.transposeCursors(1) end)
set("v", "T",
function() mc.transposeCursors(-1) end)

-- Jumplist support
set({"v", "n"}, "", mc.jumpForward)
set({"v", "n"}, "", mc.jumpBackward)

-- Customize how cursors look.
local hl = vim.api.nvim_set_hl
hl(0, "MultiCursorCursor", { link = "Cursor" })
hl(0, "MultiCursorVisual", { link = "Visual" })
hl(0, "MultiCursorSign", { link = "SignColumn"})
hl(0, "MultiCursorDisabledCursor", { link = "Visual" })
hl(0, "MultiCursorDisabledVisual", { link = "Visual" })
hl(0, "MultiCursorDisabledSign", { link = "SignColumn"})
end
}
```

## How to Use
This section explains the basic usage of multicursor.nvim with the default config.

#### Selecting Cursors:
- You can add cursors above/below the current cursor with `` and ``.
- You can skip a line with `` or ``.
- You can match the word/selection under the cursor forwards or backwards with
`n` and `N`.
- You can skip a match forwards or backwards using `s` and
`S`.
- You can add and remove cursors using the mouse with ``.

#### Changing Cursors:
- You can rotate through cursors with `` and ``.
- You can delete the current cursor using `x`
- You can disable cursors with ``, which means only the main cursor
moves.
- You can also press `` to duplicate cursors, disabling the
originals.
- When cursors are disabled, you can press `` to add a cursor under the
main cursor.
- You can press `` to enable cursors again.

#### Using the Cursors:
- Once you have your cursors, you use vim normally as you would with a single
cursor.
- You can press `a` to align cursor columns.
- You can press `S` to split a visual selection by regex into multiple
selections.
- You can press `M` to run a regex within your visual selection, creating
a new cursor for each match.
- You can press `t` and `T` to transpose visual selections,
which means the text within each visual selection will be rotated between
cursors.

#### Finished:
- When you want to collapse your cursors back into one, press ``.

## Cursor API
All of the provided features are implemented using the Cursor API, which is
accessible for writing your own complex multi-cursor logic. You can view
the docs at `:h multicursor-api`.