21

I know how to copy a word, but I seem to overwrite what is in my clipboard because, when I try to copy over a word, it does not seem to be working.

To copy a word, I can use

bye

How to copy over a word?

ib.
  • 27,830
  • 11
  • 80
  • 100
codecompleting
  • 9,251
  • 13
  • 61
  • 102
  • Not sure if exact dupe, but comes close: http://stackoverflow.com/questions/2471175/vim-replace-word-with-contents-of-paste-buffer – Marc B Oct 17 '11 at 17:05
  • https://stackoverflow.com/a/66910215/9384511 – ABN Apr 01 '21 at 19:10

7 Answers7

35

Perhaps you just want to do this:

viwp

which will visually select a new word, and paste over it.

Now, if you don't want to lose your register when doing this, you can also put in your vimrc:

xnoremap p pgvy
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • 2
    `xnoremap p pgvy` works great. `vep` is one keypress shorter than `viwp`, and does the same. – nrz Oct 19 '13 at 08:23
  • visual mode is great, but if you can do without it just as easily, with something like dwP, wouldn't that be better? – zumalifeguard Nov 03 '14 at 19:11
  • @nrz no way "it does the same",`vep` would only work the same way if cursor was at the beginning of the word. `viwp` works anywhere. – poige Apr 09 '22 at 14:48
5

Copy the source word as usual (e.g., with yiw) and use

viw"0p

to paste over the destination word.

Since the p command in Visual mode (see :help v_p) does not alter the numbered register 0 containing the text from the most recent yank command, the same copied word can be pasted over and over again.

ib.
  • 27,830
  • 11
  • 80
  • 100
4

Do this once:

ciw<C-r>0

Then to replace words always using the text you yanked do:

.

You can use search with it like this:

/foo<Cr>

.n.n.n

It works because:

ciw replaces inner word and

<C-r>0 uses the most recently yanked register to replace it, which . then also uses.

Sadly, this does not work if you visually select text you wish to replace and use ..

Note that if you originally used visual selection to select the text to replace and did c<C-r>0 then after that . will replace the same length of characters as was included in the visual selection.

Heikki Naski
  • 2,610
  • 1
  • 21
  • 13
2

When you delete a word it is put in the " register which is the default register for pasting, so when you delete the word you want to replace, it will take the previous word's place in the " register. However, the previous word will be in register 0, the one before in 1 and so on – you can at any time see this by running :reg and see the registers' contents. So, to replace a word you can first copy the first word (yiw), then “change” the word you want to replace (ciw) and then insert from register 0 by hitting ctrl-r, 0. (You could also first delete the word (diw) and then paste from register 0: "0p.

beta
  • 2,380
  • 21
  • 38
1

The easy solution is to do it the other way around: first paste the new word, then delete the old one.

JJJ
  • 32,902
  • 20
  • 89
  • 102
1

You could use register 0, which contains the just-overwritten "clipboard" text. For your example, you could yank the text to paste, put the cursor somewhere in the word "bye", and type

ciw [cut in word; deletes the word under the cursor and goes to insert mode there]

ctrl-r 0 [register 0; paste text from register 0]

You can see what's in all the registers with :disp. As Daenyth says, you can yank into a particular register with "x[del/cut/yank command] and paste with "xp from command mode, or ctrl-r x from insert / replace.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Kevin
  • 53,822
  • 15
  • 101
  • 132
  • I would say that you misunderstood the question. There is no mention of a word “bye”, just the vim normal command “bye” that is equivalent to “yiw” – Benoit Oct 18 '11 at 07:56
  • @Benoit I see the `bye` as a command now. Regardless, my answer answers the correct question and is correct and properly informative. The word to be pasted over is irrelevant. – Kevin Oct 18 '11 at 13:59
0

You can yank into a register by prepending it with "a (where a is the name of the register). See How to use vim registers

b"aye

Community
  • 1
  • 1
Daenyth
  • 35,856
  • 13
  • 85
  • 124