如何让vim突出显示光标下模式的颜色变化?

时间:2017-08-14 21:28:02

标签: vim

有没有办法让光标下的vim中的搜索高亮文本与不在光标下的搜索文本有不同的颜色?

2 个答案:

答案 0 :(得分:2)

我的.vimrc有以下代码:

function! HiInterestingWord(n) " {{{2
    " Save our location.
    normal! mz
    " Yank the current word into the z register.
    normal! "zyiw
    " Calculate an arbitrary match ID.  Hopefully nothing else is using it.
    let mid = 77750 + a:n
    " Clear existing matches, but don't worry if they don't exist.
    "silent! call matchdelete(mid)
    try
        call matchdelete(mid)
    catch 'E803'
        " Construct a literal pattern that has to match at boundaries.
        let pat = '\V\<' . escape(@z, '\') . '\>'
        " Actually match the words.
        call matchadd("InterestingWord" . a:n, pat, 1, mid)
    endtry
    " Move back to our original location.
    normal! `z
endfunction 

"clear all highlighting
function! ClearAllHi()
    for i in range(1,6)
        let mid = 77750 + i
        silent! call matchdelete(mid)
    endfor
endfunction

nnoremap <silent> <leader>0 :call ClearAllHi()<cr>
nnoremap <silent> <leader>1 :call HiInterestingWord(1)<cr>
nnoremap <silent> <leader>2 :call HiInterestingWord(2)<cr>
nnoremap <silent> <leader>3 :call HiInterestingWord(3)<cr>
nnoremap <silent> <leader>4 :call HiInterestingWord(4)<cr>
nnoremap <silent> <leader>5 :call HiInterestingWord(5)<cr>
nnoremap <silent> <leader>6 :call HiInterestingWord(6)<cr>

hi def InterestingWord1 guifg=#000000 ctermfg=16 guibg=#ffa724 ctermbg=214
hi def InterestingWord2 guifg=#000000 ctermfg=16 guibg=#aeee00 ctermbg=154
hi def InterestingWord3 guifg=#000000 ctermfg=16 guibg=#8cffba ctermbg=121
hi def InterestingWord4 guifg=#000000 ctermfg=16 guibg=#b88853 ctermbg=137
hi def InterestingWord5 guifg=#000000 ctermfg=16 guibg=#ff9eb8 ctermbg=211
hi def InterestingWord6 guifg=#000000 ctermfg=16 guibg=#ff2c4b ctermbg=195
"}}}

这样,您就可以将<leader> + 1-6按到不同颜色光标下的高光词;按两次以清除突出显示。 (您可以更改hi def...)命令中的颜色。 <leader>+0清除所有要点。

你可以把代码放在你的vimrc中试试。

它的工作原理如下:

enter image description here

答案 1 :(得分:1)

人们根据Damian Conway的谈话使用了solution

n1234
+0.50
4:01 PM

但就个人而言,我更喜欢一个简单的,但这突出了整行

" Damian Conway's Die Blinkënmatchen: highlight matches                                                                                        
nnoremap <silent> n n:call HLNext(0.1)<cr>
nnoremap <silent> N N:call HLNext(0.1)<cr>

function! HLNext (blinktime)
   let target_pat = '\c\%#'.@/
   let ring = matchadd('ErrorMsg', target_pat, 101)
   redraw
   exec 'sleep ' . float2nr(a:blinktime * 1000) . 'm'
   call matchdelete(ring)
   redraw
endfunction