NeoVim highlight current matching words
.lua
-- Highlight current matching words
vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI", "ModeChanged"}, {
callback = function()
min_length_cursor_len = 3
vim.api.nvim_set_hl(0, "CursorWord", { bg = color_table.bg_lighter2 })
local column = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line()
local cursorword = vim.fn.matchstr(line:sub(1, column + 1), [[\k*$]])
.. vim.fn.matchstr(line:sub(column + 1), [[^\k*]]):sub(2)
local mode = vim.fn.mode()
if mode ~= "n" and mode ~= "N" then
vim.api.nvim_set_hl(0, "CursorWord", {})
return
end
if cursorword == vim.w.cursorword then
return
end
vim.w.cursorword = cursorword
if vim.w.cursorword_id then
vim.call("matchdelete", vim.w.cursorword_id)
vim.w.cursorword_id = nil
end
if
cursorword == ""
or #cursorword > 100
or #cursorword < min_length_cursor_len
or string.find(cursorword, "[\192-\255]+") ~= nil
then
return
end
local pattern = [[\<]] .. cursorword .. [[\>]]
vim.w.cursorword_id = vim.fn.matchadd("CursorWord", pattern, -1)
end
})