How to Copy to System’s Clipboard
In Vim, you can copy, cut, and paste texts using the yank commands provided by the y, d, and p keys, respectively.
However, by default, vim will store the copied text into an internal register called the unnamed register. Unfortunately, this register is not supported by other applications.
This means that if you wish to paste text from vim, you will have to use the system’s clipboard.
This is what we will discuss in this tutorial.
As stated, vim does not have permission to access your system’s clipboard by default. However, you can resolve this by either compiling vim with the clipboard access ability or installing a graphical version of vim.
On Debian, install GTK Version of Vim:
GTK-Vim allows you to have access to a special register that corresponds to the system’s clipboard. This allows you to copy and paste text from vim with the yank commands.
If you are on a mac, you can reconfigure the Vim Brew Formula to work with the clipboard.
Start by installing vim with Homebrew:
Once vim is installed, we can configure the formula and enable access to the system’s clipboard.
Run the command:
The command will put you into developer mode and allow you to customize vim. In the config, locate the entry system “./configure” option.
Change the –without-x to –with-x and add the line “–with-features=huge.” Save and close the file
Next, run the command below to install the modified vim formula
The command should rebuild and install vim with the clipboard option.
You can check that you have the vim system’s clipboard functionality with the command:
If successful, you should +clipboard or +xterm_clipboard options:
Copying Text to System Clipboard
Once you have enabled the system’s clipboard access on vim, you can use the yank command, followed by a few modifications.
To copy to the system’s clipboard in Vim, start by selecting your target register using the command:
Where * refers to the system’s clipboard in mac and Windows.
Once selected, you can copy the text you wish using the y command.
Therefore, to copy a text to the system’s clipboard, run the command:
On Linux, use the command:
The commands above should allow you to copy the text into your system’s clipboard.
This tutorial explores how we can configure vim to access the system’s clipboard. We also covered the commands you can use to copy a text into the system’s clipboard in Vim.