Vim Tips Wiki
Register
No edit summary
m (Reverted edits by 61.135.152.211 (talk | block) to last version by JohnBeckett)
(33 intermediate revisions by 8 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
 
|id=605
{{Tip
 
  +
|previous=604
|id=1011
 
  +
|next=606
|title=mappings and commands for visual mode __PLUS__ some bonus mappings to quickly copy and paste
 
|created=October 6, 2005 8:31
+
|created=2003
 
|complexity=basic
 
|complexity=basic
|author=Jean-Christophe Clavier
+
|author=Yang Xiangyu
|version=5.7
+
|version=6.0
|rating=0/2
+
|rating=28/12
  +
|category1=
|text=
 
  +
|category2=
For those who use Visual mode, here are some commands and mapping that may be
 
 
}}
  +
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==
useful :
 
  +
Copy a word and paste it over other words:
  +
{| class="cleartable"
  +
| style="width:5em" | <code>yiw</code> || yank inner word (copy word under cursor, say "''first''").
  +
|-
  +
| ... || Move the cursor to another word (say "''second''").
  +
|-
  +
| <code>viwp</code> || select "''second''", then replace it with "''first''".
  +
|-
  +
| ... || Move the cursor to another word (say "''third''").
  +
|-
  +
| <code>viw"0p</code> || select "''third''", then replace it with "''first''".
  +
|}
   
  +
Copy a line and paste it over other lines:
First of all 'gv' reselect the previous visual area.
 
  +
{| class="cleartable"
  +
| style="width:5em" | <code>yy</code> || yank current line (say "''first line''").
  +
|-
  +
| ... || Move the cursor to another line (say "''second line''").
  +
|-
  +
| <code>Vp</code> || select "''second line''", then replace it with "''first line''".
  +
|-
  +
| ... || Move the cursor to another line (say "''third 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 (<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 <code>viw</code> selects the current word, then pressing <code>p</code> pastes the unnamed register over the visual selection. The visual selection is deleted to the unnamed register.
   
  +
== Alternate method ==
prepare a :command on the previous visual selected area ('&lt;,'&gt; is not very fast to type...)
 
   
  +
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.
map &lt;M-:&gt; :'&lt;,'&gt;
 
   
  +
{| 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}}
  +
*Use with clipboard @+
  +
*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:
copy the word under cursor in the clipboard
 
  +
*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 <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.
nnoremap &lt;F4&gt; "+yiw
 
   
  +
If you visually select some text (for example an inner word, <code>viw</code>), then press <code>p</code>, 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 <code>P</code> is exactly the same as pressing <code>p</code> (however, when no text is selected, <code>P</code> pastes before the cursor, while <code>p</code> pastes after the cursor).
copy the visual selected area in the clipboard (very easy)
 
   
  +
==Stamping==
vnoremap &lt;F4&gt; "+y
 
  +
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>
 
nnoremap S diw"0P
  +
</pre>
   
  +
The <code>S</code> command is then not available – use the equivalent <code>cc</code> command instead.
replace the word under cursor with the content of the clipboard
 
   
  +
An alternative mapping uses the black hole register (<code>"_</code>) for the deletion so the unnamed register is not changed:
nnoremap &lt;F5&gt; viw"+p
 
  +
<pre>
  +
nnoremap S "_diwP
  +
</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 <code>x</code> to delete an unwanted comma), the ability to easily use the previously-deleted text is lost.
replace the visual selected area with the content of the clipboard
 
   
  +
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.
nnoremap &lt;F5&gt; "+p
 
  +
<pre>
  +
vnoremap S "_d"0P
  +
</pre>
   
  +
If you want to replace visually selected text with the last yanked or deleted text, use:
  +
<pre>
  +
vnoremap S "_dP
  +
</pre>
   
  +
==From tip 1011==
 
For those who use visual mode, here are some commands and mappings that may be useful:
   
 
Type <code>gv</code> to reselect the previous visual area.
prepare the :command to replace the word under cursor
 
   
  +
Copy the current word or visually selected text to the clipboard:
nnoremap &lt;S-F4&gt; "+yiw:%s/\&lt;&lt;C-r&gt;+\&gt;/&lt;C-r&gt;+/gc&lt;LEFT&gt;&lt;LEFT&gt;&lt;LEFT&gt;
 
  +
<pre>
 
nnoremap <F4> "+yiw
 
vnoremap <F4> "+y
  +
</pre>
   
prepare the :command to replace the selected area
+
Replace the current word or visually selected text with the clipboard contents:
  +
<pre>
 
nnoremap <F5> viw"+p
 
vnoremap <F5> "+p
  +
</pre>
   
  +
Prepare a <code>:substitute</code> command using the current word or the selected text:
vnoremap &lt;S-F4&gt; "+y:%s/\&lt;&lt;C-r&gt;+\&gt;/&lt;C-r&gt;+/gc&lt;LEFT&gt;&lt;LEFT&gt;&lt;LEFT&gt;
 
  +
<pre>
  +
nnoremap <F6> yiw:%s/\<<C-r>"\>/<C-r>"/gc<Left><Left><Left>
  +
vnoremap <F6> y:%s/\<<C-r>"\>/<C-r>"/gc<Left><Left><Left>
  +
</pre>
   
 
One key <F7> does everything. Good for inter-window copying and pasting.
  +
<pre>
 
"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=@+
  +
</pre>
   
  +
==See also==
I use the clipboard but you can use whatever register you want ;-)
 
  +
*[[Repeat last change]]
 
  +
*{{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
 
 
puts "" around the word under cursor
 
 
nnoremap &lt;M-"&gt; ciw"&lt;C-r&gt;+"&lt;esc&gt;
 
 
puts "" around the visual selected area
 
 
vnoremap &lt;M-"&gt; c"&lt;C-r&gt;+"&lt;esc&gt;
 
 
 
 
Happy vimming ;-)
 
}}
 
 
== Comments ==
 
