我让我的.vimrc高亮显示所有与当前光标相匹配的单词
autocmd CursorMoved * silent! exe printf('match Search /\<%s\>/', expand('<cword>'))但有时它有点烦人,所以我想映射一个键来打开或关闭它,例如<F10>
我该怎么做呢?
发布于 2013-04-14 23:08:25
在您的.vimrc中输入以下内容:
let g:toggleHighlight = 0
function! ToggleHighlight(...)
if a:0 == 1 "toggle behaviour
let g:toggleHighlight = 1 - g:toggleHighlight
endif
if g:toggleHighlight == 0 "normal action, do the hi
silent! exe printf('match Search /\<%s\>/', expand('<cword>'))
else
"do whatever you need to clear the matches
"or nothing at all, since you are not printing the matches
endif
endfunction
autocmd CursorMoved * call ToggleHighlight()
map <F8> :call ToggleHighlight(1)<CR>这个想法是,如果你调用一个带参数的函数,它会将行为更改为print/no print。autocommand只使用最后一个设置,因为在没有参数的情况下调用那里的函数。
发布于 2013-04-14 22:20:16
清除自动命令并取消高亮显示:
nmap <f8> :autocmd! CursorMoved<cr> :call clearmatches()<cr>并使用不同的密钥将其重新打开:
nmap <f9> :autocmd CursorMoved * silent! exe printf('match Search /\<%s\>/', expand('<cword>'))<cr>https://stackoverflow.com/questions/15999958
复制相似问题