2

Author: @purpleP
Source: Turn off highlight on CursorMoved and InsertEnter without remapping (doesn't break search operator pending mode)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch() let s:pos = match(getline('.'), @/, col('.') - 1) + 1 if s:pos != col('.') call StopHL() endif endfu

fu! StopHL() if !v:hlsearch || mode() isnot 'n' return else sil call feedkeys("&lt;Plug>(StopHL)", 'm') endif endfu

augroup SearchHighlight au! au CursorMoved * call HlSearch() au InsertEnter * call StopHL() augroup end

For my init.lua config, I need this in Lua.

PS: I'm a newbie. Tried this for the first time. Please add some fixes to it.

vim.api.nvim_set_keymap('n', '<expr> <Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true })
vim.api.nvim_set_keymap('i', '<expr> <Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true })

function HlSearch() local pos = vim.fn.match(vim.fn.getline('.'), @/, vim.fn.col('.') - 1) + 1 if pos ~= vim.fn.col('.') then StopHL() end end

function StopHL() if not vim.v.hlsearch or vim.fn.mode() ~= 'n' then return else vim.cmd[[silent! call feedkeys("&lt;Plug>(StopHL)", 'm')]] end end

vim.cmd('augroup SearchHighlight') vim.cmd('autocmd!') vim.cmd('autocmd CursorMoved * lua HlSearch()') vim.cmd('autocmd InsertEnter * lua StopHL()') vim.cmd('augroup end')

Error messages:

Error detected while processing /home/killerbee/.config/nvim/init.lua:
E5112: Error while creating lua chunk: /home/killerbee/.config/nvim/init.lua:7: unexpected symbol near '@'
D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
Mega Bang
  • 199
  • 11
  • It would be nice if you could add the symptoms of failure. It helps to know if we have been able to reproduce your problem. – Vivian De Smedt Oct 10 '23 at 14:40
  • I think you need to find out how to access registers in Lua, because @/ is Vim syntax for getting a register value (there are also functions like getreg()/getreginfo()) – D. Ben Knoble Oct 10 '23 at 16:52

2 Answers2

2

Here is another attempt:

vim.api.nvim_set_keymap('n', '<Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true, expr = true })
vim.api.nvim_set_keymap('i', '<Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true, expr = true })

function HlSearch() local pos = vim.fn.match(vim.fn.getline('.'), vim.fn.getreg('/'), vim.fn.col('.') - 1) + 1 if pos ~= vim.fn.col('.') then StopHL() end end

function StopHL() if not vim.v.hlsearch or vim.fn.mode() ~= 'n' then return else vim.cmd[[silent! call feedkeys("&lt;Plug>(StopHL)", 'm')]] end end

local searchHighlightGrp = vim.api.nvim_create_augroup('SearchHighlight', { clear = true }) vim.api.nvim_create_autocmd({"CursorMoved"}, {group = searchHighlightGrp, pattern = '', callback = HlSearch}) vim.api.nvim_create_autocmd({"InsertEnter"}, {group = searchHighlightGrp, pattern = '', callback = StopHL})

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
1

Here is a first version that is working:

vim.api.nvim_set_keymap('n', '<Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true, expr = true })
vim.api.nvim_set_keymap('i', '<Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true, expr = true })

function HlSearch() print('HlSearch') local pos = vim.fn.match(vim.fn.getline('.'), vim.fn.getreg('/'), vim.fn.col('.') - 1) + 1 if pos ~= vim.fn.col('.') then StopHL() end end

function StopHL() print('StopHL') if not vim.v.hlsearch or vim.fn.mode() ~= 'n' then return else vim.cmd[[silent! call feedkeys("&lt;Plug>(StopHL)", 'm')]] end end

vim.cmd('augroup SearchHighlight') vim.cmd('autocmd!') vim.cmd('autocmd CursorMoved * lua HlSearch()') vim.cmd('autocmd InsertEnter * lua StopHL()') vim.cmd('augroup end')

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37