William Pursell
William Pursell

Reputation: 212298

How does one yank a function call in vim?

Suppose a line of text reads:

$x = ( frobnicate( foo( bar( $x, $y )[ 1 ]))[ 1 ]);

The cursor is on the 'f' of 'frobnicate' and I want to yank the text which includes the call to frobnicate. (That is, everything up to the 3rd closing parenthesis. I can certainly do:

y3f)

or do it interactively with

vf);;y

but neither of these is appealing. (I don't want to have to count the '3' manually, nor do the repeated find until I hit the end point.) Is there an easy way to accomplish the move from 'f' to the matching closing paren? I'm thinking something like the v_i 'inner word' motion command.

Upvotes: 6

Views: 955

Answers (2)

Xavier T.
Xavier T.

Reputation: 42238

You could try yel% to position the cursor to the opening parentheses and then jump to the closing one with %, but it does not look much more efficient than your current solution.

The only advantage is that you don't have to count anything, and it works with nested call.

Edit : as pointed by Peter, y% works and is a superior solution (even if I don't quite understand why it works. Vim is amazing!)

Upvotes: 0

heijp06
heijp06

Reputation: 11798

y% seems to work.

From help %:

Find the next item in this line after or under the cursor and jump to its match. inclusive motion. Items can be: ([{}]) parenthesis or (curly/square) brackets

Upvotes: 12

Related Questions