Skip to content

[Linux] Copy Text from Vim Editor to System Clipboard

When we using Vim editor to edit our text/program file, if we can copy and paste to outside environment, it is very convenient.

But in the native Vim environment, we can only select the text/code block in Visual mode, copy it with y and paste it with p. However, if we want to copy the code post it elsewhere, it may not be able to post it successfully.

不過若是在原生的 VIM 環境當中,我們可以以 “Visual” 模式選取我們要複製的程式碼區塊、使用 “y” 複製、再以 “p” 貼上 —— 然而,若是我們希望將程式碼複製貼上到別的地方,比如說我目前所寫的這篇部落格,那麼我們很有可能無法成功貼上。

Conversely, if we copy the external code, it is difficult for use to paste it directly in Vim.

But Vim still supports these functions, but we need to add some settings.


Let Vim Editor support the system clipboard

There is already some “copy and paste” registers in VIM. We can see through “:reg“. This is why we can use y and p in Vim to copy strings.

As for why it can’t be posted in the external environment? This is because the VIM you are currently using probably does not support the system clipboard. We use the following command to check:

vim --version | grep "clipboard"

Output:

As you can see, there is a - symbol on the right side my clipboard, which means that my Vim does not support system clipboards.

To solve this problem, we need to install a GUI version of Vim, which is usually automatically supported.

sudo apt-get install vim-gnome


After the installation is complete, we use the following command again to check whether VIM now supports the system clipboard:

vim --version | grep "clipboard"

Output:

As you can see, there is now a + symbol on the right side clipboard; this means that our VIM already supports the system scrapbook.


References


Read More

Tags:

Leave a Reply