你能在 vi/vim 中抓取或删除括号之间的内容吗?

发布于 2024-07-11 19:50:34 字数 295 浏览 7 评论 0 原文

给定 C 语言中的这行代码:

printf("%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32)) );

有没有办法从第一个粗体括号删除或拉到其匹配的括号? 我考虑过 df),但这只能让你到达 9.0 之后的版本。

有没有类似的方法让 vim 抓取匹配大括号之间的所有内容,而不管换行符?

Given this line of code in C:

printf("%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32)));

Is there a way to delete or yank from the first bold parenthesis to its matching parenthesis? I thought about df), but that only will get you to just after the 9.0.

Is there a similar way to get vim to grab everything between matching braces, regardless of newlines?

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

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

发布评论

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

评论(7

骷髅 2024-07-18 19:50:34

dibdi( 怎么样。

它将删除内部 (.. .)

我喜欢 text-object 动作和选择!

What about dib or di(.

It will delete the inner (...) block where the cursor is.

I love text-object motions and selections!

卖梦商人 2024-07-18 19:50:34

各种动作: %

% 命令 跳转到光标下的项目的匹配。 将光标定位在左(或右)括号上,并使用 y% 进行拉取或使用 d% 删除从光标到匹配括号的所有内容。

这是有效的,因为 % 是一个“运动命令”,所以它可以在 vim 需要这样命令的任何地方使用。 来自 :help y

["x]y{motion}       Yank {motion} text [into register x].  When no
                    characters are to be yanked (e.g., "y0" in column 1),
                    this is an error when 'cpoptions' includes the 'E'
                    flag.

默认情况下,“item " 包括方括号、大括号、括号、C 风格注释和各种预编译器语句(#ifdef 等)。

有一个“扩展%匹配”插件,你可以在 Vim 主页< /a>.

您可以通过输入 % 和相关运动命令的文档>:在命令模式下帮助各种动作

object-select

还有另一组运动命令,您可以在可视模式下使用它们来选择各种文本对象。

要解决您的特定问题,您可以执行以下操作:

printf("%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32)));
                                   ^

假设您的光标位于 ^。 输入以下序列来选择您要查找的部分:

v2a)

首先 v 进入可视模式,然后您指定要向上进行 2 级括号。 最后,a) 选择“a block”。 之后你可以使用dx删除等。

如果你不想包含外括号,你可以使用“inner block”代替:

v2i)

参见< 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 use y% for yanking or d% 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:

["x]y{motion}       Yank {motion} text [into register x].  When no
                    characters are to be yanked (e.g., "y0" in column 1),
                    this is an error when 'cpoptions' includes the 'E'
                    flag.

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:

printf("%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32)));
                                   ^

Let's say your cursor is positioned at ^. Enter the following sequence to select the part you are looking for:

v2a)

First v enters Visual mode, then you specify that you want to go 2 levels of parens up. Finally the a) selects "a block". After that you can use d or x to delete, etc.

If you don't want to include the outer parens, you can use "inner block" instead:

v2i)

See :help object-select for the complete list of related commands.

何以畏孤独 2024-07-18 19:50:34

要删除一对括号内的所有内容,您始终可以发出 di( 及其派生词。

注意:

正如 @porglezomb 在他的评论中建议的那样,您可以使用 a(“连同”)而不是 i(“内部”)来包含括号,因此,使用 da( 会删除 内的所有内容。 () 包括 ()

删除最外一对括号内的文本:

因此,对于这行代码,

printf("%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32)));
                                ^       ^
                                |       |
                                 \_______\___---> Cursor range

假设您的光标位于上述光标范围内,您可以发出以下命令:

di(   --> Deletes '5.0/9.0'
ci(   --> Substitutes '5.0/9.0'
yi(   --> Yanks '5.0/9.0'

删除第 n 个外部括号内的文本:

要抓取第n对外括号,只需在上述命令前添加n即可,因此,光标位置与上面相同,

2di(   --> Deletes '(5.0/9.0) * (fahr-32)'
2ci(   --> Substitutes '(5.0/9.0) * (fahr-32)'
2yi(   --> Yanks '(5.0/9.0) * (fahr-32)'

3di(   --> Deletes '"%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32))'
3ci(   --> Substitutes '"%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32))'
3yi(   --> Yanks '"%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32))'

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 of i ("inside") to include the parentheses. So, using da( deletes everything inside ( and ) including ( and ).

Deleting text inside the immediate outer pair of parentheses :

So, for this line of code

printf("%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32)));
                                ^       ^
                                |       |
                                 \_______\___---> Cursor range

assuming that your cursor is inside the above mentioned cursor range, you can issue the following commands :

di(   --> Deletes '5.0/9.0'
ci(   --> Substitutes '5.0/9.0'
yi(   --> Yanks '5.0/9.0'

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,

2di(   --> Deletes '(5.0/9.0) * (fahr-32)'
2ci(   --> Substitutes '(5.0/9.0) * (fahr-32)'
2yi(   --> Yanks '(5.0/9.0) * (fahr-32)'

3di(   --> Deletes '"%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32))'
3ci(   --> Substitutes '"%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32))'
3yi(   --> Yanks '"%3.0f\t%6.1f\n", fahr, ((5.0/9.0) * (fahr-32))'
陌伤ぢ 2024-07-18 19:50:34

您可以使用 d% 进行删除,使用 y% 进行拉取。

You can use d% for deleting and y% for yanking.

甜点 2024-07-18 19:50:34

将光标放在第一个括号上,然后按 v%yv%d

Place your cursor on the first parenthesis, then press v%y or v%d.

丘比特射中我 2024-07-18 19:50:34

尝试 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(

眼睛会笑 2024-07-18 19:50:34

正如 David Norman 的回答所说,

将光标放在第一个括号上,然后按 v%yv%d

来自 http://vimdoc.sourceforge.net/htmldoc/vimindex.html 的解释:

tag                char           note action in Normal mode        
------------------------------------------------------------------------------
|v|                v                   start characterwise Visual mode
|%|                %                1  find the next (curly/square) bracket on
                                       this line and go to its match, or go to
                                       matching comment bracket, or go to matching
|d|                ["x]d{motion}    2  delete Nmove text [into buffer x]

这意味着它将选择两个括号 (%) 之间(包括这两个括号)的所有内容,同时以可视方式向您显示所选内容 (v),然后猛拉/复制 y 或删除/删除它。 (到默认缓冲区。)

您可以使用 p 进行放置/粘贴。

回答了“自学钓鱼”

As answer of David Norman says,

Place your cursor on the first parenthesis, then press v%y or v%d.

Explanation from http://vimdoc.sourceforge.net/htmldoc/vimindex.html:

tag                char           note action in Normal mode        
------------------------------------------------------------------------------
|v|                v                   start characterwise Visual mode
|%|                %                1  find the next (curly/square) bracket on
                                       this line and go to its match, or go to
                                       matching comment bracket, or go to matching
|d|                ["x]d{motion}    2  delete Nmove text [into buffer x]

This means it will select everything between and including the two brackets (%) while showing the selection to you visually (v) and then yank/copy y or delete/cut d it. (To the default buffer.)

You can put/paste with p.

Made this answer to "teach myself to fish".

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