Paste and replace in Vim

Created: — modified: — tags: vim

How to copy-paste text from one place to another, replacing what's already there


Consider the following situation:
f(good);
...<somewhere below>...
f(bad)
and you want to change it to this:
f(good);
...<somewhere below>...
f(good)

Usual Windows-way would be:
  1. select "good" and copy it to clipboard
  2. select "bad" and paste stuff from clipboard

How to achieve the same in Vim, without using extra "x registers?

Visual mode to rescue!
  1. (Y)ank (copy) "good" text (yib to (Y)ank (I)nside (B)rackets when cursor is in "good" word)
  2. Move the cursor to "bad" word
  3. Select it (in visual mode) and paste stuff (vibp to go (V)isual, select (I)nside (B)rackets, and (P)aste)


Related reading: :help v :help v_p