KAEDE Hack blog

JavaScript 中心に ライブラリなどの使い方を解説する技術ブログ。

How to Copy to Clipboard in Vim

Copy to Clipboard in Vim

Why we need this

We copy words or lines, by useing a mouce or trackpad, double click or triple click, selecting by our finger.
However using Vim, copying lines by mouce might contain line numbers or line end charactors. It's annoying to delete the extra char every paste.
Vim has a nice commands that copies lines or words.

`yy`: copies one line to `register`.  
`num yy`: copies the numbers of lines to `register`.  
`yw`: copies one word to `register`.  
`p`: paste from the `register`.  

But this register is different from clipboard what we use copy and paste Control C & Vin the browser.

"*: by MacOS

`"*yy`: copies one line to `clipboard`  

f:id:kei_s_lifehack:20190923175020p:plain
yank
In MacOS, adding "*: to prefix enable to yank(copy) to clipboard, which is different in Linux.
* Make sure your Vim can use clipboard.
If your Vim is not supporting Clipboard, you cannot type "* command.
f:id:kei_s_lifehack:20190923175120p:plain
clipboard enabled

  • If not, install MacVim

github.com

"+: by Linux

In Linux, "*: do not use clipboard.
Clipboard read the "+ register.
Use "+ instead.

unnamed, unnamed plus

stackoverflow.com

vi.stackexchange.com

This "+ can also use in Mac.

Enable Clipboard by yy

"*or "+costs finger. It is far from default position. if yy reads clipboard, it saves our time a lot.

stackoverflow.com

set clipboard=unnamed

to ~/.vimrc. Very Very Very useful!!!!!