在Vim中的光标下突出显示variables,就像在NetBeans中一样

我在NetBeans工作,喜欢这个function:当你把光标放在一个variables名中时,variables的所有出现都被突出显示。 这对于快速searchvariables的所有出现非常有用。 是否有可能将此行为添加到Vim?

这个自动命令会做你想做的事情:

:autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\')) 

vi突出显示当前单词

编辑:我在我的例子中使用了IncSearch高亮组,但是你可以通过运行这个命令find其他颜色来使用:

 :so $VIMRUNTIME/syntax/hitest.vim 

如果你设置

 :set hlsearch 

突出显示search模式的所有事件,然后使用*#查找您的光标下的单词的出现位置,这将使您得到您想要的某种方式。 不过,我认为语法感知的variables突出显示超出了VIM的范围。

我认为你真正想要的是下面的插件Shuhei Kubota:

http://www.vim.org/scripts/script.php?script_id=4306

根据描述:“这个脚本像许多IDE一样在光标下突出显示单词”。

干杯。

这个语句将允许一个variables来启用/禁用突出显示光标下所有字的出现:

 :autocmd CursorMoved * exe exists("HlUnderCursor")?HlUnderCursor?printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\')):'match none':"" 

一个会激活突出显示:

 :let HlUnderCursor=1 

并禁用它:

 :let HlUnderCursor=0 

人们可以很容易地定义一个启用/禁用突出显示的快捷键:

 :nnoremap <silent> <F3> :exe "let HlUnderCursor=exists(\"HlUnderCursor\")?HlUnderCursor*-1+1:1"<CR> 

删除variables将阻止匹配语句执行,并且不清除当前突出显示:

 :unlet HlUnderCursor 

如果您不想在光标位于这些词语上时突出显示语言单词(语句/ preprocs,如if ,#define),则可以使用基于@too_much_php的答案将此函数放在.vimrc

 let g:no_highlight_group_for_current_word=["Statement", "Comment", "Type", "PreProc"] function s:HighlightWordUnderCursor() let l:syntaxgroup = synIDattr(synIDtrans(synID(line("."), stridx(getline("."), expand('<cword>')) + 1, 1)), "name") if (index(g:no_highlight_group_for_current_word, l:syntaxgroup) == -1) exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\')) else exe 'match IncSearch /\V\<\>/' endif endfunction autocmd CursorMoved * call s:HighlightWordUnderCursor()