[vim/vim] auto nohlsearch (Issue #15039)

47 views
Skip to first unread message

Maxim Kim

unread,
Jun 17, 2024, 11:51:38 PM (9 days ago) Jun 17
to vim/vim, Subscribed

'hlsearch' is very useful but at the same time too intrusive.

Help topic states specifically that :nohlsearch doesn't work in autocommand (actually it also doesn't work in timer callbacks) so there is no obvious way to make it auto turned off.

There is vim-cool plugin by @romainl and the essense of it might be the following snippet I am currently using:

# turn off hlsearch after:
# - doing nothing for 'updatetime'
# - getting into insert mode
augroup auto_nohlsearch | au!
    set updatetime=2000
    noremap <Plug>(nohlsearch) <cmd>nohlsearch<cr>
    noremap! <expr> <Plug>(nohlsearch) execute('nohlsearch')[-1]
    au CursorHold * call feedkeys("\<Plug>(nohlsearch)", 'm')
    au InsertEnter * call feedkeys("\<Plug>(nohlsearch)", 'm')
augroup END

Should we add this example, even though it is "hackish", to the help topic right after :h :nohlsearch or maybe even a simple plugin in the same way comment was added?

asciicast


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039@github.com>

Romain Lafourcade

unread,
Jun 18, 2024, 2:53:40 AM (8 days ago) Jun 18
to vim/vim, Subscribed

The doc is riddled with "hackish" examples like this so why not one more? I find the idea of adding a small plugin better, though. MacVim releases are lagging a bit so I didn't have a chance to play with your commenting plugin yet but it definitely looks like the way to go.

FWIW, vim-cool was a pretty successful attempt to solve one of my pet peeves with hlsearch: that it lingers on after it stopped being relevant. Whether it is through a built-in plugin, a snippet in the doc, a third-party plugin, or in the core, the problem can be considered solved at this point and, as such, doesn't deserve much attention.

But that still leaves the other one: that the highlighting affects all windows, for which there is no workaround AFAIK. If some effort can be made to improve hlsearch, I think that's where it would be most useful.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2175274590@github.com>

Christian Brabandt

unread,
Jun 18, 2024, 5:35:09 AM (8 days ago) Jun 18
to vim/vim, Subscribed

There is an entry in todo.txt to make 'hlsearch' global/local option. I remember I spent some time several years ago looking into it (but at least back then it wasn't that simple, but I forgot details).

Regarding @habamax request, does it make sense to add this (and possibly also the highlight-yank implementation from the help page) into the vim-scripts library. Not sure if we would be littering the library and if this is a particular good idea or rather a bad one 🤷


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2175649997@github.com>

Maxim Kim

unread,
Jun 18, 2024, 5:45:15 AM (8 days ago) Jun 18
to vim/vim, Subscribed

I am not sure about library (don't know the purpose of it) but having packadd autonohlsearch should be easy enough to use.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2175671318@github.com>

Romain Lafourcade

unread,
Jun 18, 2024, 5:55:08 AM (8 days ago) Jun 18
to vim/vim, Subscribed

(mostly for Neovim compatability)

Sigh…


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2175690468@github.com>

Christian Brabandt

unread,
Jun 18, 2024, 7:46:52 AM (8 days ago) Jun 18
to vim/vim, Subscribed

alright then can you make this a real PR then please? Please don't forget a bit of documentation and also please leave adjusting 'updatetime' option value alone.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2175904380@github.com>

Christian Brabandt

unread,
Jun 18, 2024, 1:36:18 PM (8 days ago) Jun 18
to vim/vim, Subscribed

Closed #15039 as completed via 26de90c.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/15039/issue_event/13204486382@github.com>

girishji

unread,
Jun 19, 2024, 8:18:55 AM (7 days ago) Jun 19
to vim/vim, Subscribed

I have something similar in my config, but I prefer 10 sec delay, and also have <esc> do nohls in addition. Maybe you can make the timeout configurable by using autocmd CursorHold * timer_start(timeout - &ut, (_) => feedkeys("\<plug>(nohlsearch)", 'm')).
I also prefer keeping highlights in insert mode as I replace some highlighted fragments (for instance).


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2178558033@github.com>

D. Ben Knoble

unread,
Jun 19, 2024, 5:41:41 PM (7 days ago) Jun 19
to vim/vim, Subscribed

One other note: if hlsearch is made global/local, I’d prefer the default to stay as is (it’s remarkably useful when looking at commit diffs + an open buffer to quickly highlight long function names in all windows, for example).


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2179489053@github.com>

Maxim Kim

unread,
Jun 20, 2024, 3:43:24 AM (6 days ago) Jun 20
to vim/vim, Subscribed

Maybe you can make the timeout configurable by using autocmd CursorHold * timer_start(timeout - &ut, (_) => feedkeys("\<plug>(nohlsearch)", 'm')).

Wouldn't it trigger if after updatetime but before additional timeout there would be cursor movement? And what if at that time I will search for another thing? Say if there is 6 seconds of additional timeout, I theoretically can do another search which would be "unhighlighted" almost right away. Not sure though.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/15039/2180026564@github.com>

Reply all
Reply to author
Forward
0 new messages