Vim可视化模式脚本:搜索视觉选择周围的文本

时间:2023-01-14 18:57:35

I'll try to explain by example..

我会尝试通过例子来解释..

If we have a piece of code like this in vim:

如果我们在vim中有一段这样的代码:

if ($feck == true && $drink == false)
{
    echo 'They lie in wait like wolves..';
}

And I go to visual mode and select "$drink" for example, is there a way to:

我进入视觉模式并选择“$ drink”,例如,有没有办法:

  • detect whether the current selection is one of vim's text-objects (word, WORD, inner {, etc.)
  • 检测当前选择是否是vim的文本对象之一(word,WORD,inner {等)

  • do a lookup on both sides of the selection to check for the next available vim text-object (again, word, WORD, a block, inner ", etc..)
  • 在选择的两边进行查找以检查下一个可用的vim文本对象(再次,单词,WORD,块,内部“等等。)

Please note that I'm thinking of vim scripting not just plain editing, so both of these "actions" would be in a function, sourced in my .vimrc

请注意,我正在考虑vim脚本而不仅仅是简单的编辑,所以这两个“动作”都在一个函数中,源自我的.vimrc

Also, I would need to do this without breaking the visual selection or moving the cursor (or, if it really can't be avoided, restore both the selection and cursor position).

此外,我需要在不破坏视觉选择或移动光标的情况下执行此操作(或者,如果确实无法避免,则还原选择和光标位置)。

2 个解决方案

#1


This question only makes sense in the context of this other question by dr Hannibal Lecter.

这个问题在汉尼拔莱克特博士的另一个问题的背景下才有意义。

I think what you want to achieve is possible, but it requires some work. The information given in the other answer and the comments to your post, already point in the right direction.

我认为你想要实现的目标是可能的,但它需要一些工作。其他答案中给出的信息以及对帖子的评论已经指出了正确的方向。

Your vim function that is called from your "extend selection" key mapping must do the following:

从“扩展选择”键映射调用的vim函数必须执行以下操作:

  1. Check what the current selection probably represents (markers '< and '> as dwc describes and vim functions should be helpful for this). Questions like this must be answered: is '< and '> on the same line? Are all letters between '< and '> iskeyword characters? Are the first and last characters of the selection special delimiters like '{','(', etc. ? And so on - there is no builtin functionality that checks this automatically. See also :help visualmode(

    检查当前选择可能代表什么(标记' <和'> ,因为dwc描述和vim函数应该对此有所帮助)。必须回答这样的问题:在同一行是' <和'> 吗? “ <和”> 之间的所有字母都是关键字字符吗?选择的第一个和最后一个字符是特殊分隔符,如'{','('等等?等等 - 没有内置功能可以自动检查它。另请参阅:help visualmode(

  2. Once this is determined, you have to think, how you then would like to extend your selection further - however the logic for this works in ReSharper.

    确定之后,您必须考虑,然后您希望如何进一步扩展您的选择 - 但是这个逻辑在ReSharper中有效。

  3. You will need to take care of putting the cursor back where it was, depending on the commands you used to extend the selection.

    您需要注意将光标放回原位,具体取决于您用于扩展选择的命令。

In general, the commands presented in :help eval.txt will be helpful in implementing this.

通常,帮助eval.txt中显示的命令将有助于实现此目的。

#2


After a visual selection is made, '< and '> are available for use in anything that accepts a "range."

在进行视觉选择后,' <和'> 可用于任何接受“范围”的事物。

#1


This question only makes sense in the context of this other question by dr Hannibal Lecter.

这个问题在汉尼拔莱克特博士的另一个问题的背景下才有意义。

I think what you want to achieve is possible, but it requires some work. The information given in the other answer and the comments to your post, already point in the right direction.

我认为你想要实现的目标是可能的,但它需要一些工作。其他答案中给出的信息以及对帖子的评论已经指出了正确的方向。

Your vim function that is called from your "extend selection" key mapping must do the following:

从“扩展选择”键映射调用的vim函数必须执行以下操作:

  1. Check what the current selection probably represents (markers '< and '> as dwc describes and vim functions should be helpful for this). Questions like this must be answered: is '< and '> on the same line? Are all letters between '< and '> iskeyword characters? Are the first and last characters of the selection special delimiters like '{','(', etc. ? And so on - there is no builtin functionality that checks this automatically. See also :help visualmode(

    检查当前选择可能代表什么(标记' <和'> ,因为dwc描述和vim函数应该对此有所帮助)。必须回答这样的问题:在同一行是' <和'> 吗? “ <和”> 之间的所有字母都是关键字字符吗?选择的第一个和最后一个字符是特殊分隔符,如'{','('等等?等等 - 没有内置功能可以自动检查它。另请参阅:help visualmode(

  2. Once this is determined, you have to think, how you then would like to extend your selection further - however the logic for this works in ReSharper.

    确定之后,您必须考虑,然后您希望如何进一步扩展您的选择 - 但是这个逻辑在ReSharper中有效。

  3. You will need to take care of putting the cursor back where it was, depending on the commands you used to extend the selection.

    您需要注意将光标放回原位,具体取决于您用于扩展选择的命令。

In general, the commands presented in :help eval.txt will be helpful in implementing this.

通常,帮助eval.txt中显示的命令将有助于实现此目的。

#2


After a visual selection is made, '< and '> are available for use in anything that accepts a "range."

在进行视觉选择后,' <和'> 可用于任何接受“范围”的事物。