Vim Tips Wiki
(→‎Comments: copy/paste tips)
No edit summary
(10 intermediate revisions by 8 users not shown)
Line 16: Line 16:
 
Copy a word and paste it over other words:
 
Copy a word and paste it over other words:
 
{| class="cleartable"
 
{| class="cleartable"
| style="width:5em" | <tt>yiw</tt> || yank inner word (copy word under cursor, say "''first''").
+
| style="width:5em" | <code>yiw</code> || yank inner word (copy word under cursor, say "''first''").
 
|-
 
|-
 
| ... || Move the cursor to another word (say "''second''").
 
| ... || Move the cursor to another word (say "''second''").
 
|-
 
|-
| <tt>viwp</tt> || select "''second''", then replace it with "''first''".
+
| <code>viwp</code> || select "''second''", then replace it with "''first''".
 
|-
 
|-
 
| ... || Move the cursor to another word (say "''third''").
 
| ... || Move the cursor to another word (say "''third''").
 
|-
 
|-
| <tt>viw"0p</tt> || select "''third''", then replace it with "''first''".
+
| <code>viw"0p</code> || select "''third''", then replace it with "''first''".
 
|}
 
|}
   
 
Copy a line and paste it over other lines:
 
Copy a line and paste it over other lines:
 
{| class="cleartable"
 
{| class="cleartable"
| style="width:5em" | <tt>yy</tt> || yank current line (say "''first line''").
+
| style="width:5em" | <code>yy</code> || yank current line (say "''first line''").
 
|-
 
|-
 
| ... || Move the cursor to another line (say "''second line''").
 
| ... || Move the cursor to another line (say "''second line''").
 
|-
 
|-
| <tt>Vp</tt> || select "''second line''", then replace it with "''first line''".
+
| <code>Vp</code> || select "''second line''", then replace it with "''first line''".
 
|-
 
|-
 
| ... || Move the cursor to another line (say "''third line''").
 
| ... || Move the cursor to another line (say "''third line''").
 
|-
 
|-
| <tt>V"0p</tt> || select "''third line''", then replace it with "''first line''".
+
| <code>V"0p</code> || select "''third line''", then replace it with "''first line''".
 
|}
 
|}
   
