If I were asked to pick two applications to keep on a computer, I would probably say Vim and browser. The former for coding and the latter for looking up documentation. As such, I often have copy and pasting between the two.Today we will learn how to set up Vim to copy and paste to system clipboard, both on local machine and on remote server with X11 forwarding.
Vim’s Clipboard Support
First, we need to check if the Vim your computer ships with support clipboard. In terminal, type
vim --version | grep clipboard # grep will search for sentences containing clipboard
If you see
-xterm_clipboard, it means vim doesn’t support clipboard. On Ubuntu or Debian distro, type
sudo apt-get install vim-gtk to get clipboard support.
All that remain is simply getting used to using the clipboard registers. If you are not familiar with how they works, check out my previous post.
Now if you type
:reg in command mode, you will see that you have two extra registers:
"+. These are the clipboard registers your os uses. On Windows or Mac OS, these two are the same clipboard. On Linux,
"+ is the clipboard that
Ctrl-C, Ctrl-X, and Ctrl-V uses, while
"* is the current mouse selection. (Did you know? On Linux you can middle click to paste your mouse selection.)
Now to copy to system clipboard:
To paste from system clipboard:
Default Copy and Paste to Clipboard Register
Usually you don’t really care about all these register except the clipboard. You can add the following to .vimrc to set your clipboard register(
"+) the same as your unnamed register(
p will also use the system clipboard. Not that hard, was it?
Copying from remote server to local machine’s system clipboard is not that much harder. All we need to do is enabling server’s vim clipboard register(same as local machine) and setting up X11 forwarding.
X11 is a protocol of transferring GUI information. In X11 forwarding, we ask the server to forward GUI information to our local machine, including the clipboard.
The specifics differ from servers to servers, but usually you want to locate the
/etc/ssh/sshd_config file, and have the following line:
After that, ssh with the -Y option:
ssh -Y user@server
Viola! You can now copy and paste to system clipboard.