如何触发搜索结束或nohlsearch被调用

问题描述 投票:1回答:1

我的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”被称为?

提前致谢。

vim vim-plugin vim-syntax-highlighting neovim
1个回答
0
投票
  • 如果你从:match切换到:call matchadd(...),你可以指定一个{priority}。否定的将突出显示在搜索突出显示下方(具有prio 0;默认为10)。
  • “搜索结束”没有任何事件。您可以覆盖所有与搜索相关的命令(/?*等);这会检测到搜索的开始。对于/?,你可以定义一个cmap <CR>,检查getcmdtype() =~# '[/?]'以检测搜索的结论。我不推荐这种方法;映射可能会干扰其他插件。
  • 您可以自动调整当前搜索模式(注册:match),而不是使用/(窗口本地,因此突出显示的行为也与内置搜索突出显示不同)。我的SearchHighlighting plugin用它的:SearchAutoHighlighting命令选择这种方法。 (插件页面包含许多您可能想要查看的替代插件的链接;我希望在我的~/.vimrc中使用维护良好的插件而不是自定义代码片段。)
© www.soinside.com 2019 - 2024. All rights reserved.