old multicurors until release

This commit is contained in:
2026-11-22 17:25:32 +01:00
parent 8fe8988de0
commit 3680ad5fa8
5 changed files with 158 additions and 13 deletions
+1
View File
@@ -1,4 +1,5 @@
require("plugins.colorscheme")
require("plugins.multicursors")
require("plugins.telescope")
require("plugins.treesitter")
require("plugins.lsp")
+137
View File
@@ -0,0 +1,137 @@
vim.pack.add({ { src = 'https://github.com/jake-stewart/multicursor.nvim', version = 'native' } })
--[[
local ns = vim.api.nvim_create_namespace('nvim.multicursor')
local function word_pat()
local w = vim.fn.expand('<cword>')
if w == '' then return end
return '\\V\\<' .. vim.fn.escape(w, '\\') .. '\\>'
end
-- Moving the main cursor would drag every cursor along in follow mode,
-- so switch it off for the move, then re-enable it.
local function without_follow(fn)
vim.cmd('normal! 2q=')
fn()
vim.schedule(function() vim.cmd('normal! 1q=') end)
end
-- Add a cursor on the current word, then jump to the next/previous match
local function add_match(dir)
return function()
local pat = word_pat()
if not pat then return end
without_follow(function()
vim.fn.searchpos(pat, 'bcW') -- go to start of the word
vim.api.nvim_mcursor(0, vim.api.nvim_win_get_cursor(0))
vim.fn.searchpos(pat, dir > 0 and 'w' or 'bw')
end)
end
end
-- Jump to the next/previous match and remove the cursor sitting there
local function remove_match(dir)
return function()
local pat = word_pat()
if not pat then return end
without_follow(function()
local pos = vim.fn.searchpos(pat, dir > 0 and 'w' or 'bw')
local r, c = pos[1] - 1, pos[2] - 1
for _, m in ipairs(vim.api.nvim_buf_get_extmarks(0, ns, { r, c }, { r, c }, {})) do
vim.api.nvim_buf_del_extmark(0, ns, m[1])
end
end)
end
end
vim.keymap.set({ 'n' }, '<C-j>', add_match(1), { desc = 'MC: add cursor, next match' })
vim.keymap.set({ 'n' }, '<C-k>', add_match(-1), { desc = 'MC: add cursor, prev match' })
vim.keymap.set({ 'n' }, '<C-S-j>', remove_match(1), { desc = 'MC: remove cursor, next match' })
vim.keymap.set({ 'n' }, '<C-S-k>', remove_match(-1), { desc = 'MC: remove cursor, prev match' })
]] --
local mc = require("multicursor-nvim")
mc.setup({native = false})
local set = vim.keymap.set
-- Add or skip cursor above/below the main cursor.
set({ "n", "x" }, "<C-M-k>", function()
mc.lineAddCursor(-1)
end)
set({ "n", "x" }, "<C-M-j>", function()
mc.lineAddCursor(1)
end)
set({ "n", "x" }, "<C-M-S-k>", function()
mc.lineSkipCursor(-1)
end)
set({ "n", "x" }, "<C-M-S-j>", function()
mc.lineSkipCursor(1)
end)
-- Add or skip adding a new cursor by matching word/selection
set({ "n", "x" }, "<C-j>", function()
mc.matchAddCursor(1)
end)
set({ "n", "x" }, "<C-S-j>", function()
mc.matchSkipCursor(1)
end)
set({ "n", "x" }, "<C-k>", function()
mc.matchAddCursor(-1)
end)
set({ "n", "x" }, "<C-S-k>", function()
mc.matchSkipCursor(-1)
end)
-- Add and remove cursors with control + left click.
set("n", "<c-leftmouse>", mc.handleMouse)
set("n", "<c-leftdrag>", mc.handleMouseDrag)
set("n", "<c-leftrelease>", mc.handleMouseRelease)
-- Disable and enable cursors.
set({ "n", "x" }, "<c-q>", mc.toggleCursor)
-- Mappings defined in a keymap layer only apply when there are
-- multiple cursors. This lets you have overlapping mappings.
mc.addKeymapLayer(function(layerSet)
-- Select a different cursor as the main one.
layerSet({ "n", "x" }, "<left>", mc.prevCursor)
layerSet({ "n", "x" }, "<right>", mc.nextCursor)
-- Delete the main cursor.
layerSet({ "n", "x" }, "<leader>x", mc.deleteCursor)
-- Enable and clear cursors using escape.
layerSet("n", "<esc>", function()
if not mc.cursorsEnabled() then
mc.enableCursors()
else
mc.clearCursors()
end
end)
end)
-- Customize how cursors look.
local hl = vim.api.nvim_set_hl
hl(0, "MultiCursorCursor", { reverse = true, fg = "#aaaaff" })
hl(0, "MultiCursorVisual", { link = "Visual" })
hl(0, "MultiCursorSign", { link = "SignColumn" })
hl(0, "MultiCursorMatchPreview", { link = "Search" })
hl(0, "MultiCursorDisabledCursor", { reverse = true })
hl(0, "MultiCursorDisabledVisual", { link = "Visual" })
hl(0, "MultiCursorDisabledSign", { link = "SignColumn" })
-- Autocmds to change main cursor color when multicursors is active
vim.api.nvim_create_autocmd("User", {
pattern = "MultiCursorStart",
callback = function()
vim.api.nvim_set_hl(0, "Cursor", { bg = "#000000", fg = "#ffffff" }) -- bright red
end,
})
vim.api.nvim_create_autocmd("User", {
pattern = "MultiCursorExit",
callback = function()
vim.api.nvim_set_hl(0, "Cursor", { link = "Cursor" }) -- restore default
end,
})