5

I'd like to change the default (and stupid) behavior of pasting text in visual mode. Here's the current steps and behavior:

  1. Copy any arbitrary text (let's call this "text A")
  2. Select any other text in visual mode (let's call this "text B")
  3. While selected, press "p" to replace "text B" with "text A"
  4. Press "p" again somewhere else. The result is "text B" gets pasted.

I want step 4 to result in "text A".

In other words, I want pasting in visual mode to preserve my copied text so I can continue pasting it, and not overwrite it with whatever got replaced.

Fragsworth
  • 33,919
  • 27
  • 84
  • 97

2 Answers2

6
xnoremap <expr> P '"_d"'.v:register.'P'

(Not my code, found it somewhere before).

I wouldn't say the default is stupid, I've used it to good effect before. Using P here works nice because there is no difference between p and P in visual, so it's nice to make one of them do the non-default thing, while still allowing for the default.

EDIT: It doesn't work all that well if you paste a line-wise yank into a character-wise selection though. Doesn't bother me too much, as that's not a really common situation, but still, a warning, just in case.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324