gpt4 book ai didi

vim - 如何自动突出显示vim中的当前单词?

转载 作者:行者123 更新时间:2023-12-04 17:00:15 29 4
gpt4 key购买 nike

例如,我们的文字是:

hello world
abcd hello world
hello world

在eclipse中,当光标位于某个单词上时, hello单词会在当前文件中自动突出显示。在正常模式下键入 ww时,光标位于其他单词上 world将在当前文件中突出显示, hello自动取消突出显示。此功能对用户来说非常方便。

vim是否可以使用某些插件来完成此操作?

最佳答案

vim.wikia.com上有一个脚本可以做到这一点。它会一直等到您停止移动光标,然后突出显示当前单词的所有实例。然后,您可以像通常使用搜索结果一样,使用nN在它们之间跳转。

我将其复制到此处,以防链接断开:

" Highlight all instances of word under cursor, when idle.
" Useful when studying strange source code.
" Type z/ to toggle highlighting on/off.
nnoremap z/ :if AutoHighlightToggle()<Bar>set hls<Bar>endif<CR>
function! AutoHighlightToggle()
let @/ = ''
if exists('#auto_highlight')
au! auto_highlight
augroup! auto_highlight
setl updatetime=4000
echo 'Highlight current word: off'
return 0
else
augroup auto_highlight
au!
au CursorHold * let @/ = '\V\<'.escape(expand('<cword>'), '\').'\>'
augroup end
setl updatetime=500
echo 'Highlight current word: ON'
return 1
endif
endfunction

如该页面的注释中所述,如果您始终希望启用此功能,则可以在定义后从 vimrc调用该功能。这样,您可以使用 z/(或分配给它的任何快捷方式)在以后再次将其关闭。

关于vim - 如何自动突出显示vim中的当前单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25227281/

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