2

Quite often I copy or remove content inside brackets d% or y%.

MyAwesomeClass(
   ///content
)

But this way I get:

(
   ///content
)

Is there a way to copy word in front of the bracket too? So that the copied result is:

MyAwesomeClass(
   ///content
)
1
  • I thought up a non-plugin solution to your problem (added to the answer)
    – mattb
    Sep 22, 2022 at 12:02

2 Answers 2

4

Vim only solution

The simplest thing for the example you posted is probably to use visual linewise mode - va)Vwill first select the content and the surrounding parenthesis (but not the word in front) and the final V will switch to linewise mode (thus additionally selecting everything before/after the parentheses).

The above would not work in the more general case where you want to select the inner function call (but not the outer one):

outer(inner(
   ///content
), some, more, args, here)

This case would need vim to have an understanding of what constitutes a function call...

Vim plugin solution (text objects)

I wrote the vim-surround-funk plugin to do this kind of thing (and related things - see the end). In your case, the plugin gives you a new text object if and af ('i/a function call'), so you could use either vify to visually select and then yank it, or just yif to yank it directly:

Here are some visual selections of the text objects. Capitalising (i.e. iF and aF) extends the characters considered as legal function name characters (e.g. the . in python, but you can configure this). There is also the in and an text objects (and their capital versions) that select the function call name. N.B. In the below, the cursor can be in a variety of positions (but the cursor moves after the selection, and I can't post the gif from the repo...):

enter image description here

But wait there's more!

The plugin was inspired by tpope's vim-surround plugin and allows you to delete, change and yank a surrounding function call along with its additional arguments. With the surrounding function call in the unnamed register, you can 'grip' any text object with it (including a different function call). 'Gripping' will wrap/encompass a word or function call with the one you have in the unnamed register.

1

This should work in most cases: position your cursor on the content before the opening parentheses. That is, make sure the cursor is at the beginning of whatever you want to operate one.

Then <operator>% (e.g., d%), just like you mentioned. Since % will jump to the closing parenthesis corresponding to the opening one ahead of the cursor, moving over the original content, you'll get the whole thing. That is, starting on a in abc(def), % moves to ) even though you didn't start on (. So similarly d% from that position deletes all of abc(def).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.