你能在 vi/vim 中抓取或删除括号之间的内容吗?
给定 C 语言中的这行代码:
printf("%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32)) );
有没有办法从第一个粗体括号删除或拉到其匹配的括号? 我考虑过 df),但这只能让你到达 9.0 之后的版本。
有没有类似的方法让 vim 抓取匹配大括号之间的所有内容,而不管换行符?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
dib
或di(
怎么样。它将删除内部 (.. .)
我喜欢 text-object 动作和选择!
What about
dib
ordi(
.It will delete the inner (...) block where the cursor is.
I love text-object motions and selections!
各种动作: %
%
命令 跳转到光标下的项目的匹配。 将光标定位在左(或右)括号上,并使用 y% 进行拉取或使用 d% 删除从光标到匹配括号的所有内容。这是有效的,因为
%
是一个“运动命令”,所以它可以在 vim 需要这样命令的任何地方使用。 来自:help y
:默认情况下,“item " 包括方括号、大括号、括号、C 风格注释和各种预编译器语句(
#ifdef
等)。有一个“扩展%匹配”插件,你可以在 Vim 主页< /a>.
您可以通过输入 % 和相关运动命令的文档>
:在命令模式下帮助各种动作
。object-select
还有另一组运动命令,您可以在可视模式下使用它们来选择各种文本对象。
要解决您的特定问题,您可以执行以下操作:
假设您的光标位于
^
。 输入以下序列来选择您要查找的部分:首先
v
进入可视模式,然后您指定要向上进行2
级括号。 最后,a)
选择“a block”。 之后你可以使用d
或x
删除等。如果你不想包含外括号,你可以使用“inner block”代替:
参见< a href="http://vimdoc.sourceforge.net/htmldoc/motion.html#object-select" rel="noreferrer">
:help object-select
查看完整列表的相关命令。Various Motions: %
The
%
command jumps to the match of the item under the cursor. Position the cursor on the opening (or closing) paren and usey%
for yanking ord%
for deleting everything from the cursor to the matching paren.This works because
%
is a "motion command", so it can be used anywhere vim expects such a command. From:help y
:By default, "item" includes brackets, braces, parens, C-style comments and various precompiler statements (
#ifdef
, etc.).There is a plugin for "extended % matching" that you can find on the Vim homepage.
You can read the documentation on
%
and related motion commands by entering:help various-motions
in command mode.object-select
There is another set of motion commands that you can use in Visual mode to select various text objects.
To solve your specific problem you would do the following:
Let's say your cursor is positioned at
^
. Enter the following sequence to select the part you are looking for:First
v
enters Visual mode, then you specify that you want to go2
levels of parens up. Finally thea)
selects "a block". After that you can used
orx
to delete, etc.If you don't want to include the outer parens, you can use "inner block" instead:
See
:help object-select
for the complete list of related commands.要删除一对括号内的所有内容,您始终可以发出
di(
及其派生词。注意:
正如 @porglezomb 在他的评论中建议的那样,您可以使用
a
(“连同”)而不是i
(“内部”)来包含括号,因此,使用da(
会删除内的所有内容。 (
和)
包括(
和)
删除最外一对括号内的文本:。
因此,对于这行代码,
假设您的光标位于上述光标范围内,您可以发出以下命令:
删除第 n 个外部括号内的文本:
要抓取第n对外括号,只需在上述命令前添加
n
即可,因此,光标位置与上面相同,To delete all that is inside a pair of parentheses, you can always issue
di(
and its derivatives.Note :
As @porglezomb suggested in his comment, you can use
a
("along with") instead ofi
("inside") to include the parentheses. So, usingda(
deletes everything inside(
and)
including(
and)
.Deleting text inside the immediate outer pair of parentheses :
So, for this line of code
assuming that your cursor is inside the above mentioned cursor range, you can issue the following commands :
Deleting text inside the n-th outer pair of parentheses :
To grab everything inside the n-th outer pair of parentheses, just add
n
before the above command. So, with the same cursor position as above,您可以使用
d%
进行删除,使用y%
进行拉取。You can use
d%
for deleting andy%
for yanking.将光标放在第一个括号上,然后按
v%y
或v%d
。Place your cursor on the first parenthesis, then press
v%y
orv%d
.尝试 ci[block-surrounder]
在您的情况下,将光标放在突出显示的 2 个括号之间的任意位置,然后尝试按键: ci(
Try ci[block-surrounder]
In your case, place the cursor anywhere between the 2 parenthesis that you highlighed and try the keys: ci(
正如 David Norman 的回答所说,
将光标放在第一个括号上,然后按
v%y
或v%d
。来自 http://vimdoc.sourceforge.net/htmldoc/vimindex.html 的解释:
这意味着它将选择两个括号 (
%
) 之间(包括这两个括号)的所有内容,同时以可视方式向您显示所选内容 (v
),然后猛拉/复制y 或删除/删除它。 (到默认缓冲区。)
您可以使用
p
进行放置/粘贴。回答了“自学钓鱼”。
As answer of David Norman says,
Place your cursor on the first parenthesis, then press
v%y
orv%d
.Explanation from http://vimdoc.sourceforge.net/htmldoc/vimindex.html:
This means it will select everything between and including the two brackets (
%
) while showing the selection to you visually (v
) and then yank/copyy
or delete/cutd
it. (To the default buffer.)You can put/paste with
p
.Made this answer to "teach myself to fish".