Vim搜索和脚本高亮显示控件 [英] Vim search and highlighting control from a script

查看:59
本文介绍了Vim搜索和脚本高亮显示控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,在该脚本中我要以编程方式控制搜索并将其突出显示. (我认为)search()函数的结果未突出显示,因此使用该函数对我来说没有用.

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.

我想做的是使用'normal/'命令搜索变量,但这似乎并不简单.我可以编写命令脚本:

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>'

(或vim提示中此处建议的其他变体: http://vim.wikia.com/wiki/Using_normal_command_in_a_script_for_searching )

(or other variations as suggested in the vim tip here: http://vim.wikia.com/wiki/Using_normal_command_in_a_script_for_searching )

但是它什么也没做.执行脚本行后,我可以在命令行中看到正确的搜索词,但是焦点在文档中,搜索寄存器未更改,并且光标未执行任何搜索. (似乎没有输入< CR>,尽管未引发任何错误-是的,我也尝试使用文字^ M.)

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

但是,如果我在脚本中运行它,那么正常n"不会执行任何操作.如果在脚本终止后我手动按"n",则设置搜索寄存器确实有效,因为搜索发生了(并且高亮显示了,因为hlsearch处于打开状态).我什至都不在乎光标的位置,我只想突出显示寄存器模式.但是脚本中的'set hlsearch'的各种组合也不起作用.

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.

我知道我可以使用'match()',但是我想让它与常规搜索突出显示一起使用,并且我想知道我做错了什么.它一定很简单,但我看不到.感谢您的帮助.

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.

推荐答案

如果您的脚本正在使用函数,则:help function-search-undo中的引号是相关的:

If your script is using functions, then this quote from :help function-search-undo is relevant:

上次使用的搜索模式和重做命令." 不会被功能改变.这也是 表示:nohlsearch的作用被撤消 函数返回时.

The last used search pattern and the redo command "." will not be changed by the function. This also implies that the effect of :nohlsearch is undone when the function returns.

Vim通常会在函数结束时尝试重置搜索模式(以及其他一些事情),通常可以通过在映射的末尾添加n(下一个搜索)或使用,然后让您的函数返回要执行的键序列.

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.

这篇关于Vim搜索和脚本高亮显示控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