如何在 VIM 中突出显示所选单词的所有出现位置?
如何在 GVim 中突出显示所选单词的所有出现位置,就像在 Notepad++ 中一样?
How can I highlight all occurrence of a selected word in GVim, like in Notepad++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
在普通模式下:
然后在普通模式下使用命令
/
搜索模式,或者在插入模式下使用o
后跟/
搜索模式。正常模式下的*
将搜索光标下单词的下一个出现位置。如果设置,hlsearch
选项将突出显示所有内容。#
将搜索该单词之前出现的位置。要删除先前搜索的突出显示:
您可能希望将
:nohlsearch
映射到某个方便的键。In Normal mode:
Then search for a pattern with the command
/
in Normal mode, or<Ctrl>o
followed by/
in Insert mode.*
in Normal mode will search for the next occurrence of the word under the cursor. Thehlsearch
option will highlight all of them if set.#
will search for the previous occurrence of the word.To remove the highlight of the previous search:
You might wish to map
:nohlsearch<CR>
to some convenient key.*
键将突出显示光标下出现的所有单词。The
*
key will highlight all occurrences of the word that is under the cursor.我知道这是一个非常老的问题,但如果有人对此功能感兴趣,可以检查此代码
http://vim.wikia.com/wiki/Auto_highlight_current_word_when_idle
I know than it's a really old question, but if someone is interested in this feature, can check this code
http://vim.wikia.com/wiki/Auto_highlight_current_word_when_idle
最简单的方法,在正常模式下输入
*
我也有这些映射来启用和禁用
通过单击选择单词
奖金:CountWordFunction
用鼠标选择单词并立即计算出现次数:
OBS:请注意,在这个版本中,我们在末尾有“CountWord”命令
the simplest way, type in normal mode
*
I also have these mappings to enable and disable
Select word by clickin on it
Bonus: CountWordFunction
Selecting word with mouse and counting occurrences at once:
OBS: Notice that in this version we have "CountWord" command at the end
基于搜索的解决方案(*、/...)移动光标,这可能会很不幸。
另一种方法是使用 增强的 mark.vim 插件,然后完成您的 < code>.vimrc 让双击触发突出显示(我不知道键盘选择如何触发命令):
它允许多次突出显示、持久化等。
要删除突出显示,可以:
:MarkClear
Search based solutions (*, /...) move cursor, which may be unfortunate.
An alternative is to use enhanced mark.vim plugin, then complete your
.vimrc
to let double-click trigger highlighting (I don't know how a keyboard selection may trigger a command) :It allows multiple highlightings, persistence, etc.
To remove highlighting, either :
:Mark
(switch off until next selection):MarkClear
首先(或在您的 .vimrc 中):
然后将光标放在要突出显示的单词上,然后点击
*
。hlsearch
表示突出显示当前搜索的所有匹配项,*
表示搜索光标下的单词。First (or in your .vimrc):
Then position your cursor over the word you want highlighted, and hit
*
.hlsearch
means highlight all occurrences of the current search, and*
means search for the word under the cursor.要突出显示单词而不移动光标,请
进入 vimrc。
这样做的好处是
g*
和g#
仍将像“正常”*
和#
一样工作。要关闭
hlsearch
,您可以使用“short-form”(除非您有另一个在命令模式下以“noh”开头的函数)::noh
。或者您可以使用长版本::nohlsearch
为了极其方便(我发现自己每天可能会切换
hlsearch
20 次),您可以映射某些内容来切换hlsearch< /code> 像这样:
.:.如果您的
是 \(默认情况下),您可以按 \st(快速)在正常模式下切换hlsearch
。或者也许您只想映射
:noh
:上面只是运行
:nohlsearch
所以(与:set hlsearch!
不同)它仍然会突出显示下次在正常模式下按*或#时会出现单词。干杯
to highlight word without moving cursor, plop
into your vimrc.
What's nice about this is
g*
andg#
will still work like "normal"*
and#
.To set
hlsearch
off, you can use "short-form" (unless you have another function that starts with "noh" in command mode)::noh
. Or you can use long version::nohlsearch
For extreme convenience (I find myself toggling
hlsearch
maybe 20 times per day), you can map something to togglehlsearch
like so:.:. if your
<leader>
is \ (it is by default), you can press \st (quickly) in normal mode to togglehlsearch
.Or maybe you just want to have
:noh
mapped:The above simply runs
:nohlsearch
so (unlike:set hlsearch!
) it will still highlight word next time you press * or # in normal mode.cheers
例如这个插件:
https://github.com/rrethy/vim-Illuminate
https://github.com/itchyny/vim-cursorword
只需在下面搜索光标位于 vimawesome.com
键,如 clagccs 提到,突出显示不会与您的搜索冲突:https://vim.fandom.com/wiki/Auto_highlight_current_word_when_idle
与搜索不冲突的屏幕截图:
注意:
For example this plugIns:
https://github.com/rrethy/vim-illuminate
https://github.com/itchyny/vim-cursorword
Just search for under cursor in vimawesome.com
The key, as clagccs mentioned, is that the highlight does NOT conflict with your search: https://vim.fandom.com/wiki/Auto_highlight_current_word_when_idle
Screen-shot of how it does NOT conflict with search:
Notes:
首先确保通过发出以下命令启用 hlsearch
您还可以将其添加到
现在设置的 .vimrc 文件中,当您在命令模式下使用快速搜索机制或常规搜索命令时,所有结果都会突出显示。要在结果之间向前移动,请按“n” 要向后移动,请按“N”
在正常模式下,要快速搜索光标下的单词并跳转到一个命令中的下一个匹配项,请按“*”,您也可以搜索光标下的单词,然后按 '#' 移动到上一个出现位置
在正常模式下,也可以使用 来调用快速搜索以
删除出现的高亮显示,我已将其绑定到我的 .vimrc 中的快捷方式
First ensure that hlsearch is enabled by issuing the following command
You can also add this to your .vimrc file as set
now when you use the quick search mechanism in command mode or a regular search command, all results will be highlighted. To move forward between results, press 'n' to move backwards press 'N'
In normal mode, to perform a quick search for the word under the cursor and to jump to the next occurrence in one command press '*', you can also search for the word under the cursor and move to the previous occurrence by pressing '#'
In normal mode, quick search can also be invoked with the
to remove highlights on ocuurences use, I have bound this to a shortcut in my .vimrc
或许 ?
maybe ?
启用搜索突出显示:
然后搜索单词:
Enable search highlighting:
Then search for the word:
我最喜欢这样做的是 mark.vim 插件。
它允许同时用不同的颜色突出显示多个单词。
屏幕截图示例
My favorite for doing this is the mark.vim plugin.
It allows to highlight several words in different colors simultaneously.
Example Screenshot
使用 autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/', escape(expand(''), '/\'))
确保您已将
IncSearch
设置为某项。例如调用 s:Attr('IncSearch', 'reverse')
。或者,您可以使用另一个突出显示组来代替它。这将立即突出显示光标下出现的所有单词。我发现当我快速编写代码时,延迟会减慢我的速度。突出显示颜色将与单词的颜色相匹配,因此它与您的方案保持一致。
Use
autocmd CursorMoved * exe printf('match IncSearch /\V\<%s\>/', escape(expand('<cword>'), '/\'))
Make sure you have
IncSearch
set to something. e.gcall s:Attr('IncSearch', 'reverse')
. Alternatively you can use another highlight group in its place.This will highlight all occurrences of words under your cursor without a delay. I find that a delay slows me down when I'm wizzing through code. The highlight color will match the color of the word, so it stays consistent with your scheme.
为什么不只是:
z/
这将突出显示光标下的当前单词以及任何其他出现的单词。而且您不必为您正在搜索的每个项目发出单独的命令。也许这在邪恶的 gvim 中不可用?默认情况下它在 vim 中。
* 仅当您希望光标移动到下一个出现的位置时才有用。当以视觉方式比较两个事物时,您通常不希望光标移动,而且每次都按 * 键很烦人。
Why not just:
z/
That will highlight the current word under cursor and any other occurrences. And you don't have to give a separate command for each item you're searching for. Perhaps that's not available in the unholy gvim? It's in vim by default.
* is only good if you want the cursor to move to the next occurrence. When comparing two things visually you often don't want the cursor to move, and it's annoying to hit the * key every time.
~/.vimrc
文件" 突出显示搜索到的项目
设置 hlsearch
" F8 递归搜索光标下的单词 :copen , 关闭 -> :ccl
nnoremap
:so%
重新加载设置在正常模型中检查该单词。
按 * 按 F8 在整个项目中递归搜索该单词
~/.vimrc
file" highlight the searched items
set hlsearch
" F8 search for word under the cursor recursively , :copen , to close -> :ccl
nnoremap <F8> :grep! "\<<cword>\>" . -r<CR>:copen 33<CR>
:so%
In normal model go over the word.
Press * Press F8 to search recursively bellow your whole project over the word
-->如果你想突出显示 gvim 中出现的文本,
请选择 text &复制
然后?粘贴所选文本(注意:这不适用于插入模式)
--> if you want to highlight a text occurrences in gvim
Select the text & copy
then ?paste the selected text (Note: This will not work for insert mode)