Reprinted from my independent blog https://liushiming.cn/2020/01/18/copy-and-paste-in-vim/
Outline
Copy and paste text editor is the most commonly used functions, copy and paste in vim but still a little trouble, a little learning costs. This article summarizes the typical usage scenarios and use vim copy and paste, I hope to help the reader.
Vim internal copy, paste, cut
Select the text
- v + cursor (by character selection) to highlight the text, and then performs various operations (for example, d represents deleted).
- V (row selection)
- v + + c to change the selected content selected text
Copy: y (ank)
- Y after the command to select text with the v, to copy with y
- yy Copy the current line, then p-paste
- 5yy copied from the beginning of the current line 5
- y_ equivalent to yy
- Y is equivalent to yy
- yw Copy the current word
- Y $ copied from the current position to the end of line
- y0 copied from the current position to the beginning of the line
- y ^ copied from the current position to the first non-blank character
- yG copied from the current line to the end of the file
- y20G copied from the current line to the line 20
- y? bar copy to the previous position bar appears
Paste: p (aste)
- p (lower case) attached to the cursor position after the
- P (upper case) attached to the cursor position before
Shear
- v + arrow key (the hjkl) selected text pasting + d + p Cut
Clipboard
The default clipboard
providing the clipboard 12 vim, their names are vim clipboard 11 has, respectively 0
, 1
, 2
, ...
, 9
, a
, “
. If you turn the system clipboard, it will be another two more: +
and *
. Use :reg
command, you can view the contents of each clipboard.
:reg
Y is only used in a simple replication into vim “
(double quote) pasteboard, the same is with the p paste the contents of the clipboard.
Copy and paste the clipboard to the specified
Vim copy the contents to a clipboard, you need to exit edit mode, after entering the normal mode, select the content you want to copy, then press the "Ny complete copy, where N is the number pasteboard (note the quotation marks and then double click on press the clipboard by number of the last y), such as copying to the clipboard content should a, the contents of the selected press "ay it.
To some vim Paste the contents of the clipboard in, need to exit editing mode, in normal mode, press the "Np, where N is the number clipboard. For example, you can press" 5p to paste the clipboard came in No. 5 in the content, but also you can press "+ p global system clipboard to paste the contents of the room.
The system clipboard
Vim supports the system clipboard, you need to open the clipboard function. Use the following command to check the current version of Vim, supports clipboard.
$ vim --version | grep "clipboard"
-clipboard +insert_expand +path_extra +user_commands
+emacs_tags -mouseshape +startuptime -xterm_clipboard
root@sz ➜ ~ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun 07 2019 15:35:43)
-clipboard
Representatives do not support +clipboard
representatives supported.
If not, then you need to install gvim or turn on the system clipboard option at compile time.
vim copied to the system clipboard
"*y
"+y
"+2yy
- Copy the two lines{Visual}"+y
- copy the selected text into the system clipboard"+y{motion}
- copy the text specified by {motion} into the system clipboard:[range]yank +
- copy the text specified by[range]
into the system clipboard
vim into the system clipboard
"+dd
- Cut a row
Paste from the system clipboard to vim
"*p
"+p
Shift+Insert
:put +
- Ex command puts contents of system clipboard on a new line<C-r>
+ - From insert mode (or commandline mode)
"+p
Better than Ctrl-v command, it can be faster and more reliably handle pasting large blocks of text, can be avoided when pasting large amounts of text, the occurrence of each line of automatic indentation accumulation, because Ctrl-v
a stream processing through the system cache, row by row processing pasted text.
Vim default settings to use the system clipboard
OK vim support +clipboard
after, if you want y/p
, and get through the system clipboard directly, you can ~/.vimrc
add the following configuration):
set clipboard^=unnamed,unnamedplus
Wherein unnamed representative of *
registers, unnamedplus representative of +
registers. In mac system, both of the same; linux systems in general +
and *
is different, +
corresponding to ctrl + c
, ctrl + v
desktop system clipboard, *
the corresponding x desktop clipboard (select with the mouse to copy, paste with the mouse button).
Server vim copied to the local clipboard
When connecting to a linux server via ssh, vim is running on a remote server, you can not use y
copied to the local clipboard. Then there are several ways:
cmd + c
Select the text with the mouse cmd + c
to copy (the windows ctrl + c
copy), cmd + v
and paste to the local. This method is the most natural, but want to use this method has several prerequisites:
- vim configuration is turned on mouse support,
.vimrc
file plus
set mouse=a
- terminal client closes
mouse reporting
option, or mouse click vim interface will enter visual mode. I refer to this article to solve the problem iterm2 in vim can not copy the selected text - If you use tmux, tmux need to configure the mouse wheel support, otherwise can only copy the contents of the current page
Local vim editing remote files via scp
Use local vim to edit remote files directly via scp. So that you can use local registers "+y"
copied. This method does not require the remote configuration vim.
vim scp://[email protected]//absolute/path/to/file
Com and absolute attention between two backslash is //
not a knock on the wrong.
Reference links
- Accessing the system clipboard
- Editing remote files via scp in vim
Vim configuration Easy
// title :( get to know a text vim copy and paste)
// englishTitle : (Copy-and-Paste-in-vim)
// category : (vim, problem, problem-the Solved)
// Tags : (vim)
createTime // : (20,200,118)
// LastUpdateTime : (20,200,118)
reprinted from my independent blog https://liushiming.cn/2020/01/18/copy-and-paste-in-vim/Outline
Copy and paste text editor is the most commonly used functions, copy and paste in vim but still a little trouble, a little learning costs. This article summarizes the typical usage scenarios and use vim copy and paste, I hope to help the reader.
Vim internal copy, paste, cut
Select the text
- v + cursor (by character selection) to highlight the text, and then performs various operations (for example, d represents deleted).
- V (row selection)
- v + + c to change the selected content selected text
Copy: y (ank)
- Y after the command to select text with the v, to copy with y
- yy Copy the current line, then p-paste
- 5yy copied from the beginning of the current line 5
- y_ equivalent to yy
- Y is equivalent to yy
- yw Copy the current word
- Y $ copied from the current position to the end of line
- y0 copied from the current position to the beginning of the line
- y ^ copied from the current position to the first non-blank character
- yG copied from the current line to the end of the file
- y20G copied from the current line to the line 20
- y? bar copy to the previous position bar appears
Paste: p (aste)
- p (lower case) attached to the cursor position after the
- P (upper case) attached to the cursor position before
Shear
- v + arrow key (the hjkl) selected text pasting + d + p Cut
Clipboard
The default clipboard
providing the clipboard 12 vim, their names are vim clipboard 11 has, respectively 0
, 1
, 2
, ...
, 9
, a
, “
. If you turn the system clipboard, it will be another two more: +
and *
. Use :reg
command, you can view the contents of each clipboard.
:reg
Y is only used in a simple replication into vim “
(double quote) pasteboard, the same is with the p paste the contents of the clipboard.
Copy and paste the clipboard to the specified
Vim copy the contents to a clipboard, you need to exit edit mode, after entering the normal mode, select the content you want to copy, then press the "Ny complete copy, where N is the number pasteboard (note the quotation marks and then double click on press the clipboard by number of the last y), such as copying to the clipboard content should a, the contents of the selected press "ay it.
To some vim Paste the contents of the clipboard in, need to exit editing mode, in normal mode, press the "Np, where N is the number clipboard. For example, you can press" 5p to paste the clipboard came in No. 5 in the content, but also you can press "+ p global system clipboard to paste the contents of the room.
The system clipboard
Vim supports the system clipboard, you need to open the clipboard function. Use the following command to check the current version of Vim, supports clipboard.
$ vim --version | grep "clipboard"
-clipboard +insert_expand +path_extra +user_commands
+emacs_tags -mouseshape +startuptime -xterm_clipboard
root@sz ➜ ~ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun 07 2019 15:35:43)
-clipboard
Representatives do not support +clipboard
representatives supported.
If not, then you need to install gvim or turn on the system clipboard option at compile time.
vim copied to the system clipboard
"*y
"+y
"+2yy
- Copy the two lines{Visual}"+y
- copy the selected text into the system clipboard"+y{motion}
- copy the text specified by {motion} into the system clipboard:[range]yank +
- copy the text specified by[range]
into the system clipboard
vim into the system clipboard
"+dd
- Cut a row
Paste from the system clipboard to vim
"*p
"+p
Shift+Insert
:put +
- Ex command puts contents of system clipboard on a new line<C-r>
+ - From insert mode (or commandline mode)
"+p
Better than Ctrl-v command, it can be faster and more reliably handle pasting large blocks of text, can be avoided when pasting large amounts of text, the occurrence of each line of automatic indentation accumulation, because Ctrl-v
a stream processing through the system cache, row by row processing pasted text.
Vim default settings to use the system clipboard
OK vim support +clipboard
after, if you want y/p
, and get through the system clipboard directly, you can ~/.vimrc
add the following configuration):
set clipboard^=unnamed,unnamedplus
Wherein unnamed representative of *
registers, unnamedplus representative of +
registers. In mac system, both of the same; linux systems in general +
and *
is different, +
corresponding to ctrl + c
, ctrl + v
desktop system clipboard, *
the corresponding x desktop clipboard (select with the mouse to copy, paste with the mouse button).
Server vim copied to the local clipboard
When connecting to a linux server via ssh, vim is running on a remote server, you can not use y
copied to the local clipboard. Then there are several ways:
cmd + c
Select the text with the mouse cmd + c
to copy (the windows ctrl + c
copy), cmd + v
and paste to the local. This method is the most natural, but want to use this method has several prerequisites:
- vim configuration is turned on mouse support,
.vimrc
file plus
set mouse=a
- terminal client closes
mouse reporting
option, or mouse click vim interface will enter visual mode. I refer to this article to solve the problem iterm2 in vim can not copy the selected text - If you use tmux, tmux need to configure the mouse wheel support, otherwise can only copy the contents of the current page
Local vim editing remote files via scp
Use local vim to edit remote files directly via scp. So that you can use local registers "+y"
copied. This method does not require the remote configuration vim.
vim scp://[email protected]//absolute/path/to/file
Com and absolute attention between two backslash is //
not a knock on the wrong.