我的vimrc中有以下代码
hi CurrentWordUL cterm=underline gui=underline
hi Search cterm=underline ctermfg=124 gui=underline guifg=#af0000
augroup MyHighlighter
autocmd!
" autocmd User IncSearchEnter MatchHighlighter 0
" autocmd User IncSearchExecute MatchHighlighter 1
set updatetime=700
autocmd CursorHold * if (get(g:, 'matchhl', 1) && (&ft !~ join(g:ft_blacklist, '\|')))
\ | silent! exe printf('match CurrentWordUL /\<%s\>/', expand('<cword>'))
\ | endif
autocmd CursorMoved * if (get(g:, 'matchhl', 1) && (&ft !~ join(g:ft_blacklist, '\|')))
\ | silent! exe printf('match none')
\ | endif
augroup END
nnoremap <silent> <f4> :MatchHighlighter<CR>
command! -nargs=? MatchHighlighter
\ call ToggleSetMatchHL(
\ empty(<q-args>) ? !get(g:, 'matchhl', 1) : expand(<q-args>))
function! ToggleSetMatchHL(arg) abort
match none | diffupdate | syntax sync fromstart
let g:matchhl = a:arg
endfunction
这样做的目的是突出显示(下划线)光标下的匹配单词。
我遇到的问题是当我用/,?,*,#等搜索时,匹配的高亮显示会覆盖搜索突出显示。
无论如何我可以知道“搜索结束”(我不知道“搜索结束”的定义,但也许有人可以提出建议)或“nohlsearch”被称为?
提前致谢。
:match
切换到:call matchadd(...)
,你可以指定一个{priority}
。否定的将突出显示在搜索突出显示下方(具有prio 0
;默认为10
)。/
,?
,*
等);这会检测到搜索的开始。对于/
和?
,你可以定义一个cmap <CR>
,检查getcmdtype() =~# '[/?]'
以检测搜索的结论。我不推荐这种方法;映射可能会干扰其他插件。:match
),而不是使用/
(窗口本地,因此突出显示的行为也与内置搜索突出显示不同)。我的SearchHighlighting plugin用它的:SearchAutoHighlighting
命令选择这种方法。 (插件页面包含许多您可能想要查看的替代插件的链接;我希望在我的~/.vimrc
中使用维护良好的插件而不是自定义代码片段。)