Sorry, read
 
vnoremap &lt;F5&gt; "+p
 
instead of
 
nnoremap &lt;F5&gt; "+p
 
   
  +
==References==
'''Anonymous'''
 
  +
*{{help|registers}}
, October 6, 2005 8:35
 
  +
*{{help|v_p}}
----
 
One key &lt;F7&gt; does everything. Good for inter-window copying and pasting.
 
   
 
==Comments==
"copy
 
  +
{{todo}}
vmap &lt;F7&gt; "+ygv"zy`&gt;
 
  +
*Prune out any misguided information.
"paste (Shift-F7 to paste after normal cursor, Ctrl-F7 to paste over visual selection)
 
  +
*That will leave some notes on copying a word (or selected text) and pasting it to replace another word (or selected text).
nmap &lt;F7&gt; "zgP
 
  +
*There will also be some notes on visual mode which should be moved elsewhere (possibly 1584).
nmap &lt;S-F7&gt; "zgp
 
imap &lt;F7&gt; &lt;C-r&gt;&lt;C-o&gt;z
 
vmap &lt;C-F7&gt; "zp`]
 
cmap &lt;F7&gt; &lt;C-r&gt;&lt;C-o&gt;z
 
   
  +
Related:
"copy register
 
  +
*[[VimTip47|47 Swapping characters, words and lines]]
autocmd FocusGained * let --AT--z=--AT--+
 
  +
*[[VimTip191|191 Transposing]]
  +
*[[VimTip438|438 Search and replace in a visual selection]]
  +
*[[VimTip646|646 Moving lines up or down in a file]]
  +
*[[VimTip1584|1584 Visual selection]]
  +
I hope some of these can be merged, while others will be "see also".
   
  +
Copy/paste tips:
Gerald Lai
 
  +
*[[VimTip66|66 Transfer text between two Vim instances]]
, November 19, 2005 3:25
 
  +
*[[VimTip71|71 Transfer text between two gvim sessions using clipboard]]
----
 
  +
*[[VimTip261|261 Close windows from gvim popup menu]]
<!-- parsed by vimtips.py in 0.604913 seconds-->
 
  +
*[[VimTip312|312 Copy, cut and paste]]
  +
*[[VimTip344|344 Cut or copy lines without counting the lines]]
  +
*[[VimTip356|356 Quick yank and paste]]
  +
*[[VimTip386|386 Cut/copy and paste using visual selection]]
  +
*[[VimTip432|432 Putting the current file on the Windows clipboard]]
  +
*[[VimTip471|471 Using the mouse for Vim in an xterm]]
  +
*[[VimTip478|478 Copy the search results into clipboard]]
  +
*[[VimTip829|829 Copy and paste between Vim instances]]
  +
*[[VimTip834|834 Word-wise cut, copy and paste]]
  +
*[[VimTip964|964 GNU/Linux clipboard copy/paste with xclip]]
  +
*[[VimTip984|984 Accessing the system clipboard]]
  +
*[[VimTip1199|1199 Unconditional linewise or characterwise paste]]
  +
*[[VimTip1268|1268 Copy and paste between sessions using a temporary file]]
  +
*[[VimTip1593|1593 Smart paste]]
  +
*[[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'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)

Revision as of 15:18, 25 June 2013

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 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:

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

  • Repeat last change
  • 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

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)