Wikia

Vim Tips Wiki

Changes: Copy, cut and paste

Edit

Back to page

m (Reverted edits by 116.202.129.157 (talk | block) to last version by 209.119.64.116)
(Reword.)
 
(22 intermediate revisions by 15 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=312
 
|id=312
Line 6: Line 5:
 
|created=August 13, 2002
 
|created=August 13, 2002
 
|complexity=intermediate
 
|complexity=intermediate
|author=Anis W. Nugroho
+
|author=
|version=5.7
+
|version=6.0
 
|rating=688/270
 
|rating=688/270
 
|category1=Getting started
 
|category1=Getting started
 
|category2=
 
|category2=
 
}}
 
}}
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.
+
Here is how to cut-and-paste or copy-and-paste text using a visual selection in Vim.
   
Cut and paste:
+
'''Cut and paste:'''
 
#Position the cursor where you want to begin cutting.
 
#Position the cursor where you want to begin cutting.
#Press v (or upper case V if you want to cut whole lines).
+
#Press '''v''' to select characters (or uppercase '''V''' to select whole lines).
 
#Move the cursor to the end of what you want to cut.
 
#Move the cursor to the end of what you want to cut.
#Press d.
+
#Press '''d''' to cut (or '''y''' to copy).
 
#Move to where you would like to paste.
 
#Move to where you would like to paste.
#Press p to paste after the cursor, or P to paste before.
+
#Press '''P''' to paste before the cursor, or '''p''' to paste after.
   
Copy and paste can be performed with the same steps, only pressing y instead of d in step 4.
+
'''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
   
The name of the mark used is related to the operation (d:delete or y:yank).
+
==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.
   
I found that those mark names requires minimal movement of my finger.
+
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.
   
==Comments==
+
===Windows clipboard===
Cut and paste:
+
When using Vim under Windows, the clipboard can be accessed with the following:
#Position the cursor where you want to begin cutting.
+
*In step 4, press Shift+Delete to cut or Ctrl+Insert to copy.
#Press v (or upper case V if you want to cut whole lines).
+
*In step 6, press Shift+Insert to paste.
#Move the cursor to the end of what you want to cut.
 
#Press d.
 
#Move to where you would like to paste.
 
#Press p to paste after the cursor, or P to paste before.
 
   
Copy and paste can be performed with the same steps, only pressing y instead of d in step 4.
+
===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.
 
 
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.
 
 
----
 
 
How copy and paste between two instances of Vim on different Linux consoles?
 
How copy and paste between two instances of Vim on different Linux consoles?
   
Line 39: Line 38:
 
:e ~/dummy
 
:e ~/dummy
   
paste the text to the new buffer<br>
+
*Paste the text to the new buffer.
write the new buffer (:w)<br>
+
*Write the new buffer (:w).
switch to the previous buffer (:bp) to release *.swp<br>
+
*Switch to the previous buffer (:bp) to release *.swp.
now switch to the other console<br>
+
*Now switch to the other console.
put the cursor at the desired place<br>
+
*Put the cursor at the desired place.
read the dummy file (:r ~/dummy)
+
*Read the dummy file (:r ~/dummy)
   
----
+
==Increasing the buffer size==
If you want to simulate the Windows way of Cut/Copy/Paste you could add the following line to your initialization file.
+
Sometimes you can only copy up to 50 lines. To solve this, increase the buffer limit between multiple files.
+
<pre>
source $VIMRUNTIME/mswin.vim
+
:help 'viminfo'
+
...
Read :help :behave for other information.
+
< Maximum number of lines saved for each register.
  +
...
  +
:set viminfo?
  +
:set viminfo='100,<100,s10,h
  +
</pre>
   
 
----
 
----
See [[VimTip356|Quick yank and paste]].
+
See also: [[VimTip356|Quick yank and paste]]}}
   
----
+
==Comments==
Why can I not copy and paste a block of 200 lines? The yank works fine, but the paste only does about 50.
 
 
----
 
same problem with me (50 line limit). I've fixed this before but dont dont remember what the fix was.
 
 
----
 
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
 

Latest revision as of 09:59, November 24, 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 copyingEdit

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 clipboardEdit

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 instancesEdit

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 sizeEdit

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

CommentsEdit

Around Wikia's network

Random Wiki