Vim 搜索并突出显示脚本中的控制
我正在编写一个脚本,我想在其中以编程方式控制搜索,并突出显示它们。 search() 函数结果没有突出显示(我认为),因此使用该函数对我来说没有用。
我想做的是使用“正常/”命令来搜索变量,但这似乎并不简单。我可以编写命令脚本:(
execute 'normal /' . my_variable . '\<CR>'
或 vim 提示中建议的其他变体: http://vim.wikia .com/wiki/Using_normal_command_in_a_script_for_searching )
但它没有做任何事情。执行脚本行后,我可以在命令行中看到正确的搜索项,但焦点位于文档中,搜索寄存器尚未更改,并且光标未进行任何搜索。 (虽然没有抛出错误,但似乎没有输入
我至少可以通过这样做来控制搜索寄存器:
execute 'let @/ ="' . a:term .'"'
然后显而易见的事情似乎是执行 a:
normal n
但如果我在脚本中运行它,“正常 n”不会执行任何操作。如果我在脚本终止搜索后手动按“n”,设置搜索寄存器确实有效(并且突出显示,因为 hlsearch 已打开)。我什至不关心光标是否定位,我只想突出显示寄存器模式。但脚本中“set hlsearch”的各种组合也不起作用。
我知道我可以使用“match()”,但我想让它与常规搜索突出显示一起使用,我想知道我做错了什么。这一定是简单的事情,但我没有看到。感谢您的任何帮助。
I'm writing a script in which I want to control searches programmatically, and get them highlighted. The search() function results are not highlighted (I think), so using that function is not of use to me.
What I want to do is use the 'normal /' command to search for a variable, but that doesn't seem to be straightforward. I can script the command:
execute 'normal /' . my_variable . '\<CR>'
(or other variations as suggested in the vim tip here: http://vim.wikia.com/wiki/Using_normal_command_in_a_script_for_searching )
but it doesn't do anything. I can see the correct search term down in the command line after execution of the script line, but focus is in the document, the search register has not been altered, and the cursor has not done any search. (It seems as though the < CR > isn't getting entered, although no error is thrown -- and yes, I have tried using the literal ^M too.)
I can at least control the search register by doing this:
execute 'let @/ ="' . a:term .'"'
and then the obvious thing seems to be to do a:
normal n
But that 'normal n' doesn't do anything if I run it in a script. Setting the search register does work, if I manually press 'n' after the scrip terminates the search happens (and highlighting appears, since hlsearch is on). I don't even care if the cursor is positioned, I just want the register pattern to be highlighted. But various combinations of 'set hlsearch' in the script don't work either.
I know I could use 'match()', but I want to get it working with regular search highlighting, and I wonder what I'm doing wrong. It must be something simple but I'm not seeing it. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
跑步:
从你的函数内部然后运行
来自函数外部的
或者你可以使用:
如果您不希望光标移动,
而不是正常的 n (如果没有函数之外的东西,我无法找到另一种方法来做到这一点。)
run:
from inside your function then run
from outside your function (inside it does nothing) eg.
or you can use:
instead of normal n if you don't want the cursor to move
(I cannot work out another way of doing this without having something outside the function.)
如果您的脚本使用函数,那么来自
:help function-search-undo
的引用是相关的:Vim 通常会在函数结束时尝试重置搜索模式(以及其他一些事情),通常您可以通过将
n
(下一个搜索)添加到映射的末尾,或使用:map
并让您的函数返回要执行的键序列。If your script is using functions, then this quote from
:help function-search-undo
is relevant:Vim usually tries to reset the search pattern (and a few other things) when a function ends, often you can get around this by adding the
n
(next search) to the end of a mapping, or using:map <expr>
and having your function return the key sequence to be executed.仔细检查后,似乎
\
没有在单引号内被拾取。尝试使用这个代替:On closer inspection, it seems
\<CR>
is not picked up inside single quotes. Try using this instead: