我曾在 NetBeans 工作并喜欢此功能:当您将光标放在变量名中时,该变量的所有出现都会突出显示。这对于快速搜索所有出现的变量非常有用。是否可以将此行为添加到 Vim?
10 回答
如果你设置
:set hlsearch
突出显示所有出现的搜索模式,然后使用*
或#
查找光标下单词的出现,这将使您以某种方式获得所需的内容。但是我认为语法感知变量突出显示超出了 VIM 的范围。
此语句将允许变量启用/禁用突出显示光标下单词的所有出现:
: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>
删除变量会阻止 match 语句执行,并且不会清除当前突出显示:
:unlet HlUnderCursor
如果您不想在光标位于这些单词上时突出显示语言单词(语句/ preprocs,例如if
, ),您可以根据@too_much_php 答案#define
将此函数放入您的:.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()
我认为您真正想要的是 Shuhei Kubota 的以下插件:
http://www.vim.org/scripts/script.php?script_id=4306
根据描述:“这个脚本像许多 IDE 一样突出显示光标下的单词。”
干杯。
vim_current_word
开箱即用,具有语法意识,并允许自定义颜色。
映射 F2 以切换突出显示:
map <F2> :set hlsearch!<CR> * #
这当然不是完美的。'* #' 跳得有点多……
此变体针对速度(使用 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
类似于接受的答案,但这种方式允许您在将光标悬停在单词上之后设置延迟时间,然后突出显示出现。以1000
毫秒为单位,表示它将在 1 秒后突出显示。
set updatetime=1000
autocmd CursorHold * exe
\ printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\'))
有关:h CursorHold
更多信息,请参阅。
vim-illuminate对我有用。
对决对我有用。
vim 匹配:甚至更好 % 导航和突出显示匹配的单词 现代 matchit 和 matchparen
特征
- 在匹配词之间跳转
- 跳转到打开和关闭单词
- 跳进 (z%)
- 全套文本对象
- 强调 (), [], & {}
- 突出显示所有匹配的单词
- 显示匹配离屏
- 显示你在哪里(面包屑)
- (neovim) 保姆集成