Vim Tips Wiki
m (Copy, Cut, and Paste moved to Copy, cut and paste: Page moved by JohnBot to improve title)
(Reword.)
(34 intermediate revisions by 21 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=312
 
|id=312
  +
|previous=311
|title=Copy, Cut, and Paste
 
  +
|next=313
|created=August 13, 2002 23:20
+
|created=August 13, 2002
 
|complexity=intermediate
 
|complexity=intermediate
  +
|author=
|author=Anis W. Nugroho (anis_wn--AT--cbn.net.id)
 
|version=5.7
+
|version=6.0
 
|rating=688/270
 
|rating=688/270
  +
|category1=Getting started
|text=
 
  +
|category2=
PS: copy, cut, and paste are the words from (usually) gui editor.
 
 
 
 
Ever try to cut (or copy) some lines and paste to another place?
 
 
If you need to count the lines first, then try these to eliminate counting task.
 
 
 
 
Cut and Paste:
 
 
 
 
1. Place the cursor at the beginning of the block you want to CUT.
 
 
2. Mark it with md
 
 
3. Go to the end of the block.
 
 
4. Cut it with d'd
 
 
5. Go to the new location that you want to PASTE those text.
 
 
6. Press P.
 
 
 
 
Copy and Paste:
 
 
 
 
1. Place the cursor at the beginning of the block you want to COPY.
 
 
2. Mark it with my
 
 
3. Go to the end of the block.
 
 
4. Cut it with y'y
 
 
5. Go to the new location that you want to PASTE those text.
 
 
6. Press P.
 
 
 
 
The name of the mark used is related to the operation (d:delete or y:yank).
 
 
I found that those mark names requires minimal movement of my finger. ;)
 
 
 
 
}}
 
}}
  +
Here is how to cut-and-paste or copy-and-paste text using a visual selection in Vim.
   
 
'''Cut and paste:'''
== Comments ==
 
 
#Position the cursor where you want to begin cutting.
Sorry, step 4 for Copying supposed to be 'Copy...' :(
 
  +
#Press '''v''' to select characters (or uppercase '''V''' to select whole lines).
 
#Move the cursor to the end of what you want to cut.
  +
#Press '''d''' to cut (or '''y''' to copy).
 
#Move to where you would like to paste.
 
#Press '''P''' to paste before the cursor, or '''p''' to paste after.
   
 
'''Copy and paste''' is performed with the same steps except for step 4 where you would press '''y''' instead of '''d''':
anis_wn--AT--cbn.net.id
 
  +
*'''d''' = delete = cut
, August 13, 2002 23:24
 
  +
*'''y''' = yank = copy
----
 
2. Press v to select block
 
4. Cut it with "dd
 
4. Copy it with "yd
 
6. Press P (if you want to paste before) or p (if you want to paste after).
 
   
  +
==Multiple copying==
6. If you want to paste it later press "dp (or "yp for copy) to insert it after.
 
  +
Deleted or copied text is placed in the unnamed register. If wanted, a register can be specified so the text is also copied to the named register. A register is a location in Vim's memory identified with a single letter. A double quote character is used to specify that the next letter typed is the name of a register.
   
  +
For example, you could select the text <code>hello</code> then type <code>"ay</code> to copy "hello" to the <code>a</code> register. Then you could select the text <code>world</code> and type <code>"by</code> to copy "world" to the <code>b</code> register. After moving the cursor to another location, the text could be pasted: type <code>"ap</code> to paste "hello" or <code>"bp</code> to paste "world". These commands paste the text after the cursor. Alternatively, type <code>"aP</code> or <code>"bP</code> to paste before the cursor.
marcin--AT--amiga.pl
 
, August 13, 2002 23:31
 
----
 
Cut and paste:
 
1. Position the cursor where you want to begin cutting.
 
2. Press v (or upper case V if you want to cut whole lines).
 
3. Move the cursor to the end of what you want to cut.
 
4. Press d.
 
5. Move to where you would like to paste.
 
6. Press p to paste after the cursor, or P to paste before.
 
   
  +
===Windows clipboard===
Copy and paste can be performed with the same steps, only pressing y instead of d in step 4.
 
  +
When using Vim under Windows, the clipboard can be accessed with the following:
  +
*In step 4, press Shift+Delete to cut or Ctrl+Insert to copy.
  +
*In step 6, press Shift+Insert to paste.
   
  +
