如何在双击时突出显示 vim 中出现的所有单词
当我在 Windows 上时,我使用 notepad++,在 Linux 上我使用 vim。我真的很喜欢vim。但在 notepad++ 中至少有一件事我觉得非常有趣。您可以双击某个单词,它会自动突出显示该单词的所有出现位置。我想知道我是否可以用 vim 做类似的事情?所以问题是当你在 vim 中双击某个单词时如何突出显示该单词的所有出现位置。
显然,我不想搜索该单词,或更改光标位置,只是突出显示。我的 :set hlsearch
也已打开。
也许你可能想避免在 vim 中使用鼠标,但我在这里例外:)。
我知道 *
可以完成相同的工作,但是鼠标呢?
When I'm on Windows, I use notepad++, and on Linux I use vim. I really like vim. But there is at least one thing I find really interesting in notepad++. You can double-click on a word and it highlights all occurrences of that word automatically. I was wondering if I could do something like that with vim? So the question is how to highlight all occurrences of a word when you double click on the word in vim.
Obviously, I don't want to search for that word, or change my cursor position, just highlighting. My :set hlsearch
is also on.
probably you may want to avoid the mouse in vim, but I make an exception here :).
I know that *
does the same job, but what about mouse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想突出显示光标下的单词,例如
*
,但不想想要移动光标,那么我建议如下:基本上,此命令设置搜索寄存器(< code>@/) 到当前单词并打开
'hlsearch'
以便突出显示结果。通过设置@/
,光标不会像使用*
或#
那样移动。说明:
- 执行后不显示命令<2-LeftMouse>
- 双击鼠标左键@/ 是用于搜索的寄存器
/
和?
expand('')
获取光标下的当前单词escape(pattern, '\')
在元字符的情况下转义正则表达式\V
使用非常非魔术模式,因此所有元字符都必须使用转义/
\<
和\>
确保当前单词位于单词边界set hls
set' hlsearch'
上,因此突出显示如果设置
@/
寄存器不是您喜欢的类型,您可以使用:match
代替,如下所示:要清除匹配项,只需使用:
If you want to highlight the word under the cursor like
*
, but do not want to move the cursor then I suggest the following:Basically this command sets the search register (
@/
) to the current word and turns on'hlsearch'
so the results are highlighted. By setting@/
the cursor is not moved as it is with*
or#
.Explanation:
<silent>
- to not show the command once executed<2-LeftMouse>
- Double click w/ the left mouse button@/
is the register used for searching with/
and?
expand('<cword>')
get the current word under the cursorescape(pattern, '\')
escape the regex in case of meta characters\V
use very-non-magic mode so everything meta character must be escaped with/
\<
and\>
to ensure the current word is at a word boundaryset hls
set'hlsearch'
on so highlighting appearsIf setting the
@/
register is not your cup of tea than you can use:match
instead like so:To clear the matches just use:
您可以通过简单的映射将 * 映射到双击:
如果您想在插入模式下使用所述功能,您可以使用此映射:
如果没有 (Ctrl-o),将打印 *
[编辑]
正如 ZyX 指出的,如果您想确保
*
或分别使用
将被映射到其他东西,这个递归映射不会被扩展。noremap
总是一个好主意inoremap
You can map * to double-click by a simple mapping:
If you want to use said functionality in insert-mode you can use this mapping:
Without (Ctrl-o) the * would be printed
[EDIT]
As ZyX pointed out, it is always a good idea to use
noremap
respectivelyinoremap
if you want to make sure that if*
or<c-o>
will be mapped to something else, that this recursive mapping won't be expanded.