Vim: copy, then paste more than once

vim
6,899

Solution 1

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.

Solution 2

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.

Solution 3

Luckily for you and me, this question has been asked and answered on StackOverFlow. You can alter the default behaviour of p to be more intuitive or handy for some, by adding the following to your vimrc file:

xnoremap p pgvy

Solution 4

You might want to consider "0p when pasting in visual mode.

Ref :help v_p

Move to L in Linux
ye     # Yank to end of word.
w      # Move to next word.
<C>ve  # Highlight Solaris.
"0p    # Paste Linux, Solaris ends up in unnamed register and Linux in 0
w      # Move to next word.
<C>ve  # Mark next word. (Or anywhere else)
"0p    # Paste Linux.

Or in this exact example one could also:

Positioned on L in Linux

yww<C>v2f 2p
# Or
yww<C>v2el2p
yw           # Yank Linux 
w            # Move to S in Solaris
<C>v2el      # Enter visual mode, highlight two * end, and l to consume space.
2p           # Paste Linux twice.

'<C>v2f '    # Alternative to <C>v2el

Solution 5

Use registers and avoid visual mode.

Move to 'L' (type 0fL)                  |L|inux Solaris Irix HP-UX
"lye                                    
'Linux' is now in the 'l' register.     
Move to 'S' (type fS)                   Linux |S|olaris Irix HP-UX
"sde"lP                                 Linux Linu|x| Irix HP-UX
'Solaris' is now in the 's' register.   
Move to 'I' (type fI)                   Linux Linux |I|rix HP-UX
"ide"lP                                 Linux Linux Linu|x| HP-UX

You could also just delete 'Solaris ' and 'Irix ' and paste 'Linux ' twice in their place.

0fLw2dwbyf<space>w2P
Share:
6,899
kevinarpe
Author by

kevinarpe

LinkedIn Profile: https://hk.linkedin.com/pub/kevin-arpe/12/4a4/277 My open source libraries: Papaya for Java: https://github.com/kevinarpe/kevinarpe-papaya Rambutan for Python3: https://github.com/kevinarpe/kevinarpe-rambutan3 I specialise in answering older -- perhaps forgotten -- questions, and making corrections. Best answer: http://stackoverflow.com/questions/13331989/how-to-handle-multipart-alternative-mail-with-javamail/16931800#16931800

Updated on September 18, 2022

Comments

  • kevinarpe
    kevinarpe about 1 year

    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?