Open the system clipboard function of Vim in Ubuntu 18


1. Check whether Vim supports the system clipboard

There are two ways to check whether Vim supports the system clipboard function.

Method 1: View the information on the terminal

  • Enter at the terminalvim --version | grep clipboardEnter:
jpch89@jpch89-Ubuntu:~$ vim --version | grep clipboard
+clipboard         +jumplist          +persistent_undo   +virtualedit
-ebcdic            +mouseshape        +statusline        +xterm_clipboard
Copy the code
  • ifclipboardThe front is-Please continue to see the next section if both are supported+You can skip to the last section.

Method two: InVimIn the view

  • Enter at the terminalvimEnter into theVim
  • The input:echo has('clipboard')enter
  • Let’s say it shows0The system clipboard is not supported. Please continue to the next section if1Support system clipboard, you can directly skip to the last section.

2. Enable the system clipboard function

  • There are many ways to open the system clipboard function, the more orthodox should be recompile and installVimSource code, because I am afraid of trouble (horizontal slag), so choose the simple method, namely installationvim-gnome.
  • A line of instructions can be done, in the terminal inputsudo apt install vim-gnome -yPress enter.
  • At this point, the above two methods are used again to verify, and foundVimThe system clipboard function is supported.

3. Use the system clipboard function

3.1 Copying a Vim File from a Terminal

  • First copy a sentence in the terminal, such asprint('Hello, world! ').
  • vim hello.pyPress enter.
  • :reg +View the system clipboard, as shown in the figure below:

  • "+pPaste, successful:


3.2 Copying Vim Files to Terminals

  • In the firstVimCopy a sentence in it, likels
  • ctrl + alt + tOpen a new terminal,ctrl + shift + vPaste, success!

Note: if the Vim terminal is closed here, it is not possible to paste!


3.3 Setting the System Clipboard as the Default Register

  • inVimThe default register is an anonymous register (""), we can pass the modificationvimrcFile to use the system clipboard as the default register, so you don’t have to add it every time"+The prefix.
  • Add this line to the configuration fileset clipboard^=unnamed,unnamedplus, done!

As to why write this line configuration, mainly for cross-platform general, specific how to study, I have no interested friends can refer to the following links: stackoverflow.com/questions/3…


Completed in 2019.6.27