Shane Stillwell
Shane Stillwell

Reputation: 3248

Vim replace inside quotes

Many times I find that I will will want to yank the content between quotes and paste them inside another set up of quotes. For example, take this code for instance.

var foo = 'bar',
    baz = 'buz';

I would normally do a yi' inside of 'bar' to yank the word bar.

How do I replace buz with my yank? I know one option is to do a di'"0P, I just wonder if there is an easier solution I'm overlooking.

Upvotes: 11

Views: 3906

Answers (3)

Tung Cheng
Tung Cheng

Reputation: 13

also note after you replace the content in visual mode,the content being replaced will now in you register. command :reg will show it. In your example, command p will paste the buz in the editor.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172510

I need this so often, I wrote a plugin to simplify and allow maximum speed: ReplaceWithRegister.

This plugin offers a two-in-one gr command that replaces text covered by a {motion} / text object, entire line(s) or the current selection with the contents of a register; the old text is deleted into the black-hole register, i.e. it's gone. It transparently handles many corner cases and allows for a quick repeat via the standard . command. Should you not like it, its page has links to alternatives.

Upvotes: 2

Mark Rushakoff
Mark Rushakoff

Reputation: 258138

With your cursor anywhere on the word buz, vi'p to visually select inside the quotes and then put the contents of the most recent yank.

Upvotes: 29

Related Questions