Paste multiple times
Asked Answered
E

8

118

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.

Ectogenous answered 23/8, 2011 at 15:58 Comment(0)
U
139

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 can press P (Shift+p) instead of p to paste over the selection without overwriting the clipboard.

Unfolded answered 23/8, 2011 at 16:10 Comment(5)
Clever. 'p' to paste, 'gv' to re-select what was originally selected. 'y' to copy it again.Bonedry
@SibbsGambling, probably using a version prior to Vim 7? Then either upgrade Vim, or use v instead of x (vnoremap). Vim help file version7.txt states xmap and smap appeared in that version.Unfolded
@Unfolded Hmm, I have 7.4.52, but neither xnoremap nor vnoremap worksGerfen
Why isn't this the default behavior??Merrythought
Please note that this remapping has one issue though : if you are replacing a shorter text by a longer one, the second time you paste, it will have stored a shorter text. i.imgur.com/Nehm7fL.mp4Shuck
I
33

"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.

Isochor answered 23/8, 2011 at 17:20 Comment(4)
sounds like a place where regex with confirm :%s/.../.../gc type commands might be closer to what you wantScoop
@Scoop +1 didn't know you could confirm like that.Upswell
Upvoting because macros can make this a non-issue. Just make the edit once, and you can do something like 5@m (for a macro named m)Fret
Thanks for great explanation. Now I use xnoremap p "0p in my ~/.vimrcFantail
C
16

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

Cupola answered 23/8, 2011 at 16:15 Comment(0)
A
14

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.

Automat answered 23/8, 2011 at 16:3 Comment(1)
Actually the default (here, on Ubuntu) was clipboard=autoselect; still, thanks for the most elegant answer so far.Sextain
A
10

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".
Aurignacian answered 24/8, 2017 at 7:29 Comment(3)
ciw<C-r>0 does not work in my case. ciw changes to the insert mode where <C-r> becomes useless. Ist there an easy explanation for this? Could that have to do with the texteditor (I am not using pur VIM but VIM Keyboard bindings in RStudio)Christianism
@Christianism <C-r> is intended to be used in insert mode, as it's how you paste from a register without leaving insert mode—in Vim, that is; I'm not familiar with RStudio but I'd guess that you're right and they haven't implemented this particular key binding.Kennakennan
@Sean H: Yes. They did not implement the key binding. It work fine in VIM and even with VIM bindings in other programmes (e.g. Visual Studio Code).Christianism
V
8

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

Vasya answered 23/8, 2011 at 16:4 Comment(2)
Doesn't work when you want to paste the same line over non-contiguous lines.Sextain
@Sextain but it does answer the title of this question 'how do you paste multiple times'Trichotomy
L
5

Use P instead of p.

p will paste from the unnamed register (") and replace its contents with the deleted text.

P will keep pasting from the unnamed register (") without replacing its contents with the deleted text.

Likeminded answered 3/10, 2023 at 14:51 Comment(1)
This should be the accepted answer nowadays in my opinion. Works out of the box withouth having to remap or anything.Setula
C
2

Add below key mapping, hit Ctrlp to paste.

" Paste Register 0  
map <C-p> "0p
Centistere answered 5/7, 2023 at 9:54 Comment(1)
Simple and effective.Polyurethane

© 2022 - 2024 — McMap. All rights reserved.