How to Copy Text to Clipboard in Vim

copy text to clipboard in vim

In this article, I am going to show you how to copy text from Vim editor to another program. Our goal is to get the content copied to the clipboard.

To use copy and paste within the vim is easier using yank command or using registers. But when it comes to copy to another application say to a browser we need extra steps. Here I will explain two methods to copy text to your system clipboard.

  • By Installing vim-gtk
  • By Compiling Vim

Method 1: By Installing vim-gtk

So, how do we copy from the Vim to another Vim window or a browser? By default, Vim does not have this ability to access your system clipboard, but getting this ability is actually extremely simple. One way is just to compile a version of Vim that has access to clipboard, another lazy way is to just install gvim (also known as vim-gtk).

Gvim is a graphical version of Vim. When you install gvim, it will give the normal terminal vim its copying and pasting abilities to your clipboard. In other words, it gives you the ability to copy to a special register that corresponds to your system's clipboard. Special register is a +. It is really just a register you can copy things to and that will automatically put it in your system clipboard.

To install gvim (or vim-gtk), run:

$ sudo apt-get install vim-gtk

Let us consider an example of copying a link address and paste it to the browser. Please open our example file and navigate to the last line. Now, you have to run the following command:


+  is your system's clipboard

Open your browser and press ctrl+v the same way you would normally paste and see that now it is pasted in. We can go to that site and get something from it, copy and paste to our Vim. Let's copy the same way that we do normally with ctrl+c. Now, we can access this copied line in Vim by running the following commands:


You see that it successfully pastes the content from the browser to Vim.

Method 2: By Compiling Vim

Another way is to compile Vim to support clipboard copying. The default Vim does not have it under the hood. So, to check, whether this option is valid, please run:

vim --version | grep .xterm_clipboard -o

If you'll get something like this: -xterm_clipboard

This means that copying to clipboard is not supported and you have to compile your Vim. Worth to mention, that only the Vim version with GUI supports clipboard access. So, in order to recompile the Vim you have to start by getting the compile dependencies of Vim and install mercurial:

$ sudo apt-get build-dep vim
$ sudo apt-get install mercurial

Now, get the source:

$ sudo hg clone

If you already had this repository clone, then update it:

$ sudo hg pull
$ sudo hg update

And then check if you have the latest version:

$ hg summary

Now, you have to compile it:

cd vim_source
./configure \
   --enable-perlinterp=dynamic \
   --enable-pythoninterp=dynamic \
   --enable-rubyinterp=dynamic \
   --enable-cscope \
   --enable-gui=auto \
   --enable-gtk2-check \
   --enable-gnome-check \
   --with-features=huge \
   --with-x \
   --with-compiledby="YourName <>" \
make && sudo make install

That will install it in /usr/local path. Please note that you will end up with two vim's: /usr/bin/vim without access to your clipboard:

vim installed in /usr/bin/vim
vim installed in /usr/bin/vim

and /usr/local/bin/vim with access to your clipboard:

vim installed in /usr/local/bin/
vim installed in /usr/local/bin/

And with gvim /usr/local/bin/gvim (with a symlink to /usr/local/bin/vim. It will support clipboards whether or not you're in GUI mode. And you can always run the old one with the explicit path.

Once everything is compiled, you can use /usr/local/bin/vim to open any file and copy-paste text the same as we considered before by using the ‘+’ register.

How to store text into register

Registers are small storage space in on your system that can store the contents you have yanked or copied. You can use many registers to store different yanked contents.

Use the following command  will select a line and store to 'a' register:



V (capital V) - for marking the whole line
" (double quote) - to access the register
a - register name
y - yank (copy)

And for the 'b' register


Now, we have saved text in two different registers and we can paste them out, and we can paste them out in a different order. For instance, we can paste out ‘b’ register by running the following command:



p - paste


In this article, we have considered two ways of how to enable access to your clipboard in Vim. Both options are valid and it seems that installing the vim-gtk package is faster and efficient than compiling Vim.

Leave a Comment