Can you grab or delete between parentheses in vi/vim?

EditorVim

Editor Problem Overview


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?

Editor Solutions


Solution 1 - Editor

What about dib or di(.

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

I love text-object motions and selections!

Solution 2 - Editor

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.

Solution 3 - Editor

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))'

Solution 4 - Editor

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

Solution 5 - Editor

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

Solution 6 - Editor

Try ci[block-surrounder]

In your case, place the cursor anywhere between the 2 parenthesis that you highlighed and try the keys: ci(

Solution 7 - Editor

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".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionromandasView Question on Stackoverflow
Solution 1 - EditorChristian C. SalvadóView Answer on Stackoverflow
Solution 2 - Editoruser3850View Answer on Stackoverflow
Solution 3 - EditorKevinView Answer on Stackoverflow
Solution 4 - Editorahy1View Answer on Stackoverflow
Solution 5 - EditorDavid NormanView Answer on Stackoverflow
Solution 6 - EditorJustin NguyenView Answer on Stackoverflow
Solution 7 - Editorn611x007View Answer on Stackoverflow