tags:

views:

1447

answers:

5

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?

A: 

How about 3df)? This way you have to count how many closing parens you want to delete, though.

unbeknown
And, because of the counting, the % operator (motion) is the better choice.
Jonathan Leffler
+2  A: 

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

David Norman
Great! % jumps to the matching paren. So you can even use d% and y%.
unbeknown
While correct, this answer would be nice if it explained the _why_...
hop
What is the purpose of the 'v'? I've always used 'y%' or 'd%'. Also, in classic vi, 'v' is one of the few unused letters. (I'll guess: v% is a visual highlight, and then the d/y operates on that scope?)
Jonathan Leffler
OK - tests confirm v% highlights the range. Useful, but not always necessary (thanks to the undo). Live and learn.
Jonathan Leffler
Note, too, that 'd%' can start at the beginning of a function name, and will delete the entire function call. It also works for '{}', '[]' and even '<>' pairs.
Jonathan Leffler
@Leffler, Thanks for the tips! And it's good to know % works in visual mode as well.
strager
+5  A: 

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

ahy1
While correct, this answer would be nice if it explained the _why_...
hop
% is the movement specifier for "matched parenthesis, bracket, or brace". Any movement specification can be paired with d and y (and many other things).
chaos
@hop, You asked for an explanation (in essence). Someone interested in why could possibly open the comments and read chaos's explanation.
strager
i did not ask for anything, i suggested that the answer could be made more useful with an explanation.
hop
+30  A: 

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.

hop
Thanks! I knew about % switching between matching items; didn't know it was useful within commands as well. +1 and accepted.
romandas
I second the use of text-objects.
graywh
+13  A: 

What about dib or di(.

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

I love text-object motions and selections!

CMS
Luckily, this question is the first hit when I google for it – I don’t know how many times this answer has saved me. And I always forget it … :-(
Konrad Rudolph