Vim 对象选择与语法感知文本对象

发布于 2024-09-08 16:24:22 字数 711 浏览 3 评论 0原文

我刚刚了解了真正令人敬畏的 对象-选择vim的功能。当光标位于某个“文本对象”内时,一组简单的动词可以选择或操作整个对象。例如,将光标放在下面引号内的任意位置(例如,在“o”上方):

print "Hello, world"
           ^

命令 vi" 将选择引号内的整个短语。相同的功能适用于多个“< vim 定义的 href="http://vimdoc.sourceforge.net/htmldoc/motion.html#object-select" rel="nofollow noreferrer">文本对象",包括单词、句子、段落和 。

但现在我希望“文本对象”的概念能够识别我正在编写的语言,例如,考虑以下 python:

re.sub("[^A-Z ]", " ", string)

我希望能够放置 将光标放在该位置上,然后选择整个内容。函数调用是一个定义明确的语法结构,但它不是“单词”、“句子”、“段落”,也不是用引号或大括号括起来

。有没有任何插件或 vimrc hacks 可以定义这些依赖于语言的“文本对象”?

I just learned about the truly awesome object-select capabilities of vim. With the cursor within some "text object", a set of simple verbs can select or operate on the whole object. For example, with the cursor anywhere inside the quotes below (e.g. over the 'o'):

print "Hello, world"
           ^

The command vi" will select the whole phrase inside the quotes. The same capability works with a number of "text objects" that vim defines, including words, sentences, paragraphs, and characters enclosed by quotes, parentheses, and braces.

But now I want this notion of a "text object" to be aware of the language I'm writing. For example, consider the following python:

re.sub("[^A-Z ]", " ", string)

I'd like to be able to place the cursor somewhere over that, and to select the whole thing. The function call is a well-defined syntactic construct, but it isn't a "word", "sentence", "paragraph", or enclosed in quotes or braces.

Are there any plugins or vimrc hacks out there that define these sorts of language-dependent "text objects"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

知你几分 2024-09-15 16:24:22

此脚本使用缩进来定义文本对象。如果您按照通用标准进行格式化,并且保证适用于 python,它将适用于多种语言。

This script uses indentation to define a text-object. It'll work for many languages if you're formatting according to common standards, and guaranteed for python.

晨曦÷微暖 2024-09-15 16:24:22

虽然可以构建选择整个语法区域的映射,但这不适用于您的给定场景,因为没有“函数调用”语法区域。

一种选择是选择括号表达式,然后向后扩展以包含函数调用。

va)oB
  • va) 选择括号表达式
  • o 切换光标位于视觉选择的哪一端以及展开的方向。
  • B 将光标向后移动一个 WORD。即,到光标之前的空白之后的字符。

Although it's is possible to construct maps that will select an entire syntax region, that wouldn't work with your given scenario since there isn't a "function call" syntax region.

One option is to select the parenthetical expression and then extend that backwards to include the function call.

va)oB
  • va) selects the parenthetical expression
  • o toggles which end of the visual selection the cursor is at and which direction you're expanding.
  • B moves the cursor backwards one WORD. That is, to the character just after the whitespace previous to the cursor.
ㄖ落Θ余辉 2024-09-15 16:24:22

有一个插件来创建用户定义的文本对象。评分不高,但可能值得一试。

There is a plugin out there to create user defined text-objects. Doesn't have a huge rating, but it might be worth a shot.

谁许谁一生繁华 2024-09-15 16:24:22

我对此事的看法: https://github.com/ngn/vim-select-by -syntax

我并不是真的想让它成为一个文本对象,但无论如何,它解决了同样的问题。

My take on the matter: https://github.com/ngn/vim-select-by-syntax

I didn't really want to make it a text object, but anyway, it solves the same problem.

被你宠の有点坏 2024-09-15 16:24:22

我的 SameSyntaxMotion 插件 提供跳转到边界和下一个 [count] 的映射以与光标下相同的方式突出显示文本的出现。 ayiy 文本对象将选择
周围的文本属于同一语法组,或者以相同的方式突出显示。

My SameSyntaxMotion plugin provides mappings to jump to the borders and next [count] occurrences of text highlighted in the same way as under the cursor. The ay and iy text objects will select the
surrounding text that belongs to the same syntax group, or is highlighted the same way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文