简体   繁体   English

如何从vim的`operator-pending`映射内的搜索中删除突出显示的内容?

[英]How to remove the highlight from a search inside an `operator-pending` mapping in vim ?

I'm trying to define an operator-pending mapping (using onoremap ) to match all text between the previous { and the next } . 我正在尝试定义一个运算符待处理的映射(使用onoremap )以匹配上一个{和下一个}之间的所有文本。 This is for use in a css file, so I can easily delete, change or copy rules with ir . 这用于css文件中,因此我可以使用ir轻松删除,更改或复制规则。

I've used the syntax used in "Learn vimscript the hard way", which is a combination of execute and normal . 我使用了“以硬方式学习vimscript”中使用的语法,该语法是executenormal的组合。 What I have been unable to do is clear the highlight search after my last search. 我一直无法做的是在上次搜索后清除突出显示搜索。 After using the mapping, all the } in the file get highlighted. 使用映射后,文件中的所有}都会突出显示。

So far, here's what I got : 到目前为止,这是我得到的:

au Filetype css noremap <buffer> ir :<C-U>execute "normal! ?{\rjV/}\rk"<CR>

I know I need to call :nohlsearch but I don't know how to fit that in the mapping. 我知道我需要打电话给:nohlsearch但是我不知道如何在映射中适应它。 The mapping results in a text being visually selected, so when :nohlsearch gets called it's applied to the selection, which does not work. 映射会导致在视觉上选择文本,因此当:nohlsearch被调用时,它将应用于选择,这是行不通的。

I need a way to clear the '<,'> markers but I couldn't find how to write <CU> in the normal command to get it executed right. 我需要一种清除'<,'>标记的方法,但是我找不到如何在normal命令中编写<CU>使其正确执行的方法。 Another way might be to leave Visual mode, execute the :nohlsearch and then reselect the last selected text with gv , but even for that I can't find how to write <Esc> in my normal statement. 另一种方法可能是退出可视模式,执行:nohlsearch ,然后使用gv重新选择最后选择的文本,但是即使如此,我也无法在normal语句中找到如何编写<Esc>

In this particular case, it's probably easier to use something like: 在这种情况下,使用类似这样的方法可能会更容易:

au Filetype css noremap <buffer> ir :<C-U>call search('{', 'Wb')\|call search('}', 'Ws')\|normal! v''<CR>

Note that this does what your example does, not the right thing. 请注意,这是您的示例所做的事情,而不是正确的事情。 Nested braces for example won't be handled correctly. 例如,嵌套括号无法正确处理。

Calling the search function without the 's' flag won't set any marks or change the search register so you don't need to reset anything. 调用不带“ s”标志的搜索功能不会设置任何标记或更改搜索寄存器,因此您无需重置任何内容。

Note that this isn't really the best way of accomplishing what you want - you probably want the iB or i{ (they're the same) text object instead. 请注意,这并不是真正实现所需目标的最佳方法-您可能希望使用iBi{ (它们是相同的)文本对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM