简体   繁体   English

突出显示在vim中的下一个

[英]Highlighting find next in vim

I often use vim's / search capabilities and will use n to jump to the next match. 我经常使用vim /搜索功能,并使用n跳转到下一个匹配。 However, when the cursor jumps to the next match and the screen redraws, it is often not at all obvious where the cursor is on the screen (and thus where the next match is). 但是,当光标跳转到下一个匹配并且屏幕重绘时,通常根本不明显光标在屏幕上的位置(因此下一个匹配的位置)。 In the past, I have had to do a jk dance to make the cursor move so I can find it. 在过去,我不得不做一个jk舞,让光标移动,所以我可以找到它。 My cursor does not blink by default (I find that annoying), but what I would like is this: when the cursor jumps to the next match, it should change color or blink briefly to draw attention to its placement on the screen, then switch back to the default cursor behavior. 我的光标默认不闪烁(我觉得很烦人),但我想要的是:当光标跳到下一个匹配时,它应该改变颜色或短暂闪烁以引起注意它在屏幕上的位置,然后切换回到默认的游标行为。 How can I create this behavior in my .vimrc file? 如何在我的.vimrc文件中创建此行为? My google-fu has failed me thus far. 到目前为止,我的google-fu失败了。

Note: I am aware there is a setting for highlighting all search matches ( hlsearch ), but I prefer to keep my view clean. 注意:我知道有一个突出显示所有搜索匹配( hlsearch )的设置,但我更喜欢保持我的视图干净。 Temporarily highlighting the current match would be fine, but I want to only draw attention to the current match, not all matches. 暂时突出显示当前的比赛会很好,但我想只关注当前比赛,而不是所有比赛。

I use this in my .vimrc, which will center the search term in the middle of your display. 我在我的.vimrc中使用它,它将搜索项置于显示中间。 This not only makes it easy to find the search term, but also automatically provides me with enough context before and after that I usually don't need to scroll around after searching. 这不仅可以轻松找到搜索词,还可以自动为我提供足够的上下文,我通常不需要在搜索后滚动。

" Center the display line after searches. (This makes it *much* easier to see
" the matched line.)
"
" More info: http://www.vim.org/tips/tip.php?tip_id=528
"
nnoremap n nzz
nnoremap N Nzz
nnoremap * *zz
nnoremap # #zz
nnoremap g* g*zz
nnoremap g# g#zz

Steve Losh made something similar - when you jump to a next location (n key) the cursor blinks! Steve Losh做了类似的事情 - 当你跳到下一个位置(n键)时,光标闪烁! Here's how it works screencast , and here is the code (see older commits for some variations on the theme). 以下是它的工作方式截屏这里是代码 (请参阅主题的一些变体的旧提交)。

I whipped up a couple of functions that seem to handle this alright. 我掀起了几个似乎处理这个问题的功能。 They may not be the best implementation but they seem to work. 它们可能不是最好的实现,但它们似乎有效。

function! s:NextMatch()
    let param = getreg('/')
    let pos = getpos('.')
    let next_match = matchadd('Search', '\%'.pos[1].'l\%'.pos[2].'v'.param)
    redraw
    sleep 250ms
    silent! call matchdelete(next_match)
endfunction

function! s:DoNextMatch()
    let cmd_type = getcmdtype()
    if cmd_type == '/' || cmd_type == '?'
        return "\<cr>:call " . s:SID() . "NextMatch()\<cr>"
    endif
    return "\<cr>"
endfunction

function s:SID()
  return matchstr(expand('<sfile>'), '<SNR>\d\+_\zeSID$')
endfun

nnoremap <silent> n n:call <sid>NextMatch()<cr>
nnoremap <silent> N N:call <sid>NextMatch()<cr>
cnoremap <silent> <expr> <cr> <sid>DoNextMatch()

Tested with: vim -u test.vim -N file.txt 测试: vim -u test.vim -N file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM