Vim: copy, then paste more than once

vim

I use the highlight mode in vim to copy a few characters. I then want to paste more than once. My current technique does not work well.

Sample text: Linux Solaris Irix HP-UX

Suppose I want to copy the word Linux, then paste over Solaris and Irix.

  1. Place cursor at L in Linux
  2. Command v (for visual hilite), then e (for end-of-word), then y (for yank/copy)
  3. Now Linux is on my "vim clipboard"
  4. Move cursor to S in Solaris (first instance)
  5. Command v (for visual hilite), then e (for end-of-word), then p (for paste)
  6. Text is now: Linux Linux Irix HP-UX, but now Solaris is on my "vim clipboard"
  7. Move cursor to I in Irix (second instance)
  8. Command v (for visual hilite), then e (for end-of-word), then p (for paste)
  9. Text is now: Linux Linux Solaris HP-UX which is not what I expected.

I resort to using highlite/paste with the mouse (via X Terminal). Surely, I can do this better. How?

Best Answer

I would do that in this way (really useful for many paste):

  1. Go somewhere into the word Linux, then "ayiw to copy the word
    • "a to select register «a»
    • y for copying
    • i to specify we are "in" (the word, the paragraph, ...)
    • w to choose the word
  2. Got to next word w (or somewhere into the word)
  3. Paste on time and save that as macro qbdiw"aPq
    • qb to start recording macro in register «b»
    • d for deleting
    • i to specify we are "in" (the word, the paragraph, ...)
    • w to choose the word
    • "a to select the register «a» (previously saved)
    • P to paste the word before the cursor
    • q to stop recording the macro
  4. Then to use the macro the first time, go to the next word w and press @b
  5. Finally, and it is where the advantage of this method can be seen, go the each word you want to replace and press @@

Hint: Replace the w by W in qbdiw"aPq to select word with punctuation, like HP-UX

Issue: When the word is the last in the line it will delete the space before the word.

Related Question