YXD
YXD

Reputation: 32521

Is there a good way to copy text within parentheses in Vim?

I want to copy the parameters foo(bar).baz in the following code:

function(foo(bar).baz)

First attempt: Cursor on one of the parentheses, then y%. This gives me the parameters plus a bit extra:

(foo(bar).baz)

Second attempt: Cursor on opening parenthesis. Set a mark ma, jump to end with % then y`a to copy back to the mark. This gives me:

(foo(bar).baz

Setting a mark at the end and going the other way gives me exactly the same. Setting a mark on the f, then typing mah%y`a does give me the foo(bar).baz that I want, but maybe there's something more concise. Is there?

Upvotes: 15

Views: 4205

Answers (3)

hammar
hammar

Reputation: 139930

Use text objects:

yi( (or ya( if you want to include the parenthesis).

You can also use " to work inside quotes, etc. See the link for details, or type :help text-objects in Vim.

Upvotes: 26

Walter
Walter

Reputation: 7991

A slightly shorter alternative to yi( is yib. Similarly yiB is equivalent to yi{ - yanks the contents inside braces.

Personally I usually do vib (visual select the text inside braces) first to make sure that the expected text is selected, followed by a y.

For more text object goodness, see :help text-objects.

Upvotes: 7

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58491

Following should do it

Yank Inner Block

yi(

Upvotes: 3

Related Questions