开发者

How to copy a word and paste it over another word in Vim?

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?


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


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.


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.


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.


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