如何在不禁用 Vim 的搜索突出显示可见性的情况下切换它
我想要的是映射一个键,例如 F4,以便按 F4 将切换搜索突出显示的可见性,和,以便无论当前可见性如何,开始新搜索都会启用可见性。
我尝试过的方法:
- 将 F4 映射到
:nohlsearch
会暂时禁用突出显示可见性,而不关闭hlsearch
设置,但不会将可见性切换回来再次。 - 将 F4 映射到
:set hlsearch!
确实会打开/关闭,但我不想关闭hlsearch
设置,只是关闭可见性环境。如果hlsearch
关闭,则它不会自动返回并进行新搜索。
似乎没有相反的 :nohlsearch
形式,并且该命令本身在从函数调用时存在问题。
我发现了类似的问题,但他们没有提供答案。
更新:
第一条评论准确地提供了我所要求的内容,复制如下:(
let hlstate=0
nnoremap <F4> :if (hlstate == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=1-hlstate<cr>
请注意,对于任何使用此功能的人来说——将地图塞到一行而不是使用函数是必要的,因为您无法从函数内部对突出显示进行更改.)
功能略有不同的相关问题:https://stackoverflow.com/a/16750393/1176650
What I'd like is to map one key, e.g. F4, so that pressing F4 will toggle the visibility of search highlights, and so that starting a new search enables visibility no matter the current visibility.
What I've tried:
- Mapping F4 to
:nohlsearch
temporarily disables highlight visibility without turning thehlsearch
setting off, but it does not toggle visibility back again. - Mapping F4 to
:set hlsearch!
does toggle on/off, but I don't want to toggle thehlsearch
setting off, just the visibility setting. Ifhlsearch
is off then it doesn't come back automatically with a new search.
There doesn't seem to be an opposite form of :nohlsearch
and the command itself has problems being called from a function.
I've found similiar questions, but they don't provide an answer.
Update:
The first comment provides exactly what I was asking for, reproduced below:
let hlstate=0
nnoremap <F4> :if (hlstate == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=1-hlstate<cr>
(N.B. for anyone using this --- cramming the map onto one line instead of using a function is necessary since you can't effect a change on highlighting from inside a function.)
Related question for slightly different functionality: https://stackoverflow.com/a/16750393/1176650
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
请注意,最近的 Vims (7.4.79) 具有可用的
v:hlsearch
变量。这意味着您可以将映射改进为:Note, recent Vims (7.4.79) have the
v:hlsearch
variable available. This means you can improve your mapping to:现在只需按 ctrl+c 即可切换突出显示。唯一的怪癖是,您必须在搜索后按两次才能关闭突出显示,因为搜索不会增加计数器。
Now just press ctrl+c to toggle highlight. Only quirk is you gotta press it twice after a search to toggle off highlight because searching doesn't increment the counter.
刚刚尝试过这个,似乎可以解决问题:
Just tried this and seems to do the trick:
您可以使用
:set invhlsearch
进行切换You can toggle with
:set invhlsearch
从突出显示所有搜索模式匹配中,我们可以映射一个键来切换突出显示状态。与 trusktr 的答案不同,这里我们使用变量来存储状态。
"如果您希望能够快速启用/禁用突出显示,您可以映射一个键来切换 hlsearch 选项":
"或者,按回车键暂时退出突出显示的搜索":
From Highlight all search pattern matches, we can map a key to toggle the highlighting states. Unlike trusktr's answer, here we do use a variable to store the state.
"If you want to be able to enable/disable highlighting quickly, you can map a key to toggle the hlsearch option":
"Or, press return to temporarily get out of the highlighted search":
其他答案需要切换突出显示搜索,这不太好。
其实你只需要
我写一个小函数来实现这个功能,并突出显示当前单词(就像*星键一样),但不跳转到下一个匹配的单词,也不触摸跳转列表。
很简单,但我在谷歌上找不到它。
祝大家玩得愉快! :)
Other answer need to toggle highlight search, this is not so good.
Actually you only need to
I write a little function to implement this feature, and highlight current word (just like * star key), but not jump to next matched word, and not touch jumplist.
it's simple, but i'm not find it in google.
Have fun with vim! :)
好的,试试这个:
然后,如果您按 F12,它会关闭突出显示,然后将最后一个搜索字符串设置为空(即:清除它),然后重新打开突出显示。
或者,如果您想保存搜索字符串,则可以执行以下操作:
现在按 SHIFTF12 后,原始搜索字符串将被设置回来并突出显示。
如果这仍然不能满足您,您仍然可以这样做:
其中
OVx
是您在发出:highlight Search
时可以记下的原始值。这样就可以关闭然后重新打开,但有两个键盘快捷键。如果您想要一个,您应该为切换它创建一个函数,然后为该函数创建一个映射。Okay, try this:
Then if you hit F12, it turns of the higlighting, then sets the last search string to an empty one (that is: clears it), and turns back on the highlighting.
Or if you want to save the search string, then you can do something like:
Now after pressing SHIFTF12 the original searchstring will be set back and highlighted.
If that still not satisfy you, you can still do it like:
Where
OVx
are the original values which you can write down when you issue a:highlight Search<CR>
. This way it can be turned off then set back on, but with two keyboard shortcuts. If you want it with one, you should create a function for that which toggles it, then create a mapping for that function.嗯,
:set hlsearch
不是与:nohlsearch
相反吗?第一个打开匹配突出显示,第二个暂时关闭匹配突出显示。
我不明白你的意思“我不想切换‘hlsearch’设置本身,只是切换可见性设置。”:
:set hlsearch
及其挂件仅处理可见性( “突出显示”)搜索匹配项,仅此而已。如果您希望每次搜索时都突出显示匹配项,只需将
:set hlsearch
添加到~/.vimrc
中即可。如果您希望能够在搜索后关闭突出显示,只需将
F4
映射到:nohlsearch
,突出显示将在您下次搜索时恢复。Hmm, isn't
:set hlsearch
the opposite of:nohlsearch
?The first one turns matches highlighting on, the second one turns matches highlighting off temporarilly.
I don't understand what you mean by "I don't want to toggle the 'hlsearch' setting itself, just the visibility setting.":
:set hlsearch
and its pendant only deal with the visibility ("highlighting") of search matches, nothing else.If you want to have matches highlighted each time you do a search just add
:set hlsearch
to your~/.vimrc
.If you want the ability to turn highlighting off after a search just map
F4
to:nohlsearch
, highlighting will be back at your next search.我使用 trusktr 的这个稍微改进的版本。请注意,trusktr 示例中使用的 ctrl+c 默认情况下已映射到 Escape,这非常方便。
I use this slightly improved version from trusktr. Note that ctrl+c as used in the example from trusktr is already mapped to Escape by default which is very handy.
为了实现这一点,我在 .vimrc 中添加了这一行:
In order to accomplish this, I added this unsophisticated line in my .vimrc:
我使用以下内容:
自动组确保在输入 vim 时启用突出显示,但在重新获取 vimrc 文件时不会打开突出显示。映射会打开和关闭它。
I use the following:
The autogroup ensure's that highlighting is enabled when entering vim, but not switched on when re-sourcing your vimrc file(s). The mapping toggles it on and off.
如果有人想在 neovim(在 lua 中)中执行此操作,这是我编写的一个简单命令:
然后您可以使用命令
:HighlightToggle
运行它(如果需要,可以在键盘映射中分配)。If anyone is looking to do this in neovim (in lua) this is a simple command that I wrote:
You can then run it with the command
:HighlightToggle
(and assign in a keymap if you want).我在
vimrc
中使用此映射:基本上,如果
hlsearch
关闭,则映射是无操作的(v:hlsearch
始终为 false)。如果打开,则\a
会切换搜索突出显示,而不更改hlsearch
设置。 (不完全是OP所要求的,但我真的不喜欢每次新搜索开始时都打开hlsearch
的想法。)I use this mapping in my
vimrc
:Basically if
hlsearch
is off, the mapping is no-op (v:hlsearch
is always false). If it's on, then\a
toggles search highlight without changing thehlsearch
setting. (Not exactly what OP asked, but I really don't like the idea of switchinghlsearch
on every time new search starts.)