如何在不禁用 Vim 的搜索突出显示可见性的情况下切换它

发布于 2024-12-29 15:25:05 字数 902 浏览 3 评论 0原文

我想要的是映射一个键,例如 F4,以便按 F4 将切换搜索突出显示的可见性,,以便无论当前可见性如何,开始新搜索都会启用可见性。

我尝试过的方法:

  1. F4 映射到 :nohlsearch 会暂时禁用突出显示可见性,而不关闭 hlsearch 设置,但不会将可见性切换回来再次。
  2. 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:

  1. Mapping F4 to :nohlsearch temporarily disables highlight visibility without turning the hlsearch setting off, but it does not toggle visibility back again.
  2. Mapping F4 to :set hlsearch! does toggle on/off, but I don't want to toggle the hlsearch setting off, just the visibility setting. If hlsearch 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

请注意,最近的 Vims (7.4.79) 具有可用的 v:hlsearch 变量。这意味着您可以将映射改进为:

:nnoremap <silent><expr> <Leader>h (&hls && v:hlsearch ? ':nohls' : ':set hls')."\n"

Note, recent Vims (7.4.79) have the v:hlsearch variable available. This means you can improve your mapping to:

:nnoremap <silent><expr> <Leader>h (&hls && v:hlsearch ? ':nohls' : ':set hls')."\n"
近箐 2025-01-05 15:25:05
" ctrl+c to toggle highlight.
let hlstate=0
nnoremap <c-c> :if (hlstate%2 == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=hlstate+1<cr>

现在只需按 ctrl+c 即可切换突出显示。唯一的怪癖是,您必须在搜索后按两次才能关闭突出显示,因为搜索不会增加计数器。

" ctrl+c to toggle highlight.
let hlstate=0
nnoremap <c-c> :if (hlstate%2 == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=hlstate+1<cr>

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.

夏尔 2025-01-05 15:25:05

我想要的是映射一个键,例如F4,这样按F4就会
切换搜索突出显示的可见性,以便开始新的搜索
无论当前可见性如何,搜索都会启用可见性。

刚刚尝试过这个,似乎可以解决问题:

" switch higlight no matter the previous state
nmap <F4> :set hls! <cr>
" hit '/' highlights then enter search mode
nnoremap / :set hlsearch<cr>/

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.

Just tried this and seems to do the trick:

" switch higlight no matter the previous state
nmap <F4> :set hls! <cr>
" hit '/' highlights then enter search mode
nnoremap / :set hlsearch<cr>/
只是我以为 2025-01-05 15:25:05

您可以使用 :set invhlsearch 进行切换

You can toggle with :set invhlsearch

千寻… 2025-01-05 15:25:05

突出显示所有搜索模式匹配中,我们可以映射一个键来切换突出显示状态。与 trusktr 的答案不同,这里我们使用变量来存储状态。

"如果您希望能够快速启用/禁用突出显示,您可以映射一个键来切换 hlsearch 选项":

" Press F4 to toggle highlighting on/off, and show current value.
:noremap <F4> :set hlsearch! hlsearch?<CR>

"或者,按回车键暂时退出突出显示的搜索":

:nnoremap <CR> :nohlsearch<CR><CR>

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":

" Press F4 to toggle highlighting on/off, and show current value.
:noremap <F4> :set hlsearch! hlsearch?<CR>

"Or, press return to temporarily get out of the highlighted search":

:nnoremap <CR> :nohlsearch<CR><CR>
紫南 2025-01-05 15:25:05

其他答案需要切换突出显示搜索,这不太好。

其实你只需要

let @/ = ''

我写一个小函数来实现这个功能,并突出显示当前单词(就像*星键一样),但不跳转到下一个匹配的单词,也不触摸跳转列表。

function! HLtoggle()
    if (@/ == '')
        let @/ = expand("<cword>")
    else
        let @/ = ''
    endif
endfunc

很简单,但我在谷歌上找不到它。

祝大家玩得愉快! :)

Other answer need to toggle highlight search, this is not so good.

Actually you only need to

let @/ = ''

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.

function! HLtoggle()
    if (@/ == '')
        let @/ = expand("<cword>")
    else
        let @/ = ''
    endif
endfunc

it's simple, but i'm not find it in google.

Have fun with vim! :)

梦里的微风 2025-01-05 15:25:05

好的,试试这个:

:map <F12> :set nohls<CR>:let @/ = ""<CR>:set hls<CR>

然后,如果您按 F12,它会关闭突出显示,然后将最后一个搜索字符串设置为空(即:清除它),然后重新打开突出显示。

或者,如果您想保存搜索字符串,则可以执行以下操作:

:map <F12> :set nohls<CR>:let @s = @/<CR>:let @/ = ""<CR>:set hls<CR>
:map <SHIFT><F12> :let @/=@s<CR>

现在按 SHIFTF12 后,原始搜索字符串将被设置回来并突出显示。

如果这仍然不能满足您,您仍然可以这样做:

