How to copy a word and paste it over another word in Vim? [duplicate]
Asked Answered
I

8

25

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?

Interrex answered 17/10, 2011 at 17:2 Comment(1)
T
36

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
Transgress answered 17/10, 2011 at 17:39 Comment(3)
xnoremap p pgvy works great. vep is one keypress shorter than viwp, and does the same.Insnare
visual mode is great, but if you can do without it just as easily, with something like dwP, wouldn't that be better?Countershading
@Insnare 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.Trochaic
S
6

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.

Spew answered 18/10, 2011 at 6:6 Comment(0)
O
5

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.

Oscan answered 19/10, 2011 at 18:31 Comment(0)
A
3

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.

Afroasian answered 17/10, 2011 at 17:16 Comment(2)
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”Transgress
@Transgress 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.Afroasian
S
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.

Spindlelegs answered 17/10, 2011 at 18:39 Comment(0)
S
1

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

Stutzman answered 17/10, 2011 at 17:4 Comment(1)
but I want to be able to paste it multiple times.Interrex
Y
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

Yuen answered 17/10, 2011 at 17:6 Comment(0)
V
0

yiw - Copies a word
ciw - Cuts a word

Then take the cursor wherever you want to paste and press p

Vino answered 14/6 at 0:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.