3

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("\<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("\<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 '@'
2
  • 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. Commented Oct 10, 2023 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
    Commented Oct 10, 2023 at 16:52

2 Answers 2

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("\<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})
1
  • 1
    I think this goes more with Lua if I am not wrong.
    – Mega Bang
    Commented Oct 10, 2023 at 19:40
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("\<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')
2
  • So close, yet missed it. Thanks a million.
    – Mega Bang
    Commented Oct 10, 2023 at 19:31
  • 1
    Thanks for the feedback :-) Step by step you'll make it ;-) Commented Oct 10, 2023 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.