Vim 对象选择与语法感知文本对象
我刚刚了解了真正令人敬畏的 对象-选择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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此脚本使用缩进来定义文本对象。如果您按照通用标准进行格式化,并且保证适用于 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.
虽然可以构建选择整个语法区域的映射,但这不适用于您的给定场景,因为没有“函数调用”语法区域。
一种选择是选择括号表达式,然后向后扩展以包含函数调用。
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)
selects the parenthetical expressiono
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.有一个插件来创建用户定义的文本对象。评分不高,但可能值得一试。
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.
我对此事的看法: 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.
我的 SameSyntaxMotion 插件 提供跳转到边界和下一个 [count] 的映射以与光标下相同的方式突出显示文本的出现。
ay
和iy
文本对象将选择周围的文本属于同一语法组,或者以相同的方式突出显示。
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
andiy
text objects will select thesurrounding text that belongs to the same syntax group, or is highlighted the same way.