Vim Tips Wiki
Register
(Undo revision 41543 by 203.167.225.194 (talk) :help v_x)
Tag: Undo
 
(76 intermediate revisions by 35 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=
 
}}
 
}}
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. See [[Cut/copy and paste using visual selection]] for the main article.
   
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 <code>v</code> to select characters, or uppercase <code>V</code> to select whole lines, or <code>Ctrl-v</code> to select rectangular blocks (use <code>Ctrl-q</code> if <code>Ctrl-v</code> is mapped to paste).
#Press '''v''' (or upper case '''V''' if you want to cut 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 <code>d</code> to cut (or <code>y</code> 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 <code>P</code> to paste before the cursor, or <code>p</code> 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 <code>y</code> instead of <code>d</code>:
  +
*<code>d</code> stands for ''delete'' in Vim, which in other editors is usually called ''cut''
  +
*<code>y</code> stands for ''yank'' in Vim, which in other editors is usually called ''copy''
   
  +
==Copying and cutting in normal mode==
The name of the mark used is related to the operation (d:delete or y:yank).
 
   
  +
In normal mode, one can copy (yank) with <code>y{motion}</code>, where <code>{motion}</code> is a Vim motion. For example, <code>yw</code> copies to the beginning of the next word. Other helpful yanking commands include:
I found that those mark names requires minimal movement of my finger.
 
   
  +
*<code>yy</code> or <code>Y</code> – yank the current line, including the newline character at the end of the line
==Copying and pasting outside the editor buffer==
 
  +
