如何在 VimScript 中获得视觉上选择的文本 [英] How to get visually selected text in VimScript
问题描述
我可以使用 getpos()
获取光标位置,但我想在一行内检索所选文本 内,即 '<,'>
.这是怎么做的?
I'm able to get the cursor position with getpos()
, but I want to retrieve the selected text within a line, that is '<,'>
. How's this done?
更新
我想我删掉了我解释说我想从 Vim 脚本中获取此文本的部分...
I think I edited out the part where I explained that I want to get this text from a Vim script...
推荐答案
我不完全确定这里的上下文,因为 getpos()
确实可以接受标记(例如 '<
和 '>
) 作为参数.
I'm not totally sure about the context here, because getpos()
can indeed accept marks (like '<
and '>
) as arguments.
然而,为了了解您可能需要的内容,还有 v
,它类似于 '<
,只是它总是更新(即当用户仍处于可视模式).这可以与 .
结合使用,当前光标位置将代表视觉选择的结束.
However, to take a stab at what you might be asking for, there's also v
, which is like '<
except it's always updated (i.e. while the user is still in visual mode). This can be used in combination with .
, the current cursor position, which will then represent the end of the visual selection.
我在 :help line()
中找到了这些;包括 line()
和 getpos()
在内的几个函数具有相同的可能参数集.
I found these in :help line()
; several functions including line()
and getpos()
have the same set of possible arguments.
我猜您可能只是在问如何在两个任意标记之间获取文本,而不是逐行...(即这与视觉模式无关).我认为实际上没有办法.是的,这似乎是一个非常明显的遗漏.您应该可以通过使用 getpos()
查找标记来伪造它,使用 getline()
获取所有行,然后根据列位置(个案工作取决于它是否是多行).抱歉,这不是一个真正的答案,但至少您可以将其包装在一个函数中并忘记它.
I guess you're probably simply asking how to get the text between two arbitrary marks, not going line-by-line... (i.e. this doesn't specifically pertain to visual mode). I don't think there actually is a way. Yes, this seems like a pretty glaring omission. You should be able to fake it by finding the marks with getpos()
, getting all the lines with getline()
, then chopping off on the first and last according to the column position (with casework depending on whether or not it's multi-line). Sorry it's not a real answer, but at least you can wrap it up in a function and forget about it.
这篇关于如何在 VimScript 中获得视觉上选择的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!