loupe icon indicating copy to clipboard operation
loupe copied to clipboard

Feature Request: auto nohl on cursor move.

Open ZacharyRizer opened this issue 5 years ago • 3 comments

Was curious if implementing something similar to vim-slash and is.vim in regards to automatically calling :nohl after you move your cursor after n or N. I appreciate the mapping to be able to call :nohl, but I get tired of having to do that all the time.

ZacharyRizer avatar Dec 09 '20 17:12 ZacharyRizer

I just took a look at the code of vim-slash and I came up with

augroup Loupe
   autocmd!
   " autocmd CursorMoved,CursorMovedI * set nohlsearch | autocmd! Loupe
   autocmd CursorMoved,CursorMovedI * :execute "normal \<Plug>(LoupeClearHighlight)"
augroup END

But this doesn't quite work. Or rather: it works a bit too good: since n moves the cursor, it removes the highlighting everywhere while searching.

A comment in is.vim specifically says

Make sure move cursor by search related action after calling this function because the first move event just set nested autocmd which does :nohlsearch

so they do this

Maybe someone else has an idea on how to improve this

miallo avatar Dec 16 '20 23:12 miallo

I'm trying this out and have a similar feeling. I coded something like this

function _G.coh()
  if vim.o.hlsearch then
    vim.fn['loupe#hlmatch']()
  else
    vim.fn['loupe#private#clear_highlight']()
  end
end
vim.cmd('autocmd OptionSet hlsearch call v:lua.coh()')

@miallo this is neovim's lua, it's mostly the same in viml. Feels hacky though, calling loupe#private..., Is there any chance to get this into the plugin instead?

davidsu avatar Jan 11 '21 15:01 davidsu

@davidsu Can't you "type" <Plug>(LoupeClearHighlight)? This will call that function for you without that hack.

At some point I wanted to take a look at lua, but so far my neovim-config is all vimscript...

miallo avatar Jan 12 '21 08:01 miallo