The interaction between vim and the system clip version

Posted May 27, 20205 min read

1 Overview

The contents of copying, deleting, and replacing(d, r, s, x, y, etc.) in vim will be saved to the default unnamed register, which can be pasted through p later, but this register is not a scrapbook version of the system In many cases, the interaction between vim and the system clip version is required. So, how to do it?

2 Violent resolution

When copying vim in the system clip version, create a new file, directly copy ctrl + v and then open vim.
Copy the content in vim to the system clip version, use cat + grep, copy it in the terminal ctrl + c.
This is the simplest and more general method, and will not be affected by what vim does not support the scrapbook version.
~ Yes, it's simple and rough. ~

3 Elegant plan

Of course, the above method is not what you want, so here is a more elegant method.

3.1 View clipboard support

First of all, you need to know whether vim supports the clipping version:

vim --version | grep clipboard

Insert picture description here
If there is

+ clipboard

It means support, if not, for example, the author does not support this, and needs to be installed.
My Manjaro can be installed with yay, but the prompt expired:
Insert picture description here
You can also choose to install gvim, in addition, Debian can install vim-gtk:

sudo apt install vim-gtk

For Redhat system, you need to install vim-X11 and vim-enhanced, and then add an individual name:

alias vi = 'vimx'
alias vim = 'vimx'

MacOS:

sudo brew install vim

3.2 Copy system clipboard to vim

Very simple, use

"+ p

or

"* p

That s it, vim provides

+ *

These two registers interact with the system clipboard. On most systems, these two systems are the same, but may not be equivalent on some Linux. The following is official document :
Insert picture description here

+

Corresponding to the system clipboard used by ctrl + c, ctrl + v,

*

Corresponding to the content selected by the mouse, you can use the middle paste.
Of course, the higher version of vim has a very simple solution:Shift + Insert can directly paste.

3.3 Copy the vim register to the system clipboard

Use y to copy in vim, but it is copied to the default unnamed register. If you want to copy to the system clipboard, you only need to change y to

"+ y

or

"* y

That's it.

3.4 Lazy method

The so-called lazy method is to associate the default unnamed register with the system clipboard and use

set clipboard = unnamed

That's it. Vim7.3.74 and above can be used:

set clipboard = unnamedplus

In this way, y, d, s, x and other operations are associated with the system's scrapbook.

4 Register related knowledge

The contents of the operations of y, d, c, x, s, etc. in vim will be copied to the register and can be used

:reg

View the contents of the register, use

:help registers

View help. In addition, common operations of registers include:

"{register} y is copied to the register, the default register is an unnamed register
"{register} p paste the contents of the register

such as

"a4yy
"+ p

Vim provides a variety of registers. Specifically, it can be divided into 10 types and a total of 48 registers.

4.1 Unnamed registers

""

Also called anonymous register, the characters deleted using d, c, x, etc. will be stored in the anonymous register. In other words, the last deleted, modified, copied content will be stored here and will be overwritten.

4.2 Number register

"0
"1
...
"9

A total of 10 from 0 to 9 can be subdivided into copy special registers and delete special registers, because

"0

Holds the last copy of y, and

"1
...
"9

These 9 registers hold the contents of the last deletion(d, c, s, x, etc.). The last deleted content is stored in

"1

The last time it was stored in

"2

And so on.

Note that only the deletion of the entire line, and the deletion of the movement instructions through the paragraph level will be put in

"1

In addition, after specifying the register, such as

"ayy

Will not be written to

"0

Medium, but

"add

will be written to

"1

In , that is to say, the copied content will not be written to the 0 register, but the deleted content will be written to the 1 register.

4.3 Small delete register

"-

Small deletes with less than one line will be put in, including s, d, c, x. versus

"0

Similarly, the user-specified register will not be written.

4.4 Named Register

"a
"b
...

The 26 registers composed of lowercase letters are generally used to record keyboard macros. For example, they have the following contents:

iostream
string
cmath

You can position it to the first line, and then

qaI # include <{ESC} A> {ESC} jq

among them

{ESC}

It means pressing the ESC key, so that the first line adds include, and the macro is recorded in the register a. Next, use

2 @ a

You can add include to the remaining two lines.
In addition, the uppercase letters corresponding to lowercase registers are used for additional content, such as:

"Ayy

Can

"a

Append content.

4.5 Read-only registers

". Last inserted content
"%The current file name, the path from vim's working directory to the file
":Commands typed in the last command mode, use @:to execute the last command.. Only some simple repetitions, some complex commands such as replacement, need to use @:

A total of three, read-only.

4.6 Alternate file register

"#

Save the alternate file of the current window.
Alternate file is a file on the buffer(Buffer), and the part of the memory occupied by the buffer in the real-time vim when the file is opened for editing. When editing the file, it is only to edit the file in the buffer. When used

:w

, The buffer file will be written to disk.

4.7 Expression register

"=

Calculate the return value of the vim script. After input, it will jump to the command line. At this time, if you enter 3 + 2, press Enter and p, it will paste 5.

4.8 Select Drag and Drop Register

Two have been introduced above:

"+
"*

Another one is

"~

among them

"+
"*

On Mac and Windows, it refers to the system clipboard, which is different in most X11, that is, Linux distributions with desktop environments. One point is mentioned above:

"+ System Clipboard
"* Mouse selected area, you can paste with the middle button

When there is text dragged to vim, it is stored in

"~

, The content will be inserted into the cursor position by default.

4.9 Black Hole Register

"_

All content that enters will disappear, and is usually used in scripts.

4.10 Search Register

"/

Store the last keyword searched. Writable

:let @/= "123"

The next search can be directly

/

Enter without entering keywords.
Note that it does not exist

"?

register.

5 Reference

  1. Vim Register Complete Manual

  2. Vim Official Document-1

  3. Vim Official Document-2

  4. Know-how to paste the contents of the Vim clipboard to a place outside Vim

Related Posts