Can you capture or delete between parentheses in vi / 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 remove or wrest from the first bold parenthesis in the appropriate bracket? I was thinking about df) , but that will only give you right after 9.0.

Is there a similar way to force vim to grab everything between the corresponding curly braces, regardless of newlines?

+89
vim editor
Jan 01 '09 at 20:13
source share
7 answers

Various movements:%

The % command proceeds to match the item under the cursor. Place the cursor on the opening (or closing) groove and use y% for yanking or d% to remove everything from the cursor to the corresponding partner.

This works because % is a "move command", so it can be used wherever vim expects such a command. From :help y :

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

By default, an “element” contains brackets, braces, parsers, C-style comments, and various precompilers ( #ifdef , etc.).

There is a plugin for “advanced% match” that you can find on the Vim homepage .

You can read the documentation for % and related motion commands by typing :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 should do the following:

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

Say your cursor is located in ^ . Enter the following sequence to select the part you need:

 v2a) 

The first v goes into visual mode, then you indicate that you want to go 2 levels of parens up. Finally, a) selects a "block". After that, you can use d or x to delete, etc.

If you do not want to include external parens, you can use the "internal block" instead:

 v2i) 

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

+134
Jan 01 '09 at 20:25
source share

How about dib or di( .

It will remove the inner (...) block where the cursor is located.

I love text objects , movements and choices!

+151
Jan 01 '09 at 20:36
source share

To remove everything that is inside a pair of parentheses, you can always give di( and its derivatives.

Note:

As @porglezomb explained in his comment, you can use a ("along with") instead of i ("inside") to include parentheses. Thus, using da( removes everything inside ( and ) including ( and ) .

Removing text inside an 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 your cursor is inside the specified cursor range, you can run the following commands:

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

Removing text inside the nth outer pair of parentheses:

To capture everything inside the nth outer pair of parentheses, just add n before the specified 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))' 
+19
Jul 27 '14 at
source share

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

+15
Jan 01 '09 at 20:17
source share

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

+10
Jan 01 '09 at 20:15
source share

Try ci [block-surrounder]

In your case, place the cursor anywhere between the two brackets that you are top secret and try the keys: ci (

+1
Jan 15 '14 at 9:55
source share

As David Norman's answer says,

Hover over the first bracket, 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 that it will select everything between the two brackets ( % ) and show a visual selection ( v ), then yank / copy y or delete / cut d . (To the default buffer.)

You can put / paste with p .

Did this answer "teach yourself how to fish . "

0
Apr 01 '15 at 14:34
source share



All Articles