A common requirement, particularly when programming, is to copy or delete text from one location, and use what was copied or deleted to replace text in one or more other locations. This tip describes how this operation is performed using standard Vim techniques, and with some tricks that may make it easier.

Deleting, changing and yanking text copies the affected text to the unnamed register (""). Yanking text also copies the text to register 0 ("0). So the command yiw copies the current word to "" and to "0.

Typing viw selects the current word, then pressing p pastes the unnamed register over the visual selection. The visual selection is deleted to the unnamed register.

Alternate method

An alternate method is to do the paste/replace using cw. This method has the advantage of being easily repeatable using the . repeat command.

yiw

yank inner word (copy word under cursor, say "first". Same as above).

...

Move the cursor to another word (say "second").

ciw<C-r>0

select "second", then replace it with "first" If you are at the start of the word then cw<C-r>0 is sufficient.

...

Move the cursor to another word (say "third").

.

select "third", then replace it with "first".

TO DO

Use with clipboard @+

Explain: Use register 2 to insert previously deleted text: diw"2P

This process works with any selected text, although it works best if:

The yanked text is characterwise and the replaced text is selected characterwise; or

The yanked text is linewise and the replaced text is selected linewise; or

The yanked text is blockwise and the replaced text is selected blockwise.

Typing yiw (yank inner word) is an example of a characterwise copy, while yy (yank current line) is an example of a linewise copy.

If you visually select some text (for example an inner word, viw), then press p, the selected text is replaced with the contents of the unnamed register; the replaced text is placed in the unnamed register. When text has been selected, pressing P is exactly the same as pressing p (however, when no text is selected, P pastes before the cursor, while p pastes after the cursor).

Stamping

With this mapping, you can press S to replace the current word with the last yanked text. You can repeat the operation to replace words at different locations (move to another word and press S, then move to another word and press S, and so on). Each time you press S, the yanked text is "stamped" over the current word.

nnoremap S diw"0P

The S command is then not available – use the equivalent cc command instead.

An alternative mapping uses the black hole register ("_) for the deletion so the unnamed register is not changed:

nnoremap S "_diwP

The alternative replaces the current word with the last text that was yanked or deleted. This means that you can copy or delete text from one location, then use that text to replace words in multiple other locations. However, if you delete any more text (for example, if you press x to delete an unwanted comma), the ability to easily use the previously-deleted text is lost.

In addition to the above, you could use the following to replace visually selected text with the last yanked text. Again, you can quickly repeat this by selecting some more text and pressing S to make the same change.

vnoremap S "_d"0P

If you want to replace visually selected text with the last yanked or deleted text, use:

vnoremap S "_dP

From tip 1011

For those who use visual mode, here are some commands and mappings that may be useful:

Type gv to reselect the previous visual area.

Copy the current word or visually selected text to the clipboard:

nnoremap <F4> "+yiw
vnoremap <F4> "+y

Replace the current word or visually selected text with the clipboard contents:

nnoremap <F5> viw"+p
vnoremap <F5> "+p

Prepare a :substitute command using the current word or the selected text:

See also

ReplaceWithRegister plugin to replace text covered by a motion, entire line(s) or the current selection with the contents of a register; more versatile and covers corner cases better than simple mappings

I'm parking the copy/paste list here; later will work out which is "main" copy/paste tip and will move list there. JohnBeckett 11:24, September 28, 2009 (UTC)

I'd like for this tip to remain about copy/paste as it relates to visual mode, and we should have at least one other (separate) tip on the system clipboard (maybe more than one if specific systems have problems). One more that is a general "how to copy/paste" that also discusses use of registers would be good too. I don't want to see too much combination though, or the tip will probably become too big. --Fritzophrenic 16:27, October 1, 2009 (UTC)