115

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.

alex
  • 479,566
  • 201
  • 878
  • 984
jwerre
  • 9,179
  • 9
  • 60
  • 69

7 Answers7

134

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.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • 39
    Clever. 'p' to paste, 'gv' to re-select what was originally selected. 'y' to copy it again. – Amjith Aug 23 '11 at 21:17
  • @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. – Benoit Apr 04 '17 at 06:30
  • 1
    @Benoit Hmm, I have 7.4.52, but neither `xnoremap` nor `vnoremap` works – Sibbs Gambling Apr 04 '17 at 14:42
  • 14
    Why isn't this the default behavior?? – Seth Jun 24 '17 at 15:30
  • 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. https://i.imgur.com/Nehm7fL.mp4 – Sbu Dec 21 '20 at 07:06
32

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

dash-tom-bang
  • 17,383
  • 5
  • 46
  • 62
  • 2
    sounds like a place where regex with confirm `:%s/.../.../gc` type commands might be closer to what you want – Milimetric Oct 09 '12 at 19:34
  • @Milimetric +1 didn't know you could confirm like that. –  Aug 20 '14 at 00:05
  • 1
    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`) – skatenerd Apr 22 '15 at 07:56
  • Thanks for great explanation. Now I use `xnoremap p "0p` in my ~/.vimrc – nexayq Feb 03 '18 at 12:01
15

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

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
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.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    Actually the default (here, on Ubuntu) was clipboard=autoselect; still, thanks for the most elegant answer so far. – Tobu Nov 30 '11 at 16:24
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".
jack guan
  • 331
  • 2
  • 12
  • 1
    `ciw0` does not work in my case. `ciw` changes to the insert mode where `` 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) – Bushroot Jan 05 '19 at 15:11
  • @Bushroot `` 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. – insectean Jan 05 '19 at 21:11
  • @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). – Bushroot Jan 06 '19 at 18:59
8

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

toopay
  • 1,635
  • 11
  • 18
0

Add below key mapping, hit Ctrlp to paste.

" Paste Register 0  
map <C-p> "0p
Fisher
  • 376
  • 3
  • 14