How to use clipboard for Vim in VSCode

Visual Studio Code has a decent Vim extension. I use it by default and sometimes wish I could yank to and paste from the system clipboard. Thankfully, this feature is available in the Vim extension, but turned off by default.

To turn on this feature, set the vim.useSystemClipboard setting to true. You may need to restart VSCode for it to take effect.

Note that you can yank text to the clipboard of your OS and also paste from it into VSCode using Vim.

Tried with: Vim extension for Visual Studio Code 0.11.5 and Visual Studio Code 1.22.2

The two clipboards in Linux

If you have come to Linux from Windows or Mac, you might have observed some strange behavior when copying or pasting content. This confusing behavior is caused because there are usually two clipboards in Linux.

X clipboard

The X window system has its own clipboard. It is also known as a cutbuffer. Any text or content you mark by highlighting with the mouse cursor is automatically copied to this clipboard. This is known as the PRIMARY selection or X Window selection or just selection in X jargon. When you middle-click the mouse cursor at the destination location, this copied content is pasted there.

Window manager clipboard

In addition to the X clipboard, your window manager or desktop environment (like KDE or GNOME) typically maintains its own clipboard. It might even have a clipboard manager which can maintain a historical buffer of content copied to the clipboard.

Typically, text or content is copied to this clipboard when you highlight it and use the keyboard shortcut Ctrl+C or right-click with mouse and choose Copy. Content from this clipboard is pasted when you use the keyboard shortcut Ctrl+V or right-click with mouse and choose Paste.

Syncing clipboards

Typically, the window manager or its clipboard manager will have an option that allows you to sync this clipboard with the X window selection clipboard. That is, content copied in either clipboard appears in the other ready for pasting.


How to use xclip

xclip is a very useful tool to copy and paste text content between the shell and GUI (X11) programs. You can pipe text to xclip and that text will be copied to the X clipboard. You can invoke xclip to output and it will spit out the content in the X clipboard. Once you learn it, you will wonder how you ever worked without it at the shell, inside Vim and across SSH sessions.

  • Installing it is easy:
$ sudo apt install xclip
  • To copy output from shell to X11 clipboard:
$ pwd | xclip
  • To paste from X11 clipboard to shell:
$ xclip -o | ls
  • Note that xclip works with the X clipboard, not the clipboard maintained by your window manager. Yes, you have two clipboards in Linux! So, if this copy-paste is not working for you, then the selection copied to your X clipboard might not be appearing in your window manager clipboard. Typically, the window manager clipboard is configured by default for all of this to work transparently. If it is not working, then you can configure the window manager clipboard to get all the selections from X clipboard.

  • To copy from Vim to clipboard: Visually highlight the text or lines you want to copy and type :w !xclip. The :w command is used to write the entire contents of the buffer or the visually highlighted text to a specified output file. Instead of a file, we write that text to xclip here, so it will be copied to the clipboard.

  • To copy from clipboard to Vim: Just do the reverse of the above operation. Type this command :r !xclip -o. The :r command in Vim is used to read text into the current buffer. So, this command takes the output of xclip and inserts it into the currently buffer.

  • Copy or paste in SSH session: The above commands works without any change between your local computer and the remote computer you have SSHed into! Yes, works like magic all thanks to the X11 protocol! Note that the remote computer should have xclip installed and you should have created the SSH session with X11 forwarding.

Tried with: XClip 0.12 and Ubuntu 14.04

Diodon not copying to its clipboard


I have Diodon working fine on a Ubuntu 14.04 computer. But, when I tried it on a Ubuntu 15.04 computer, it would not work. It is running, but nothing I copy is appearing in its clipboard, which remains empty!


The newer version of Diodon now uses the Zeitgeist service for tracking what is copied by the user. This is described in this blog post. I usually turn off all logging of file and application use on Ubuntu and this is nothing but the Zeitgeist service.

To minimally turn back Zeitgeist just enough for Diodon to work, go to System Settings -> Security and Privacy -> File and Applications. Turn ON the Record file and application usage option and include Documents. The rest can be disabled, they are not needed.

Restart Diodon and it should now work as expected.

Tried with: Diodon 1.2.0 and Ubuntu 15.04

How to copy and paste using clipboard in Vim

You can use the clipboard of X or your desktop from Vim to copy and paste text. This is useful to copy text from Vim to an external application or vice versa. It is also useful to copy and paste between multiple instances of Vim running in different terminals.

This capability is available in Vim only if it has been compiled with the +clipboard feature. With this feature, the doublequote-plus register "+ of Vim is connected to the desktop clipboard. Anything you cut, copy or yank to this register appears in the system clipboard. If you paste from this register in Vim, then the text in the clipboard appears.

  • To copy to system clipboard, visually mark the text and type: "+y

  • To paste from system clipboard: "+p

  • Note that Vim may be copying everything you visually mark to clipboard by default. This is because most Vim builds have the autoselect feature turned on. This autoselect feature can be disabled if you wish to.

  • This feature just does not work on some systems for me. I have resorted to using xclip to copy and paste between Vim and desktop clipboard.

Tried with: Vim 7.4 and Ubuntu 16.04

How to disable autoselect in Vim

When you visually mark or yank any text in terminal Vim, you may find that it is copied to the clipboard in your desktop. In GUI Vim, you may find that any text you mark with the mouse is also copied to the desktop clipboard. (This is different from marking and then explicitly choosing Copy from the toolbar). This is true for most modern compilations of Vim which are compiled with the +xterm_clipboard feature. This behavior of Vim is called autoselect.

If you use a clipboard manager, like Diodon for example, you may find that any editing session in Vim quickly overwrites everything in the clipboard manager. Thankfully, this autoselect behavior of Vim can be turned off.

To disable the automatic selection and copying of text in terminal Vim, add this line to your vimrc file:

set clipboard-=autoselect

To disable the automatic selection and copying of text in GUI Vim, add these lines to your vimrc file:

set guioptions-=a
set guioptions-=A
set guioptions-=aA

Tried with: Vim 7.4 and Ubuntu 14.04


ClipX is a tiny clipboard manager that does its sole job well and stays out of the way. It maintains a history of the most recent X items copied to the clipboard. When you need to paste back something you copied a while ago, just click the ClipX icon in the system tray to get a list of all your recent copies. Click on the item you want and it will be copied to the Windows clipboard, so you can paste it back anywhere using Ctrl+V.

Tried with: ClipX (x64)