Skip to main content

Other Vim Tips

Copy Paste Text to and from System Clipboard in Vim

Learn to configure Vim so that you can copy paste between Vim and other tools outside Vim through the system clipboard.

As you travel further in your Vim journey, after you start using Vim more frequently, you might notice that you're getting annoyed due to the lack of integration between Vim's clipboard and your system's.

You can configure Vim to copy the text to the system clipboard and paste it outside Vim.

To do that, run the following Vim command from your current Vim session:

:set clipboard+=unnamedplus

And now, if you use the yanking command to copy text in Vim, it will be copied to the system clipboard as well. And then you can use the regular way to paste them (Ctrl+V).

You remember the yanking commands for copying, don't you? Press Esc to enter the normal mode and use one of the following key that suits your copying needs.

Running the above command in Vim will make Vim use the system clipboard for that particular session. To make this change permanent, please add the following line to your ~/.vimrc file:

set clipboard+=unnamedplus

Keep reading if you want to understand how this works!

Modifying the clipboard provider

To have Neovim/Vim use the system clipboard, all we need to do is append the unnamedplus value to the clipboard option. You can either enable it temporarily or permanently.

Let's see how both are done!

Enabling system clipboard temporarily

To use your system's clipboard for a particular session, run the following command:

:set clipboard+=unnamedplus

Enabling system clipbard permanently

To have Vim always use your system's clipboard (works on Linux and macOS), add the following line to your .vimrc file:

set clipboard+=unnamedplus

If you're a chad, then add this to your Neovim config:

vim.opt.clipboard:append('unnamedplus')

What does this do?

Like a CPU, Vim has registers that it uses for storing results of operations. There is a register named + which is where yanked (copied) or deleted text is stored as kind of a clipboard.

Though not technically an "internal clipboard", the + register does act like an internal clipboard to Vim, isolating your yanks and deletes from reaching the system clipboard.

By appending the unnamedplus name to the clipboard list-value, we tell Vim to use a different register instead. This register is named *.

This, * register is your system's clipboard.

The TL;DR of this is, on Linux Vim kind of sort of has an "internal" clipboard (not a clipboard but a register). By appending unnamedplus to clipboard, we tell Neovim/Vim that "Hey, please always copy to and paste from the system clipboard."

Troubleshooting

If copy pasting still does not work for you, please make sure that clipboard utilities for your particular platform and environment are installed.

For Wayland users on Linux, please make sure that wl-clipboard is installed. If you are on X11, please install xsel and xclip.

Conclusion

In this short article, we looked at how appending the unnamedplus value to the clipboard option in Vim, we can use the system clipboard directly.

Pratham Patel
Gujarat, India