gpt4 book ai didi

vim - 如何让 vim 为光标下的图案高亮显示颜色变化?

转载 作者:行者123 更新时间:2023-12-01 13:31:46 25 4
gpt4 key购买 nike

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

最佳答案

我的 .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

关于vim - 如何让 vim 为光标下的图案高亮显示颜色变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com