Vim Tips Wiki
Register
No edit summary
(Undo revision 38861 by 24.216.184.191 (talk))
Tag: sourceedit
(39 intermediate revisions by 28 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=
 
}}
 
}}
  +
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:'''
#Place the cursor at the beginning of the block you want to CUT.
 
#Mark it with md
 
#Go to the end of the block.
 
#Cut it with d'd
 
#Go to the new location that you want to PASTE those text.
 
#Press P.
 
 
Copy and Paste:
 
#Place the cursor at the beginning of the block you want to COPY.
 
#Mark it with my
 
#Go to the end of the block.
 
#Copy it with y'y
 
#Go to the new location that you want to PASTE those text.
 
#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.
 
 
==Comments==
 
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
   
  +
==Multiple copying==
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.
 
  +
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.
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.
 
   
  +
===Windows clipboard===
----
 
  +
When using Vim under Windows, the clipboard can be accessed with the following:
How copy and paste between two instances of Vim on different Linux consoles?
 
  +
*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:
 
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==
----
 
  +
By default, only the first 50 lines in a register are saved, and a register is not saved if it contains more than 10 kilobytes. {{help|'viminfo'}}
If you want to simulate the Windows way of Cut/Copy/Paste you could add the following line to your initialization file.
 
   
  +
In the example below, the first line displays the current settings, while the second line sets:
source $VIMRUNTIME/mswin.vim
 
  +
*<code>'100</code> [[Using marks|Marks]] will be remembered for the last 100 edited files.
  +
*<code><100</code> Limits the number of lines saved for each register to 100 lines; if a register contains more than 100 lines, only the first 100 lines are saved.
  +
*<code>s20</code> Limits the maximum size of each item to 20 kilobytes; if a register contains more than 20 kilobytes, the register is not saved.
  +
*<code>h</code> Disables [[Highlight all search pattern matches|search highlighting]] when Vim starts.
  +
<pre>
 
:set viminfo?
 
:set viminfo='100,<100,s20,h
  +
</pre>
   
  +
==See also==
Read :help :behave for other information.
 
 
*[[VimTip356|Quick yank and paste]]
   
 
==Comments==
----
 
See [[VimTip356|Quick yank and paste]].
 
 
----
 
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 copied pages.
 
 
:help 'viminfo'
 
:set viminfo?
 
:set viminfo='100,<100,s10,h
 

Revision as of 13:02, 25 January 2016

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

By default, only the first 50 lines in a register are saved, and a register is not saved if it contains more than 10 kilobytes. :help 'viminfo'

In the example below, the first line displays the current settings, while the second line sets:

  • '100 Marks will be remembered for the last 100 edited files.
  • <100 Limits the number of lines saved for each register to 100 lines; if a register contains more than 100 lines, only the first 100 lines are saved.
  • s20 Limits the maximum size of each item to 20 kilobytes; if a register contains more than 20 kilobytes, the register is not saved.
  • h Disables search highlighting when Vim starts.
:set viminfo?
:set viminfo='100,<100,s20,h

See also

Comments