:map <F12> :highlight Search term=None ctermfg=None ctermbg=None guifg=None guibg=None<CR>
:map <SHIFT><F12> :highlight Search term=OV1 ctermfg=OV2 ctermbg=OV3 guifg=OV4 guibg=OV5<CR>

其中 OVx 是您在发出 :highlight Search 时可以记下的原始值。这样就可以关闭然后重新打开,但有两个键盘快捷键。如果您想要一个,您应该为切换它创建一个函数,然后为该函数创建一个映射。

Okay, try this:

:map <F12> :set nohls<CR>:let @/ = ""<CR>:set hls<CR>

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:

:map <F12> :set nohls<CR>:let @s = @/<CR>:let @/ = ""<CR>:set hls<CR>
:map <SHIFT><F12> :let @/=@s<CR>

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:

:map <F12> :highlight Search term=None ctermfg=None ctermbg=None guifg=None guibg=None<CR>
:map <SHIFT><F12> :highlight Search term=OV1 ctermfg=OV2 ctermbg=OV3 guifg=OV4 guibg=OV5<CR>

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.

半城柳色半声笛 2025-01-05 15:25:05

嗯,: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.

っ〆星空下的拥抱 2025-01-05 15:25:05

我使用 trusktr 的这个稍微改进的版本。请注意,trusktr 示例中使用的 ctrl+c 默认情况下已映射到 Escape,这非常方便。

" Toggle highlight search
let hlstate=0
nnomap <Leader>b :if (hlstate%2 == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=hlstate+1<CR>:echo "toggled visibility for hlsearch"<CR>
inomap <Leader>b <ESC>:if (hlstate%2 == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=hlstate+1<CR>:echo "toggled visibility for hlsearch"<CR>a

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.

" Toggle highlight search
let hlstate=0
nnomap <Leader>b :if (hlstate%2 == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=hlstate+1<CR>:echo "toggled visibility for hlsearch"<CR>
inomap <Leader>b <ESC>:if (hlstate%2 == 0) \| nohlsearch \| else \| set hlsearch \| endif \| let hlstate=hlstate+1<CR>:echo "toggled visibility for hlsearch"<CR>a
怪我入戏太深 2025-01-05 15:25:05

为了实现这一点,我在 .vimrc 中添加了这一行:

nmap <LEADER>h /xxxxx<CR>

In order to accomplish this, I added this unsophisticated line in my .vimrc:

nmap <LEADER>h /xxxxx<CR>
喵星人汪星人 2025-01-05 15:25:05

我使用以下内容:

augroup onsearch                                                                                                                    
    autocmd!                                                                                                                          
    autocmd VimEnter * set hlsearch                                                                                                   
augroup END

nnoremap <leader>/ :set hlsearch!<CR>

自动组确保在输入 vim 时启用突出显示,但在重新获取 vimrc 文件时不会打开突出显示。映射会打开和关闭它。

I use the following:

augroup onsearch                                                                                                                    
    autocmd!                                                                                                                          
    autocmd VimEnter * set hlsearch                                                                                                   
augroup END

nnoremap <leader>/ :set hlsearch!<CR>

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.

通知家属抬走 2025-01-05 15:25:05

如果有人想在 neovim(在 lua 中)中执行此操作,这是我编写的一个简单命令:

HLSTATE = vim.opt.hlsearch
function TOGGLE_SEARCH_HIGHLIGTING()
  if HLSTATE == true then
    vim.opt.hlsearch = false
    HLSTATE = false
  else
    vim.opt.hlsearch = true
    HLSTATE = true
  end
end
vim.api.nvim_create_user_command('HighlightToggle', 'lua TOGGLE_SEARCH_HIGHLIGTING()', {})

然后您可以使用命令 :HighlightToggle 运行它(如果需要,可以在键盘映射中分配)。

If anyone is looking to do this in neovim (in lua) this is a simple command that I wrote:

HLSTATE = vim.opt.hlsearch
function TOGGLE_SEARCH_HIGHLIGTING()
  if HLSTATE == true then
    vim.opt.hlsearch = false
    HLSTATE = false
  else
    vim.opt.hlsearch = true
    HLSTATE = true
  end
end
vim.api.nvim_create_user_command('HighlightToggle', 'lua TOGGLE_SEARCH_HIGHLIGTING()', {})

You can then run it with the command :HighlightToggle (and assign in a keymap if you want).

物价感观 2025-01-05 15:25:05

我在 vimrc 中使用此映射:

nnoremap <silent> <leader>a :let v:hlsearch=(&hls && !v:hlsearch)<CR>

基本上,如果 hlsearch 关闭,则映射是无操作的(v:hlsearch 始终为 false)。如果打开,则 \a 会切换搜索突出显示,而不更改 hlsearch 设置。 (不完全是OP所要求的,但我真的不喜欢每次新搜索开始时都打开 hlsearch 的想法。)

I use this mapping in my vimrc:

nnoremap <silent> <leader>a :let v:hlsearch=(&hls && !v:hlsearch)<CR>

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 the hlsearch setting. (Not exactly what OP asked, but I really don't like the idea of switching hlsearch on every time new search starts.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文