7

Something that I find myself doing often is yanking the text between two parenthesis and pasting that over another pair of parenthesis. For example:

foo(int a, int b, int c) bar(int d, int e) 

becomes

foo(int a, int b, int c) bar(int a, int b, int c) 

Is there a quick way in Vim to yank the text from foo and paste it over the text in bar?

5 Answers 5

17

Yank the content of the first pair of parentheses:

yib 

Visually select the content of the second pair of parentheses and put:

vibp 
Sign up to request clarification or add additional context in comments.

1 Comment

There is a Vimcast episode on this topic: Pasting from Visual mode
13

One way would be yi) inside foo's arguments and "_di)P within bar's arguments.

yi) yanks the text inside the parentheses

"_di)P uses the null register to delete the text inside the parentheses and pastes the text, vi)p also works and avoids the null register

The only thing changing is the function name though, so you could also just yank the line and use cw (change word) to change foo to bar.

1 Comment

@DirkHorsten I added brief explanations.
4

Cursor over the first paren of foo, then use y% to yank all the text until the matching paren. (You can also use v%y if you prefer to visually see the text you're yanking.)

Then cursor over the first paren of bar, then use v%p. It selects the text up until the matching paren then pastes over it.

Comments

1

I use vim-scripts/ReplaceWithRegister.

Copy as usual with yi(

Paste with gri(

Comments

1

Use this to go to last parenthesis shift + 5.

Press 5 twice for the first parentheses.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.