*<code>y$</code> – yank to the end of the current line (but don't yank the newline character); note that many people like to remap <code>Y</code> to <code>y$</code> in line with <code>C</code> and <code>D</code>
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.
 
  +
*<code>yiw</code> – yank the current word (excluding surrounding whitespace)
  +
*<code>yaw</code> – yank the current word (including leading or trailing whitespace)
  +
*<code>ytx</code> – yank from the current cursor position up to and before the character (til <code>x</code>)
  +
*<code>yfx</code> – yank from the current cursor position up to and including the character (find <code>x</code>)
   
  +
Cutting can be done using <code>d{motion}</code>, including:
Whaze Commands
 
  +
* <code>dd</code> - cut the current line, including the newline character at the end of the line
   
  +
To copy into a register, one can use <code>"{register}</code> immediately before one of the above commands to copy into the register <code>{register}</code>. See [[pasting registers]] for more information on register syntax.
[HELP COMMANDS]
 
LVL#0 !help //Common info [gainAccess]
 
LVL#0 !cmds //Command list
 
   
  +
==Pasting in normal mode==
   
  +
In normal mode, one can use <code>p</code> to paste after the cursor, or <code>P</code> to paste before the cursor.
[DP COMMANDS]
 
LVL#2 !dpstat //DP top fifteen
 
LVL#2 !dpdrop //DP drop fifteen
 
LVL#2 !dprank //Show your DP rank
 
LVL#2 !dphelp //Show DP help list
 
LVL#2 !dpinfo //Show how much DP YOU GAIN
 
LVL#2 !buy lottery //Buy lottery ticket
 
LVL#2 !buy //Buy items
 
LVL#2 !sell //Sell items
 
LVL#2 !drop //Drop items
 
LVL#2 !lottery //Show current lottery game
 
LVL#2 !dp (-username-) //Show your or someone else's DP
 
LVL#2 !dp give -username- -amount- //Give DP
 
LVL#2 !atk username -amount- //Perform DP attack
 
LVL#2 !touch -username -
 
LVL#2 !smoke
 
LVL#2 !drive
 
LVL#2 !sex -username-
 
LVL#8 !dp set -username- -amount-
 
LVL#8 !dp add -username- -amount-
 
LVL#8 !dp sub -username- -amount-
 
LVL#8 !dpban add/rem -username //Abusing this cmd = blacklist
 
   
  +
The variants <code>gp</code> and <code>gP</code> move the cursor after the pasted text, instead of leaving the cursor stationary.
   
  +
To select a register from which to paste, one can use <code>"{register}p</code> to paste from the register <code>{register}</code>. See [[pasting registers]].
[FUN COMMANDS]
 
LVL#2 !games //Gamelist
 
LVL#2 !rr //Russian-Roulette
 
LVL#2 !sb //Spin a bottle
 
LVL#2 !fc //Flip a coin
 
LVL#2 !passjoint //pass a joint
 
LVL#2 !passblunt //pass a blunt
 
LVL#2 !dice //Roll dice
 
LVL#2 !8ball -question- //Questioner
 
LVL#2 !rf //Random fact
 
LVL#2 !bury -item- //Bury item
 
LVL#2 !dig //Dig up item
 
LVL#2 !flip -username- (and -username)
 
LVL#2 !kill -username-
 
LVL#2 !heal -username-
 
LVL#2 !ps -username-
 
LVL#2 !rape -username-
 
LVL#2 !teabag -username-
 
LVL#2 !punch -username-
 
LVL#2 !nuke -username-
 
LVL#2 !vacuum -username-
 
LVL#2 !shiton -username-
 
LVL#2 !fuck -username-
 
LVL#2 !give -username- -item-
 
LVL#2 !make -username- -item-
 
LVL#2 !wod //Word of the day
 
LVL#2 !rq | !rq pb //RQ
 
LVL#2 !hugs -username-
 
LVL#2 !kiss -username-
 
LVL#2 !grab
 
   
  +
==Pasting in insert mode==
  +
The contents of a register can be pasted while in insert mode: type Ctrl-r then a character that identifies the register. For example, Ctrl-r then <code>"</code> pastes from the default register, and Ctrl-r then <code>0</code> pastes from register zero which holds the text that was most recently yanked (copied). See [[pasting registers]].
   
  +
==Copying and cutting in command-line mode==
[SRC COMMANDS]
 
  +
Command-line mode occurs after typing <code>:</code> to enter a command. By default, while in the command line, typing Ctrl-f opens the command-line window where commands can be edited using normal mode. For example, part of one command can be copied then pasted into another command. See [[using command-line history]].
LVL#2 !whois -username- //Someone's possible account(s)
 
LVL#2 !mini -username- //Get someone's mini profile!
 
LVL#2 !cso -username- //Check if someone is online
 
LVL#2 !find -username- //Find user
 
LVL#2 !rooms //Rooms without modded rooms
 
LVL#2 !mrooms //Rooms with modded rooms
 
LVL#2 !bgtime -username- //BG time
 
LVL#2 !bg -username- //Background Picture
 
LVL#2 !fpix -username- //Profile Picture
 
LVL#2 !rec -username- //Recording
 
LVL#2 !gis -search- //Google Image Search
 
LVL#2 !ytb -search- //Youtube
 
LVL#2 !udict -search- //Urban dictionary
 
   
  +
==Pasting in command-line mode==
  +
There are two approaches to pasting in command-line mode. The first is to open the command-line window with Ctrl-f, then use normal-mode commands to paste. See the [[#Copying and cutting in command-line mode|previous section]].
   
  +
The second approach is to type Ctrl-r then a character to paste the contents of the register identified by the character. See [[#Pasting in insert mode|Pasting in insert mode]] above.
[LVL COMMANDS]
 
LVL#2 !lvl -username- //Check your own or someone else's level
 
LVL#2 !reqlvl //Request level upgrade
 
LVL#2 !wl -username- //Whitelist for use
 
LVL#8 !chrank -username- -rank to set- //Set username level
 
   
  +
==Copy, cut, and paste from the system clipboard==
  +
''Main article: [[Accessing the system clipboard]]''
   
  +
Unlike most text editors, Vim distinguishes between its own registers and the system clipboard. By default, Vim copies to, cuts to, and pastes from its own default register, called the ''unnamed register'' (<code>""</code>, also called [http://vimdoc.sourceforge.net/htmldoc/change.html#quote_quote quotequote]) instead of the system clipboard.
[MOD COMMANDS]
 
LVL#M !purge //Get rid of useless anons! (If bot = mod)
 
LVL#M !banvote -username- //Request banvote (If bot = mod)
 
LVL#M !ban -username- //Ban user (If bot = mod)
 
LVL#M !tempban -username- -minutes- //Tempban user (If bot = mod)
 
LVL#M !unban //Unban user (If bot = mod)
 
LVL#M !banlist //Get room banned userlist
 
LVL#M !clear // Clear chat (If bot = mod)
 
LVL#M !del -username- //Clear the user's chat (If bot = mod)
 
LVL#M !rest | !lock //Lock Whaze
 
LVL#M !wake | !unlock //Unlock Whaze
 
LVL#O !leave -room- //LVL#8 also.
 
LVL#O !demod -username- //Demod username in your chatroom (If bot = owner)
 
LVL#O !mod -username- //Mod username in your chatroom (If bot = owner)
 
LVL#5 !join -room-
 
   
  +
Assuming Vim was compiled with clipboard access, it is possible to access the <code>"+</code> or <code>"*</code> registers, which can modify the system clipboard. In this case, one can copy with e.g. <code>"+y</code> in visual mode, or <code>"+y{motion}</code> in normal mode, and paste with e.g. <code>"+p</code>.
   
  +
If your installation of Vim was not compiled with clipboard support, you must either install a package that has clipboard support, or use an external command such as xclip as an intermediary. See [[Accessing the system clipboard]] for detailed information.
[MAIL COMMANDS]
 
LVL#2 !sn -username- //Send a note to someone through the chat!
 
LVL#2 !rn //Read your note
 
LVL#5 !pm -username- -message- //Send someone a message
 
LVL#2 !drag -username- //Invite someone to the room you're in
 
LVL#6 !send -roomname- -msg- //send msg to a chatroom.
 
LVL#7 !anc -msg- //Broadcast
 
   
  +
==Multiple copying==
  +
''Main article: [[Pasting registers]].''
   
  +
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.
[SET COMMANDS]
 
LVL#2 !df -username- as -definition- //Define someone || !df -definition- //See def.
 
LVL#2 !df rem -definition- //Remove the current definition
 
LVL#2 name me -newname-
 
LVL#7 !rename -username- -newname- //Rename username
 
   
  +
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.
   
  +
===Windows clipboard===
 
  +
When using Vim under Windows, the clipboard can be accessed with the following:
[MECH COMMANDS]
 
  +
*In step 4, press Shift+Delete to cut or Ctrl+Insert to copy.
LVL#2 love between -name- and -name-
 
  +
*In step 6, press Shift+Insert to paste.
LVL#2 !say -word/phrase- //Will make Whaze say something
 
LVL#3 !flag -username- //Flag someone
 
LVL#7 !banroom add/rem -roomname- //blacklist room
 
LVL#7 !mute add/rem -username- //Mute user (If bot = mod)
 
LVL#9 !sban add/rem -username- //Spamban user (If bot = mod)
 
 
 
[BOT MANAGING]
 
LVL#9 !ld
 
LVL#9 !ld release
 
LVL#9 !!! -room- -msg-
 
LVL#9 !ut
 
LVL#9 !sl
 
 
 
_________________________________________
 
 
[Phrases|words] v.3.6
 
_________________________________________
 
 
LVL#2 *gives Whaze a cookie*
 
LVL#2 cookie?
 
LVL#2 dance
 
LVL#2 fap
 
LVL#2 ><>
 
LVL#2 8|8
 
LVL#2 hello Whaze
 
LVL#2 bye Whaze
 
LVL#2 Whaze
 
LVL#2 brb
 
LVL#2 bck
 
LVL#2 the cake is a lie
 
LVL#2 Whaze behave
 
LVL#2 fuck you Whaze
 
LVL#2 ily Whaze
 
LVL#2 ty Whaze
 
LVL#2 Whaze show me the goods
 
 
 
_______________________________________
 
 
LEVEL | DEFINITION LEVEL [LVL]
 
_______________________________________
 
 
LVL#0 // NONE
 
LVL#1 // BLACKLISTED
 
LVL#2 // USER
 
LVL#3 // MEMBER
 
LVL#4 // TRUSTED USER
 
LVL#5 // MODERATOR
 
LVL#6 // MASTER
 
LVL#7 // SUPER-MASTER
 
LVL#8 // BOT OPERATOR
 
LVL#9 // GOD
 
-------------------------
 
LVL#M // CHAT MODERATOR
 
LVL#O // CHAT OWNER
 
   
 
===Different instances===
 
===Different instances===
How copy and paste between two instances of Vim on different Linux consoles?
+
How does one 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 {{Code|:w}}.
switch to the previous buffer (:bp) to release *.swp<br>
+
*Switch to the previous buffer {{Code|: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 {{Code|:r ~/dummy}}
   
 
==Increasing the buffer size==
 
==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'}}
Sometimes you can only copy up to 50 lines. To solve this, increase the buffer limit between multiple files.
 
   
  +
In the example below, the first line displays the current settings, while the second line sets:
:help 'viminfo'
 
  +
*<code>'100</code> [[Using marks|Marks]] will be remembered for the last 100 edited files.
...
 
< Maximum number of lines saved for each register.
+
*<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.
:set viminfo?
 
  +
<pre>
:set viminfo='100,<100,s10,h
 
 
:set viminfo?
 
:set viminfo='100,<100,s20,h
  +
</pre>
   
  +
==See also==
----
 
See also: [[VimTip356|Quick yank and paste]]}}
+
* [[VimTip356|Quick yank and paste]]
  +
* [[Cut/copy and paste using visual selection]]
  +
* [[In line copy and paste to system clipboard]]
  +
* [[Accessing the system clipboard]]
   
 
==Comments==
 
==Comments==

Latest revision as of 08:42, 10 December 2022

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. See Cut/copy and paste using visual selection for the main article.

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, or Ctrl-v to select rectangular blocks (use Ctrl-q if Ctrl-v is mapped to paste).
  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 stands for delete in Vim, which in other editors is usually called cut
  • y stands for yank in Vim, which in other editors is usually called copy

Copying and cutting in normal mode[]

In normal mode, one can copy (yank) with y{motion}, where {motion} is a Vim motion. For example, yw copies to the beginning of the next word. Other helpful yanking commands include:

  • yy or Y – yank the current line, including the newline character at the end of the line
  • y$ – yank to the end of the current line (but don't yank the newline character); note that many people like to remap Y to y$ in line with C and D
  • yiw – yank the current word (excluding surrounding whitespace)
  • yaw – yank the current word (including leading or trailing whitespace)
  • ytx – yank from the current cursor position up to and before the character (til x)
  • yfx – yank from the current cursor position up to and including the character (find x)

Cutting can be done using d{motion}, including:

  • dd - cut the current line, including the newline character at the end of the line

To copy into a register, one can use "{register} immediately before one of the above commands to copy into the register {register}. See pasting registers for more information on register syntax.

Pasting in normal mode[]

In normal mode, one can use p to paste after the cursor, or P to paste before the cursor.

The variants gp and gP move the cursor after the pasted text, instead of leaving the cursor stationary.

To select a register from which to paste, one can use "{register}p to paste from the register {register}. See pasting registers.

Pasting in insert mode[]

The contents of a register can be pasted while in insert mode: type Ctrl-r then a character that identifies the register. For example, Ctrl-r then " pastes from the default register, and Ctrl-r then 0 pastes from register zero which holds the text that was most recently yanked (copied). See pasting registers.

Copying and cutting in command-line mode[]

Command-line mode occurs after typing : to enter a command. By default, while in the command line, typing Ctrl-f opens the command-line window where commands can be edited using normal mode. For example, part of one command can be copied then pasted into another command. See using command-line history.

Pasting in command-line mode[]

There are two approaches to pasting in command-line mode. The first is to open the command-line window with Ctrl-f, then use normal-mode commands to paste. See the previous section.

The second approach is to type Ctrl-r then a character to paste the contents of the register identified by the character. See Pasting in insert mode above.

Copy, cut, and paste from the system clipboard[]

Main article: Accessing the system clipboard

Unlike most text editors, Vim distinguishes between its own registers and the system clipboard. By default, Vim copies to, cuts to, and pastes from its own default register, called the unnamed register ("", also called quotequote) instead of the system clipboard.

Assuming Vim was compiled with clipboard access, it is possible to access the "+ or "* registers, which can modify the system clipboard. In this case, one can copy with e.g. "+y in visual mode, or "+y{motion} in normal mode, and paste with e.g. "+p.

If your installation of Vim was not compiled with clipboard support, you must either install a package that has clipboard support, or use an external command such as xclip as an intermediary. See Accessing the system clipboard for detailed information.

Multiple copying[]

Main article: Pasting registers.

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 does one 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[]