【发布时间】:2010-12-05 19:01:26
【问题描述】:
我曾在 NetBeans 中工作并喜欢此功能:当您将光标放在变量名中时,该变量的所有出现都会突出显示。这对于快速搜索所有出现的变量非常有用。是否可以将此行为添加到 Vim?
【问题讨论】:
我曾在 NetBeans 中工作并喜欢此功能:当您将光标放在变量名中时,该变量的所有出现都会突出显示。这对于快速搜索所有出现的变量非常有用。是否可以将此行为添加到 Vim?
【问题讨论】:
如果你设置了
:set hlsearch
突出显示所有出现的搜索模式,然后使用* 或# 来查找光标下的单词的出现,这将为您提供一些您想要的方式。但是我认为语法感知变量突出显示超出了 VIM 的范围。
【讨论】:
nmap <leader>* `` (双反引号)
【讨论】:
E488: Trailing characters: match IncSearch /\<*/\>,我什至必须在继续之前按 Enter,非常烦人!我想它应该以某种方式逃脱和/或至少可以找到一种方法使警告静音。无论如何,很棒的提示。
:autocmd CursorMoved * silent! exe printf('match IncSearch /\<%s\>/', expand('<cword>'))
我认为您真正想要的是 Shuhei Kubota 的以下插件:
http://www.vim.org/scripts/script.php?script_id=4306
根据描述:'这个脚本像许多IDE一样突出光标下的单词。'
干杯。
【讨论】:
此语句将允许变量启用/禁用突出显示光标下单词的所有出现:
: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
【讨论】:
如果您不想在光标位于这些单词上时突出显示语言单词(语句/ 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()
【讨论】:
此变体针对速度(使用 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
【讨论】:
vim_current_word 开箱即用,具有语法意识,并允许自定义颜色。
【讨论】:
映射 F2 以切换突出显示:
map <F2> :set hlsearch!<CR> * #
这当然不是完美的。 '* #' 跳的有点多……
【讨论】:
类似于接受的答案,但这种方式允许您在将光标悬停在单词上之后设置延迟时间,然后突出显示才会出现。 1000 以毫秒为单位,表示它将在 1 秒后突出显示。
set updatetime=1000
autocmd CursorHold * exe
\ printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\'))
请参阅:h CursorHold 了解更多信息。
【讨论】:
vim-illuminate 对我有用。
match-up 对我有用。
vim 匹配:更好的 % 导航和突出显示匹配的单词现代 matchit 和 matchparen
特点
- 在匹配词之间跳转
- 跳转到打开和关闭单词
- 跳进去 (z%)
- 全套文本对象
- 突出显示 ()、[] 和 {}
- 突出显示所有匹配的单词
- 屏幕外显示匹配
- 显示您的位置(面包屑)
- (neovim)tree-sitter 集成
【讨论】: