在NetBeans中高亮显示Vim中的光标下的变量

问题描述 投票:53回答:7

我在NetBeans工作并喜欢这个功能:当你将光标放在一个变量名中时,所有出现的变量都会突出显示。这对于快速搜索变量的所有出现非常有用。是否可以将此行为添加到Vim?

vim netbeans highlight
7个回答
86
投票

此自动命令将执行您想要的操作:

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

vi highlight current word

编辑:我在我的示例中使用了IncSearch突出显示组,但您可以通过运行此命令找到要使用的其他颜色:

:so $VIMRUNTIME/syntax/hitest.vim

21
投票

如果你设置

:set hlsearch

突出显示所有出现的搜索模式,然后使用*#查找光标下单词的出现,这将为您提供所需的方式。但是我认为语法感知变量突出显示超出了VIM的范围。


8
投票

此语句将允许变量启用/禁用突出显示光标下所有出现的单词:

: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>

删除变量会阻止匹配语句执行,而不会清除当前突出显示:

:unlet HlUnderCursor

5
投票

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

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

根据描述:'这个脚本像许多IDE一样突出了光标下的单词。

干杯。


4
投票

如果您不希望在光标位于这些单词时突出显示语言单词(语句/ 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()

0
投票

此变体针对速度进行了优化(使用CursorHold而不是CursorMoved)以及与hlsearch的兼容性。当前搜索词突出显示不会中断。

" autosave delay, cursorhold trigger, default: 4000ms
setl updatetime=300

" highlight the word under cursor (CursorMoved is inperformant)
highlight WordUnderCursor cterm=underline gui=underline
autocmd CursorHold * call HighlightCursorWord()
function! HighlightCursorWord()
    " if hlsearch is active, don't overwrite it!
    let search = getreg('/')
    let cword = expand('<cword>')
    if match(cword, search) == -1
        exe printf('match WordUnderCursor /\V\<%s\>/', escape(cword, '/\'))
    endif
endfunction

0
投票

vim_current_word开箱即用,具有语法感知功能,并允许自定义颜色。

© www.soinside.com 2019 - 2024. All rights reserved.