Vim Tips Wiki
Tag: Visual edit
(Undo revision 38240 by 85.10.248.24 (talk))
Tag: sourceedit
(8 intermediate revisions by 6 users not shown)
Line 5: 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=
 
}}
 
}}
  +
Here is how to cut-and-paste or copy-and-paste text using a visual selection in Vim.
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:
+
'''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''' to cut or '''y''' to copy.
+
#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 before the cursor, or '''p''' to paste after.
 
#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
   
  +
==Multiple copying==
The name of the mark used is related to the operation (d:delete or y:yank).
 
  +
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.
I found that those mark names requires minimal movement of my finger.
 
 
==Copying and pasting outside the editor buffer==
 
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.
 
   
 
===Windows clipboard===
 
===Windows clipboard===
  +
When using Vim under Windows, the clipboard can be accessed with the following:
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.
 
  +
*In step 4, press Shift+Delete to cut or Ctrl+Insert to copy.
 
  +
*In step 6, press Shift+Insert to paste.
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.
 
   
 
===Different instances===
 
===Different instances===
How copy and paste between two instances of Vim on different Linux consoles?
+
How to copy and paste between two instances of Vim on different Linux consoles?
   
 
After copying text, open a new buffer for a new file:
 
After copying text, open a new buffer for a new file:
 
: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==
 
==Increasing the buffer size==
 
Sometimes you can only copy up to 50 lines. To solve this, increase the buffer limit between multiple files.
 
Sometimes you can only copy up to 50 lines. To solve this, increase the buffer limit between multiple files.
  +
<pre>
 
 
:help 'viminfo'
 
 
...
 
 
< Maximum number of lines saved for each register.
:help 'viminfo'
 
...
+
...
 
:set viminfo?
< Maximum number of lines saved for each register.
 
 
:set viminfo='100,<100,s10,h
...
 
  +
</pre>
:set viminfo?
 
:set viminfo='100,<100,s10,blablabla
 
   
 
----
 
----

Revision as of 16:46, 17 March 2015

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