检测折叠线或增量搜索(?)

发布于 2025-01-05 16:43:13 字数 269 浏览 3 评论 0原文

好吧,我同意——这个标题毫无用处。问题是,我不知道如何将其放入一个衬垫中...

我想做的是映射空格键,以便它充当 :nohl 映射,但是同时,当它处于折叠线上时,在正常模式(打开/关闭折叠)下充当za

这可能吗?

我遇到的麻烦是区分两者 - 有没有办法“检测”光标下方的折叠线,或检测“当前正在进行”的增量搜索(例如,有一些突出显示的内容)?

或者我以完全错误的方式解决这个问题?欢迎所有建议!

Okey, I agree - the title is useless. The thing is, I have no idea how to put this into a one liner...

What I'm trying to do is to map the space key so that it serves as a :nohl mapping, but at the same time, when it is on a folded line, to serve as a za in normal mode (open/close fold).

Is this even possible?

What I'm having trouble is distinguishing between the two - is there a way to "detect" a folded line below the cursor, or to detect an incremental search "currently in progress" (as in, there is something highlighted)?

Or am I tackling this in a completely wrong way? All advices welcomed!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

瞳孔里扚悲伤 2025-01-12 16:43:13

重用 Andy Rk 的想法:

function! FoldSetWithNoHL()
    set nohls
    if (foldclosed(line('.')) >= 0)
        exe "normal za"
    endif
endfunction

map <space> :silent! call FoldSetWithNoHL()<cr>

Reusing ideas from Andy Rk:

function! FoldSetWithNoHL()
    set nohls
    if (foldclosed(line('.')) >= 0)
        exe "normal za"
    endif
endfunction

map <space> :silent! call FoldSetWithNoHL()<cr>
留一抹残留的笑 2025-01-12 16:43:13

它可能很难看,但它对我有用:

noremap <Space> :nohl<CR> za

缺点是在展开的行上按空格时会发生错误。

It may be ugly, but it worked for me:

noremap <Space> :nohl<CR> za

The drawback is an error occuring when pressing space on unfolded lines.

诠释孤独 2025-01-12 16:43:13

合并对我之前评论中列出的问题的修复:

function! ToggleFoldWithNoHL()
  if foldlevel('.')
    normal! za
  endif
endfunction

nnoremap <silent> <space> :nohlsearch<cr>:call ToggleFoldWithNoHL()<cr>

似乎没有办法检测是否有活动的搜索突出显示,因此如果您在折叠内有活动的搜索,这将清除搜索,但也会关闭折叠。在这种情况下,另一个空间应该可以让您回到您想去的地方。

Incorporating fixes to the issues I listed in my earlier comment:

function! ToggleFoldWithNoHL()
  if foldlevel('.')
    normal! za
  endif
endfunction

nnoremap <silent> <space> :nohlsearch<cr>:call ToggleFoldWithNoHL()<cr>

There doesn't seem to be a way to detect that there's an active search highlight, so if you have a search active inside a fold, this will clear the search but also close the fold. In that case, another space should put you right back where you want to be.

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