开发者

Highlight current search result in vim

开发者 https://www.devze.com 2023-03-31 09:18 出处:网络
In emacs, when you do a search, there will be one highlight color for all occurences in the buffer, and another color for the occurence that your cursor happens to be on. I was wondering if anyone kne

In emacs, when you do a search, there will be one highlight color for all occurences in the buffer, and another color for the occurence that your cursor happens to be on. I was wondering if anyone knew about similar behavior in vim, a vim plugin, or even some ideas on how to accomplish it myself in vimscript.

(note, I already know abo开发者_高级运维ut hl-IncSearch, which is close, but not what I am looking for)


As far as I know there isn't a built-in way to do what you want.

If I were to try to implement it myself... Well one way you could do it is by overriding *, n and p and combining it with something like this function:

noremap n n:call HighlightNearCursor()<CR>
noremap p p:call HighlightNearCursor()<CR>
noremap * *:call HighlightNearCursor()<CR>

function HighlightNearCursor()
  if !exists("s:highlightcursor")
    match Todo /\k*\%#\k*/
    let s:highlightcursor=1
  else
    match None
    unlet s:highlightcursor
  endif
endfunction

I haven't tested it out, so this isn't a complete solution, but I think it is at least a viable approach.

EDIT : You will probably have to set some custom highlight colours. This vimwiki page gives some information about that, although I remember seeing a terser example somewhere.

EDIT AGAIN: Maybe a cleaner solution is to use Mark.vim in conjunction with the first technique. Then it would all boil down to something like:

noremap n \nn\m
noremap p \np\m
noremap * \n*\m


It sounds like you want to highlight all results in the buffer. You can say

:set hls

Which will turn on hlsearch. Then you can say

:set nohls  # turn off permanently
:noh        # turn off until next time you search.

You can also search for / highlight the word under the cursor with * (forwards) or # (backwards).


I just wrote a vim plugin that does what you requested.

https://github.com/timakro/vim-searchant


I don't have a real answer, but a simple way to get maybe 75% of what you want may be to just change the highlighting of the cursor. The default gray cursor block doesn't contrast well with the default yellow of search highlights. So just change cursor highlighting to (a) something that contrasts more with yellow and (2) also contrasts with other colors in your colorscheme. For me something like this works pretty well:

highlight Cursor guifg=green guibg=red

For me the blinking red cursor on first letter of current search match stands out pretty well. Not as good as a full-blown solution, but dead-simple. (I assume it works just as well in terminal Vim if you add those items to the highlight command but haven't tested it there.)

0

精彩评论

暂无评论...
验证码 换一张
取 消