Vim:获取光标下语法元素的内容
我正在处理突出显示的复杂语法元素,并希望获取其内容。你能想出什么方法来做到这一点吗?
也许有某种方法可以搜索正则表达式以使其包含光标?
编辑
好的,举个例子。光标在一个字符串内,我想获取文本,即该语法元素的内容。考虑以下行:
String myString = "Foobar, [CURSOR]cool \"string\""; // And some other "rubbish"
我想编写一个返回的函数
"Foobar, cool \"string\""
I'm on a highlighted complex syntax element and would like to get it's content. Can you think of any way to do this?
Maybe there's some way to search for a regular expression so that it contains the cursor?
EDIT
Okay, example. The cursor is inside a string, and I want to get the text, the content of this syntactic element. Consider the following line:
String myString = "Foobar, [CURSOR]cool \"string\""; // And some other "rubbish"
I want to write a function that returns
"Foobar, cool \"string\""
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解这个问题。我不久前发现了这个宝石,但不记得在哪里,但我曾经了解语法高亮在 vim 中的工作原理:
if I understood the question. I found this gem some time ago and don't remember where but i used to understand how syntax hilighting works in vim:
textobj-syntax 插件可能会有所帮助。它创建一个自定义文本对象,以便您可以运行 viy 来直观地选择当前语法突出显示的元素。该插件依赖于 textobj-user 插件,这是一个框架创建自定义文本对象。
The textobj-syntax plugin might help. It creates a custom text object, so that you can run
viy
to visually select the current syntax highlighted element. The plugin depends on the textobj-user plugin, which is a framework for creating custom text objects.这对于文本对象(
:help text-objects
)来说是一个很好的用途。要获取您要查找的内容(Foobar,cool \"string\"
),您只需执行以下操作:默认情况下,yank 命令使用未命名寄存器(
""
,请参阅:help registers
),因此您可以使用getreg()
函数或简写@{register-name}
以编程方式访问提取的内容code>:或者您可以将内容拉入不同的寄存器:
将内部带引号的字符串拉入
"q
寄存器,因此它不会与标准寄存器用法冲突(并且可以作为 <代码>@q变量)。This is a good use for text objects (
:help text-objects
). To get the content you're looking for (Foobar, cool \"string\"
), you can just do:The yank command by default uses the unnamed register (
""
, see:help registers
), so you can access the yanked contents programmatically using thegetreg()
function or the shorthand@{register-name}
:Or you can yank the contents into a different register:
yanks the inner quoted string into the
"q
register, so it doesn't conflict with standard register usage (and can be accessed as the@q
variable).编辑:看到nelstrom提到的插件与我原来的方法类似,我选择了这个稍微更优雅的解决方案:
EDIT: Seeing that the plugin mentioned by nelstrom works similar to my original approach, I settled on this slightly more elegant solution: