Paste multiple times

VimCopyPaste

Vim Problem Overview


What is the best way replace multiple lines with the contents of the clipboard?

The problem I'm having is when I yank a line and paste it over another line the "yank" is replaced with the line I just replace. Now, if I want to replace another line with the same line I have to go back up and yank it again.

There's got to be a better way to do this.

Vim Solutions


Solution 1 - Vim

I have this in my .vimrc:

xnoremap p pgvy

(note: this will work only with the default register, but this mapping is easy to remember). Writing a more elaborate version would be possible. Also, you still can use P to get the old behaviour.

Solution 2 - Vim

"0 should have the contents of your yank. It's a bit more tedious to type, but "0p should do what you want.

Alternatively, don't select-and-replace the old lines up front. If you find those lines with a search, just hit n. over and over (after an initial p), then when they're all pasted, do ndd followed by as many n.s as necessary.

The biggest mental switch I've needed to make when moving to Vim is to figure out how to apply group edits sequentially. I.e. rather than doing a bunch of edits on a line and then doing a bunch of the same edits on another line, I'll do the first edit on a bunch of lines (using . to great effect), then the second edit on a bunch of lines, etc. Alternatively, the use of macros may help as they are fantastic, but sometimes a little more tedious to get working correctly with "complex" changes.

Solution 3 - Vim

I often use another registry, copy the line you need to some named registry "ay and then paste from there "ap

Solution 4 - Vim

When you paste over a selection in Vim it will replace the default register with the contents of the selection. If pasting over a selection is wiping out the contents of the clipboard register then very likely you have the following line in your .vimrc

set clipboard=unnamed

One option is to remove that and use the explicit clipboard register "+

Another option is to use any of the other explicitly named registers (a-z). After the first paste yank the line back into "c for example and then use "cp to paste from there on out.

Solution 5 - Vim

use np where n is the number of how much time you want to paste the lines eg 3p will paste 3 lines.

Solution 6 - Vim

Instead of using copy/paste, it is often better to use a text object command such as ciw to change the inner word. This method has the advantage of being easily repeatable using the . repeat command.

  1. yiw Yank inner word (copy word under cursor, say "first").
  2. ... Move the cursor to another word (say "second").
  3. ciw<C-r>0 Change "second", replacing it with "first" ( is Ctrl-R).
  4. ... Move the cursor to another word (say "third").
  5. . Change "third", replacing it with "first".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjwerreView Question on Stackoverflow
Solution 1 - VimBenoitView Answer on Stackoverflow
Solution 2 - Vimdash-tom-bangView Answer on Stackoverflow
Solution 3 - VimFacundo CascoView Answer on Stackoverflow
Solution 4 - VimJaredParView Answer on Stackoverflow
Solution 5 - VimtoopayView Answer on Stackoverflow
Solution 6 - Vimjack guanView Answer on Stackoverflow