Deleting, changing and yanking text copies the affected text to the unnamed register (<tt>""</tt>). Yanking text also copies the text to register 0 (<tt>"0</tt>). So the command <tt>yiw</tt> copies the current word to <tt>""</tt> and to <tt>"0</tt>.
+
Deleting, changing and yanking text copies the affected text to the unnamed register (<code>""</code>). Yanking text also copies the text to register 0 (<code>"0</code>). So the command <code>yiw</code> copies the current word to <code>""</code> and to <code>"0</code>.
   
Typing <tt>viw</tt> selects the current word, then pressing <tt>p</tt> pastes the unnamed register over the visual selection. The visual selection is deleted to the unnamed register.
+
Typing <code>viw</code> selects the current word, then pressing <code>p</code> pastes the unnamed register over the visual selection. The visual selection that was just replaced is then stored in the default unnamed register.
  +
  +
As you can see in the examples above, if you want to paste the same thing a second time, then you must use the <code>"0</code> register, as in <code>viw"0p</code>. A workaround is to remap the <code>p</code> command in visual mode so that it first deletes to the black hole register like so:
  +
<pre>
  +
xnoremap p "_dP
  +
</pre>
  +
Note, using <code>P</code> in the remap replicates the default visual mode <code>p</code> function. Feel free to test. And you'd use <code>xnoremap</code> instead of <code>vnoremap</code>, according to the [http://vimdoc.sourceforge.net/htmldoc/map.html#mapmode-x docs]. Now you don't have to worry about pasting from the <code>"0</code> register each subsequent time.
  +
  +
However, this remapping prevents you from pasting from other registers in visual mode. So <code>viw"ap</code> to paste from the <code>"a</code> register is now broken. [https://code.google.com/p/lh-vim/source/browse/misc/trunk/macros/repl-visual-no-reg-overwrite.vim This script] may solve that. Alternatively, I guess you could use a leader key instead such as:
  +
<pre>
  +
let mapleader=","
  +
xnoremap <leader>p "_dP
  +
</pre>
  +
And then the workflow would be: <code>yiw</code>, move, <code>viw,p</code>, move again, <code>viw,p</code>, etc
  +
  +
== Alternate method ==
  +
  +
An alternate method is to do the paste/replace using <code>cw</code>. This method has the advantage of being easily repeatable using the <code>.</code> repeat command.
  +
  +
{| class="cleartable"
  +
| style="width:5em" | <code>yiw</code> || yank inner word (copy word under cursor, say "''first''". Same as above).
  +
|-
  +
| ... || Move the cursor to another word (say "''second''").
  +
|-
  +
| <code>ciw<C-r>0</code> || select "''second''", then replace it with "''first''" If you are at the start of the word then <code>cw<C-r>0</code> is sufficient.
  +
|-
  +
| ... || Move the cursor to another word (say "''third''").
  +
|-
  +
| <code>.</code> || select "''third''", then replace it with "''first''".
  +
|}
   
 
{{todo}}
 
{{todo}}
 
*Use with clipboard @+
 
*Use with clipboard @+
*Explain: Use register 2 to insert previously deleted text: <tt>diw"2P</tt>
+
*Explain: Use register 2 to insert previously deleted text: <code>diw"2P</code>
   
 
This process works with any selected text, although it works best if:
 
This process works with any selected text, although it works best if:
Line 53: Line 82:
 
*The yanked text is blockwise and the replaced text is selected blockwise.
 
*The yanked text is blockwise and the replaced text is selected blockwise.
   
Typing <tt>yiw</tt> (yank inner word) is an example of a characterwise copy, while <tt>yy</tt> (yank current line) is an example of a linewise copy.
+
Typing <code>yiw</code> (yank inner word) is an example of a characterwise copy, while <code>yy</code> (yank current line) is an example of a linewise copy.
 
If you visually select some text (for example an inner word, <tt>viw</tt>), then press <tt>p</tt>, 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 <tt>P</tt> is exactly the same as pressing <tt>p</tt> (however, when no text is selected, <tt>P</tt> pastes before the cursor, while <tt>p</tt> pastes after the cursor).
 
   
 
==Stamping==
 
==Stamping==
With this mapping, you can press <tt>S</tt> 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 <tt>S</tt>, then move to another word and press <tt>S</tt>, and so on). Each time you press <tt>S</tt>, the yanked text is "stamped" over the current word.
+
With this mapping, you can press <code>S</code> 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 <code>S</code>, then move to another word and press <code>S</code>, and so on). Each time you press <code>S</code>, the yanked text is "stamped" over the current word.
 
<pre>
 
<pre>
 
nnoremap S diw"0P
 
nnoremap S diw"0P
 
</pre>
 
</pre>
   
The <tt>S</tt> command is then not available – use the equivalent <tt>cc</tt> command instead.
+
The <code>S</code> command is then not available – use the equivalent <code>cc</code> command instead.
   
An alternative mapping uses the black hole register (<tt>"_</tt>) for the deletion so the unnamed register is not changed:
+
An alternative mapping uses the black hole register (<code>"_</code>) for the deletion so the unnamed register is not changed:
 
<pre>
 
<pre>
 
nnoremap S "_diwP
 
nnoremap S "_diwP
 
</pre>
 
</pre>
   
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 <tt>x</tt> to delete an unwanted comma), the ability to easily use the previously-deleted text is lost.
+
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 <code>x</code> 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 <tt>S</tt> to make the same change.
+
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 <code>S</code> to make the same change.
 
<pre>
 
<pre>
 
vnoremap S "_d"0P
 
vnoremap S "_d"0P
Line 85: Line 112:
 
For those who use visual mode, here are some commands and mappings that may be useful:
 
For those who use visual mode, here are some commands and mappings that may be useful:
   
Type <tt>gv</tt> to reselect the previous visual area.
+
Type <code>gv</code> to reselect the previous visual area.
   
 
Copy the current word or visually selected text to the clipboard:
 
Copy the current word or visually selected text to the clipboard:
Line 99: Line 126:
 
</pre>
 
</pre>
   
Prepare a <tt>:substitute</tt> command using the current word or the selected text:
+
Prepare a <code>:substitute</code> command using the current word or the selected text:
 
<pre>
 
<pre>
 
nnoremap <F6> yiw:%s/\<<C-r>"\>/<C-r>"/gc<Left><Left><Left>
 
nnoremap <F6> yiw:%s/\<<C-r>"\>/<C-r>"/gc<Left><Left><Left>
Line 121: Line 148:
   
 
==See also==
 
==See also==
  +
*[[Repeat last change]]
  +
  +
==Related Plugins==
 
*{{script|id=2703|text=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
 
*{{script|id=2703|text=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
  +
*{{script|id=1234|text=YankRing}} - Maintains a history of previous yanks, changes and deletes
  +
*[https://github.com/maxbrunsfeld/vim-yankstack yankstack] - It allows you to yank and delete things without worrying about losing the text that you yanked previously. It effectively turns your default register into a stack, and lets you cycle through the items in the stack after doing a paste. This plugin is intended to be a simpler alternative to the YankRing plugin.
  +
*[https://github.com/svermeulen/vim-easyclip vim-easyclip] - This plugin solves that problem by redirecting all <code>c</code>hange and <code>d</code>elete operations to the black hole register and introducing a new operator, 'cut' (by default this is mapped to the <code>m</code> key for 'move').
   
 
==References==
 
==References==
Line 161: Line 194:
 
*[[VimTip1623|1623 Using the Windows clipboard in Cygwin Vim]]
 
*[[VimTip1623|1623 Using the Windows clipboard in Cygwin Vim]]
 
I'm parking the copy/paste list here; later will work out which is "main" copy/paste tip and will move list there. [[User:JohnBeckett|JohnBeckett]] 11:24, September 28, 2009 (UTC)
 
I'm parking the copy/paste list here; later will work out which is "main" copy/paste tip and will move list there. [[User:JohnBeckett|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. --[[User:Fritzophrenic|Fritzophrenic]] 16:27, October 1, 2009 (UTC)
  +
----
  +
I find it completely ridiculous that such a common operation is not more easily accomplished in VIM. Having to use the pinky finger to hit two far characters in <code>"_</code> or <code>"0"</code> is wholly inefficient. [http://vimcasts.org/blog/2013/11/registers-the-good-the-bad-and-the-ugly-parts/ And I am not the only one who is annoyed.] It is absolutely mind boggling that there is no clean way to accomplish this. Instead we have to resort to huge ass webpages describing all the workarounds and idiosyncrasies, and/or install a plugin. Notepad wins this one.
  +
:I have to say I agree this is an annoying quirk of Vim. But note it's there for a reason. Another common need is to move multiple blocks of text around, with one block replacing the next block which then needs to move elsewhere. Vim is optimized for this use case instead of "copy one block then replace a bunch of other blocks with it". Nevertheless, I admit I probably do the latter more often than the former so I do run into this a lot; I've just gotten in the habit of using register 0 when I'm replacing text rather than the unnamed register. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 21:04, July 15, 2014 (UTC)
  +
::I understand the use case for the 'feature'. But that's what named registers are for. I think really the problem is the stupid choice to 'cut' with the <code>d</code> and <code>c</code> commands. The implementation is poor when you have to rely on the pinky finger to type all three keypresses as in <code>"0p</code>. Have you just trained yourself to be super fast with that? Because I cannot do it fast at all.

Revision as of 21:24, 15 July 2014

Tip 605 Printable Monobook Previous Next

created 2003 · complexity basic · author Yang Xiangyu · version 6.0


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.

How to copy/paste

Copy a word and paste it over other words:

yiw yank inner word (copy word under cursor, say "first").
... Move the cursor to another word (say "second").
viwp select "second", then replace it with "first".
... Move the cursor to another word (say "third").
viw"0p select "third", then replace it with "first".

Copy a line and paste it over other lines:

yy yank current line (say "first line").
... Move the cursor to another line (say "second line").
Vp select "second line", then replace it with "first line".
... Move the cursor to another line (say "third line").
V"0p select "third line", then replace it with "first line".

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 that was just replaced is then stored in the default unnamed register.

As you can see in the examples above, if you want to paste the same thing a second time, then you must use the "0 register, as in viw"0p. A workaround is to remap the p command in visual mode so that it first deletes to the black hole register like so:

xnoremap p "_dP

Note, using P in the remap replicates the default visual mode p function. Feel free to test. And you'd use xnoremap instead of vnoremap, according to the docs. Now you don't have to worry about pasting from the "0 register each subsequent time.

However, this remapping prevents you from pasting from other registers in visual mode. So viw"ap to paste from the "a register is now broken. This script may solve that. Alternatively, I guess you could use a leader key instead such as:

let mapleader=","
xnoremap <leader>p "_dP

And then the workflow would be: yiw, move, viw,p, move again, viw,p, etc

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.

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:

nnoremap <F6> yiw:%s/\<<C-r>"\>/<C-r>"/gc<Left><Left><Left>
vnoremap <F6> y:%s/\<<C-r>"\>/<C-r>"/gc<Left><Left><Left>

One key <F7> does everything. Good for inter-window copying and pasting.

"copy
vmap <F7> "+ygv"zy`>
"paste (Shift-F7 to paste after normal cursor, Ctrl-F7 to paste over visual selection)
nmap <F7> "zgP
nmap <S-F7> "zgp
imap <F7> <C-r><C-o>z
vmap <C-F7> "zp`]
cmap <F7> <C-r><C-o>z

"copy register
autocmd FocusGained * let @z=@+

See also

Related Plugins

  • 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
  • YankRing - Maintains a history of previous yanks, changes and deletes
  • yankstack - It allows you to yank and delete things without worrying about losing the text that you yanked previously. It effectively turns your default register into a stack, and lets you cycle through the items in the stack after doing a paste. This plugin is intended to be a simpler alternative to the YankRing plugin.
  • vim-easyclip - This plugin solves that problem by redirecting all change and delete operations to the black hole register and introducing a new operator, 'cut' (by default this is mapped to the m key for 'move').

References

Comments

 TO DO 

  • Prune out any misguided information.
  • That will leave some notes on copying a word (or selected text) and pasting it to replace another word (or selected text).
  • There will also be some notes on visual mode which should be moved elsewhere (possibly 1584).

Related:

I hope some of these can be merged, while others will be "see also".

Copy/paste tips:

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)

I find it completely ridiculous that such a common operation is not more easily accomplished in VIM. Having to use the pinky finger to hit two far characters in "_ or "0" is wholly inefficient. And I am not the only one who is annoyed. It is absolutely mind boggling that there is no clean way to accomplish this. Instead we have to resort to huge ass webpages describing all the workarounds and idiosyncrasies, and/or install a plugin. Notepad wins this one.

I have to say I agree this is an annoying quirk of Vim. But note it's there for a reason. Another common need is to move multiple blocks of text around, with one block replacing the next block which then needs to move elsewhere. Vim is optimized for this use case instead of "copy one block then replace a bunch of other blocks with it". Nevertheless, I admit I probably do the latter more often than the former so I do run into this a lot; I've just gotten in the habit of using register 0 when I'm replacing text rather than the unnamed register. --Fritzophrenic (talk) 21:04, July 15, 2014 (UTC)
I understand the use case for the 'feature'. But that's what named registers are for. I think really the problem is the stupid choice to 'cut' with the d and c commands. The implementation is poor when you have to rely on the pinky finger to type all three keypresses as in "0p. Have you just trained yourself to be super fast with that? Because I cannot do it fast at all.