When I have
hlsearch on and my cursor is somewhere in a highlighted search match, is it possible to detect the search highlight
synIDattr() or a similar function?
As far as I can tell by running the following command, it doesn't seem to be:
:echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
But perhaps that makes sense as a search highlight isn't a language syntax item.
In fact I can't find any way to get a list of the current search highlight positions, which surprises me. Is this possible?
I found a number of similar questions to this but nothing that answered my question.
Motivation: my cursor is hard to see when it's on a highlighted match so I would like to change the cursor's highlight when it's on a match (and restore it when it's not). To do this I need to detect when the cursor's on a search match.
An alternative approach might be to see if the last pattern (
@/) is found at/around the cursor's position.
Update: here's a small function to do that:
function! CursorInSearchMatch(...) if !&hlsearch || !v:hlsearch | return 0 | endif let [match,start,stop] = matchstrpos(getline('.'), @/, (a:0 ? a:1 : 0)) if empty(match) | return 0 | endif let col = getcurpos() if col <= start | return 0 | endif if col <= stop | return 1 | endif return CursorInSearchMatch(stop) endfunction
I call it from