如何在 Vim 中快速选择(内部)行
Vim 有一些非常有用的文本动作,例如 ib、i)、i} 等快速选择文本的一部分。但是,我经常需要逐行模式的内部块。例如(使用行号):
1: $foo = array(
2: 'bar' => 'Bar',
3: 'quux' => 'Quux',
4: );
现在,当我的光标位于第 2 行或第 3 行某处时,我点击 vib 或 vi),Vim 选择第 2、3 行和第 4 行的缩进空格。我只想第2行和第3行。我尝试过 Vib 和 Vi ) 但它们的作用与小写 v 相同。
是否有任何简单的文本运动或其他快速方法可以在逐行模式下选择内部块?
Vim has some very useful text motions such as ib, i), i}, etcetera to quickly select a portion of text. But, I often need an inner block in line-wise mode. For example (with line numbers):
1: $foo = array(
2: 'bar' => 'Bar',
3: 'quux' => 'Quux',
4: );
Now, when my cursor is on line 2 or 3 somewhere and I hit vib or vi), Vim selects line 2, 3 and the indenting spaces on line 4. I just want line 2 and 3. I have tried with Vib and Vi) but they do the same as with a lowercase v.
Is there any easy text motion or other quick way to select an inner block in line-wise mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能会对此投反对票,但我只需根据需要的行数进行向上或向下运动,例如 V3j或 V2k 只要已知即可。
我有一个在绝对编号和相对编号之间切换的绑定 (
:setrelativenumber
),因此只要它们都在屏幕上,就可以在视觉上轻松识别。I may get downvotes for this, but I'd just do an upward or downward motion the number of lines I need, like V3j or V2k as long as it's known.
I have a binding that switches between absolute and relative numbering (
:set relativenumber
) so it's visually easy to identify as long as they're all on screen.我同意拥有您所描述的行为会很好。如果您下定决心,那么您可以随时使用 Kana 的 textobj-user 框架。我用它来创建 用于使用 ruby 的文本对象块,并发现它非常容易做到。
I agree that it would be nice to have the behaviour that you describe. If you're determined, then you could always roll your own custom text object using Kana's textobj-user framework. I used this to create a text object for working with ruby blocks, and found it surprisingly easy to do.
我最终找到了一个更简单的方法: vim-indent-object 允许我根据缩进级别选择文本区域。考虑到我的源代码总是很好地缩进,这几乎是同一件事。
现在,如果我在示例中位于第 2 行或第 3 行,我只需按 vii 即可选择内部缩进对象,然后它选择第 2 行和第 3 行。Groovy!
I found a simpler way in the end: vim-indent-object allows me to select regions of text based on the indentation level. This is pretty much the same thing considering my source code is always nicely indented.
Now, if I am on line 2 or 3 in my example, I can simply press vii to select the inner indentation object, and it selects line 2 and 3. Groovy!