繁体   English   中英

突出显示在vim中的下一个

[英]Highlighting find next in vim

我经常使用vim /搜索功能,并使用n跳转到下一个匹配。 但是,当光标跳转到下一个匹配并且屏幕重绘时,通常根本不明显光标在屏幕上的位置(因此下一个匹配的位置)。 在过去,我不得不做一个jk舞,让光标移动,所以我可以找到它。 我的光标默认不闪烁(我觉得很烦人),但我想要的是:当光标跳到下一个匹配时,它应该改变颜色或短暂闪烁以引起注意它在屏幕上的位置,然后切换回到默认的游标行为。 如何在我的.vimrc文件中创建此行为? 到目前为止,我的google-fu失败了。

注意:我知道有一个突出显示所有搜索匹配( hlsearch )的设置,但我更喜欢保持我的视图干净。 暂时突出显示当前的比赛会很好,但我想只关注当前比赛,而不是所有比赛。

我在我的.vimrc中使用它,它将搜索项置于显示中间。 这不仅可以轻松找到搜索词,还可以自动为我提供足够的上下文,我通常不需要在搜索后滚动。

" 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做了类似的事情 - 当你跳到下一个位置(n键)时,光标闪烁! 以下是它的工作方式截屏这里是代码 (请参阅主题的一些变体的旧提交)。

我掀起了几个似乎处理这个问题的功能。 它们可能不是最好的实现,但它们似乎有效。

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()

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

暂无
暂无

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

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