===Different instances===
If you want to copy and paste between editor buffers (but not between instances of vim), or if you want to maintain multiple "clipboards" (copy buffers), you can name your buffer by pressing "x (that's a double quote followed by x, where x is the single letter name you choose for your buffer) before the d in step 4, and again before the p in step 6.
 
 
How copy and paste between two instances of Vim on different Linux consoles?
   
 
After copying text, open a new buffer for a new file:
If you happen to be using gvim for Windows and want to copy or cut into the Windows clipboard, press Ctrl+Insert in step 4 (to copy) or Shift+Delete to cut. To paste from the Windows clipboard, press Shift+Insert.
 
 
:e ~/dummy
   
 
*Paste the text to the new buffer.
rainbrot--AT--snark.com
 
 
*Write the new buffer (:w).
, August 18, 2002 3:12
 
 
*Switch to the previous buffer (:bp) to release *.swp.
----
 
 
*Now switch to the other console.
Hello Guys. How to copy and paste between two instances of Vim on different Linux consoles?
 
 
*Put the cursor at the desired place.
 
*Read the dummy file (:r ~/dummy)
   
  +
==Increasing the buffer size==
dedekind--AT--mail.ru
 
  +
Sometimes you can only copy up to 50 lines. To solve this, increase the buffer limit between multiple files.
, November 16, 2003 1:23
 
  +
<pre>
----
 
  +
:help 'viminfo'
after copying text, open a new buffer for a new file, ie
 
  +
...
:e ~/dummy
 
  +
< Maximum number of lines saved for each register.
 
  +
...
paste the text to the new buffer.
 
  +
:set viminfo?
write the new buffer. (:w)
 
  +
:set viminfo='100,<100,s10,h
switch to the previous buffer (:bp) to release *.swp
 
  +
</pre>
 
now switch to the other console.
 
put the cursor at the desired place.
 
read the dummy file (:r ~/dummy).
 
 
that's it.
 
the question is "why do you want to play in the other console? use multi buffer editing instead."
 
   
anis_wn--AT--cbn.net.id
 
, November 28, 2003 13:41
 
 
----
 
----
  +
See also: [[VimTip356|Quick yank and paste]]}}
How to use windows commands in vim?
 
e.g. copy/paste/selectall, etc -&gt; Ctrl-C, Ctrl-V, Ctrl-A, etc
 
   
 
==Comments==
Thanks
 
Atur
 
 
atur--AT--vigorsoft.com
 
, May 12, 2004 22:37
 
----
 
If you want to simulate the Windows way of Cut/Copy/Paste you could add the following line to your initialization file.
 
 
source $VIMRUNTIME/mswin.vim
 
 
Read :help :behave for other information.
 
 
 
 
'''Anonymous'''
 
, February 22, 2005 19:06
 
----
 
[[VimTip866]]
 
 
--
 
Matous Jan Fialka [ http://vcp.ligatura.org/ ]
 
 
Matous Jan Fialka
 
, March 5, 2005 7:35
 
----
 
Why can I not copy and paste a block of 200 lines? The yank works fine, but the paste only does about 50.
 
 
andrewz--AT--springsrescuemission.org
 
, February 7, 2007 14:41
 
----
 
<!-- parsed by vimtips.py in 0.656598 seconds-->
 

Revision as of 09:59, 24 November 2014

Tip 312 Printable Monobook Previous Next

created August 13, 2002 · complexity intermediate · version 6.0


Here is how to cut-and-paste or copy-and-paste text using a visual selection in Vim.

Cut and paste:

  1. Position the cursor where you want to begin cutting.
  2. Press v to select characters (or uppercase V to select whole lines).
  3. Move the cursor to the end of what you want to cut.
  4. Press d to cut (or y to copy).
  5. Move to where you would like to paste.
  6. Press P to paste before the cursor, or p to paste after.

Copy and paste is performed with the same steps except for step 4 where you would press y instead of d:

  • d = delete = cut
  • y = yank = copy

Multiple copying

Deleted or copied text is placed in the unnamed register. If wanted, a register can be specified so the text is also copied to the named register. A register is a location in Vim's memory identified with a single letter. A double quote character is used to specify that the next letter typed is the name of a register.

For example, you could select the text hello then type "ay to copy "hello" to the a register. Then you could select the text world and type "by to copy "world" to the b register. After moving the cursor to another location, the text could be pasted: type "ap to paste "hello" or "bp to paste "world". These commands paste the text after the cursor. Alternatively, type "aP or "bP to paste before the cursor.

Windows clipboard

When using Vim under Windows, the clipboard can be accessed with the following:

  • In step 4, press Shift+Delete to cut or Ctrl+Insert to copy.
  • In step 6, press Shift+Insert to paste.

Different instances

How copy and paste between two instances of Vim on different Linux consoles?

After copying text, open a new buffer for a new file:

:e ~/dummy
  • Paste the text to the new buffer.
  • Write the new buffer (:w).
  • Switch to the previous buffer (:bp) to release *.swp.
  • Now switch to the other console.
  • Put the cursor at the desired place.
  • Read the dummy file (:r ~/dummy)

Increasing the buffer size

Sometimes you can only copy up to 50 lines. To solve this, increase the buffer limit between multiple files.

:help 'viminfo'
...
<       Maximum number of lines saved for each register.
...
:set viminfo?
:set viminfo='100,<100,s10,h

See also: Quick yank and paste}}

Comments